-
Notifications
You must be signed in to change notification settings - Fork 2
/
user.go
49 lines (37 loc) · 1.04 KB
/
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
package sqlite
import (
"database/sql"
"github.com/cheatsnake/botyard/internal/entities/user"
"github.com/cheatsnake/botyard/pkg/exterr"
)
func (s *Storage) AddUser(user *user.User) error {
q := `INSERT INTO users (id, nickname) VALUES (?, ?)`
_, err := s.conn.Exec(q, user.Id, user.Nickname)
if err != nil {
// TODO logger
return exterr.ErrorInternalServer("Can't save user to database.")
}
return nil
}
func (s *Storage) GetUser(id string) (*user.User, error) {
q := `SELECT id, nickname FROM users WHERE id = ?`
var u user.User
err := s.conn.QueryRow(q, id).Scan(&u.Id, &u.Nickname)
if err == sql.ErrNoRows {
return nil, exterr.ErrorNotFound("User not found.")
}
if err != nil {
// TODO logger
return nil, exterr.ErrorInternalServer("Can't get user from database.")
}
return &u, nil
}
func (s *Storage) DeleteUser(id string) error {
q := `DELETE FROM users WHERE id = ?`
_, err := s.conn.Exec(q, id)
if err != nil {
// TODO logger
return exterr.ErrorInternalServer("Can't delete user from database.")
}
return nil
}