-
Notifications
You must be signed in to change notification settings - Fork 1
/
user_repository.go
59 lines (49 loc) · 1.57 KB
/
user_repository.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
package postgres
import (
"database/sql"
"time"
"github.com/riser-platform/riser-server/pkg/core"
)
type userRepository struct {
db *sql.DB
}
func NewUserRepository(db *sql.DB) core.UserRepository {
return &userRepository{db}
}
func (r *userRepository) GetByApiKey(keyHash []byte) (*core.User, error) {
user := &core.User{}
err := r.db.QueryRow(`SELECT riser_user.id, username, doc
FROM riser_user
INNER JOIN apikey ON (riser_user.id = apikey.riser_user_id)
WHERE apikey.key_hash = $1`, keyHash).Scan(&user.Id, &user.Username, &user.Doc)
if err == sql.ErrNoRows {
return nil, core.ErrNotFound
}
if err != nil {
return nil, err
}
return user, nil
}
// TODO: Refactor common projection, scanning, and error handling
func (r *userRepository) GetByUsername(username string) (*core.User, error) {
user := &core.User{}
err := r.db.QueryRow(`SELECT id, username, doc
FROM riser_user
WHERE username = $1`, username).Scan(&user.Id, &user.Username, &user.Doc)
if err == sql.ErrNoRows {
return nil, core.ErrNotFound
}
if err != nil {
return nil, err
}
return user, nil
}
func (r *userRepository) Create(newUser *core.NewUser) error {
doc := &core.UserDoc{Created: time.Now().UTC()}
_, err := r.db.Exec("INSERT INTO riser_user (id, username, doc) VALUES ($1, $2, $3) RETURNING id", newUser.Id, newUser.Username, doc)
return err
}
func (r *userRepository) GetActiveCount() (activeUserCount int, err error) {
err = r.db.QueryRow("SELECT COUNT(1) FROM riser_user INNER JOIN apikey ON riser_user.id = apikey.riser_user_id").Scan(&activeUserCount)
return activeUserCount, err
}