/
users.go
60 lines (46 loc) 路 1.61 KB
/
users.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
package store
import (
"context"
"database/sql"
"github.com/Piszmog/pathwise/db"
"github.com/Piszmog/pathwise/types"
)
type UserStore struct {
Database db.Database
}
func (s *UserStore) Insert(ctx context.Context, user types.User) error {
_, err := s.Database.DB().ExecContext(ctx, userInsertQuery, user.Email, user.Password)
return err
}
const userInsertQuery = `INSERT INTO users (email, password) VALUES (?, ?)`
func (s *UserStore) GetByEmail(ctx context.Context, email string) (types.User, error) {
row := s.Database.DB().QueryRowContext(ctx, userGetQuery, email)
return scanUser(row)
}
const userGetQuery = `SELECT id, created_at, updated_at, email, password FROM users WHERE email = ?`
func (s *UserStore) GetByID(ctx context.Context, id int64) (types.User, error) {
row := s.Database.DB().QueryRowContext(ctx, userGetByIDQuery, id)
return scanUser(row)
}
const userGetByIDQuery = `SELECT id, created_at, updated_at, email, password FROM users WHERE id = ?`
func scanUser(row *sql.Row) (types.User, error) {
var user types.User
err := row.Scan(
&user.ID,
&user.CreatedAt,
&user.UpdatedAt,
&user.Email,
&user.Password,
)
return user, err
}
func (s *UserStore) Delete(ctx context.Context, id int64) error {
_, err := s.Database.DB().ExecContext(ctx, userDeleteQuery, id)
return err
}
const userDeleteQuery = `DELETE FROM users WHERE id = ?`
func (s *UserStore) UpdatePassword(ctx context.Context, id int64, password string) error {
_, err := s.Database.DB().ExecContext(ctx, userUpdatePasswordQuery, password, id)
return err
}
const userUpdatePasswordQuery = `UPDATE users SET password = ? WHERE id = ?`