-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
39 lines (33 loc) · 1.15 KB
/
models.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import "database/sql"
type User struct {
ID int
TelegramID int64
Cookie string
DonemID string
Alarm bool
Grades string
}
func GetUserByTelegramID(db *sql.DB, telegramID int64) (*User, error) {
user := &User{}
row := db.QueryRow("SELECT id, telegram_id, cookie, donemid, alarm, grades FROM users WHERE telegram_id = ?", telegramID)
err := row.Scan(&user.ID, &user.TelegramID, &user.Cookie, &user.DonemID, &user.Alarm, &user.Grades)
if err != nil {
return nil, err
}
return user, nil
}
func InsertUser(db *sql.DB, user *User) error {
_, err := db.Exec("INSERT INTO users (telegram_id, cookie, donemid, alarm, grades) VALUES (?, ?, ?, ?, ?)",
user.TelegramID, user.Cookie, user.DonemID, user.Alarm, user.Grades)
return err
}
func UpdateUser(db *sql.DB, user *User) error {
_, err := db.Exec("UPDATE users SET cookie = ?, donemid = ?, alarm = ?, grades = ? WHERE telegram_id = ?",
user.Cookie, user.DonemID, user.Alarm, user.Grades, user.TelegramID)
return err
}
func UpdateGrades(db *sql.DB, userID int, grades string) error {
_, err := db.Exec("UPDATE users SET grades = ? WHERE id = ?", grades, userID)
return err
}