-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_user.go
66 lines (56 loc) · 1.56 KB
/
db_user.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
// +build plain
package main
import (
"log"
_ "github.com/go-sql-driver/mysql"
)
// NOTE user management
// Count users from database.
func sqlUserCount() (error, int) {
var n int
if err := db.QueryRow("SELECT COUNT(*) from user").Scan(&n); err != nil {
log.Println(err)
return err, 0
}
return nil, n
}
// Get a user identifyed by email and password.
func sqlAuthenticateUser(u *User) error {
if err := db.QueryRow("SELECT * FROM user WHERE email=? AND password=?", u.Email, u.Password).Scan(&u.Id, &u.Name, &u.Email, &u.Password, &u.isActive); err != nil {
log.Println(err)
return err
}
return nil
}
// Get the user identifyed by email.
func sqlGetUser(u *User) error {
if err := db.QueryRow("SELECT * FROM user WHERE email=?", u.Email).Scan(&u.Id, &u.Name, &u.Email, &u.Password, &u.isActive); err != nil {
log.Println(err)
return err
}
return nil
}
// Create new user identifyed by name, email and password.
func sqlInsert(u *User) error {
if _, err := db.Exec("INSERT user SET name=?, email=?, password=?", &u.Name, &u.Email, &u.Password); err != nil {
log.Println(err)
return err
}
return nil
}
// Update a user identifyed by email and password.
func sqlUpdateUser(u *User) error {
if _, err := db.Exec("UPDATE user SET name=?, password=? WHERE email=?", u.Name, u.Password, u.Email); err != nil {
log.Println(err)
return err
}
return nil
}
// Delete a user identifyed by email.
func sqlDeleteUser(u *User) error {
if _, err := db.Exec("DELETE FROM user WHERE email=?", u.Email); err != nil {
log.Println(err)
return err
}
return nil
}