-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices.go
57 lines (40 loc) · 1.01 KB
/
services.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
package main
import (
"log"
)
func getOne(id int) (user customUser, _error error) {
user = customUser{}
_error = db.QueryRow("SELECT id, name FROM custom_user WHERE id = ?;", id).Scan(&user.id, &user.name)
return
}
func getMany(id int) ([]customUser, error) {
users := []customUser{}
rows, _error := db.Query("SELECT id, name FROM custom_user WHERE id >= ?;", id)
for rows.Next() {
user := customUser{}
_error = rows.Scan(&user.id, &user.name)
if _error != nil {
log.Fatalln(_error.Error())
}
users = append(users, user)
}
return users, _error
}
func (user *customUser) Insert() (_error error) {
sql := "INSERT INTO custom_user(id, name) VALUES(?, ?);"
statement, _error := db.Prepare(sql)
if _error != nil {
log.Fatalln(_error.Error())
}
defer statement.Close()
_, _error = statement.Exec(user.id, user.name)
return _error
}
func (user *customUser) Update() (_error error) {
_, _error = db.Exec(
"UPDATE custom_user SET name = ? where id = ?",
user.name,
user.id,
)
return
}