-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.go
66 lines (58 loc) · 1.19 KB
/
db.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// User represents our user attributes
type User struct {
gorm.Model
Name string
Password string
Email string
Secret string
}
// DB provides pointer to our database
var DB *gorm.DB
func setupDB(fname string) {
db, err := gorm.Open(sqlite.Open(fname), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
// Migrate the schema
db.AutoMigrate(&User{})
// initialize DB pointer
DB = db
}
func updateUser(name, secret string) {
var user User
DB.First(&user, "name = ?", name)
user.Secret = secret
DB.Save(&user)
}
func addUser(name, password, email, secret string) {
user := &User{
Name: name,
Email: email,
Password: password,
Secret: secret,
}
DB.Create(user)
}
func userExist(name, password string) bool {
var user User
if password == "do not check" {
DB.First(&user, "name = ?", name)
} else {
DB.First(&user, "name = ? AND password = ?", name, password)
}
if user.Name == name {
return true
}
return false
}
func findUserSecret(name string) string {
var user User
// DB.First(&user, "name = ?", name)
DB.Find(&user, "name = ?", name)
return user.Secret
}