-
Notifications
You must be signed in to change notification settings - Fork 28
/
store_user.go
66 lines (54 loc) · 1.31 KB
/
store_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
package rolesgroups
import (
"database/sql"
"errors"
"github.com/authgear/authgear-server/pkg/api"
"github.com/authgear/authgear-server/pkg/lib/infra/db"
"github.com/lib/pq"
)
func (s *Store) scanUserID(scanner db.Scanner) (string, error) {
userId := ""
err := scanner.Scan(&userId)
if err != nil {
return "", err
}
return userId, nil
}
func (s *Store) selectUserIDQuery() db.SelectBuilder {
return s.SQLBuilder.Select("id").From(s.SQLBuilder.TableName("_auth_user"))
}
func (s *Store) GetUserByID(id string) (string, error) {
q := s.selectUserIDQuery().Where("id = ?", id)
row, err := s.SQLExecutor.QueryRowWith(q)
if err != nil {
return "", err
}
r, err := s.scanUserID(row)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return "", api.ErrUserNotFound
}
return "", err
}
return r, nil
}
func (s *Store) GetManyUsersByIds(ids []string) ([]string, error) {
q := s.selectUserIDQuery().Where("id = ANY (?)", pq.Array(ids))
return s.queryUserIDs(q)
}
func (s *Store) queryUserIDs(q db.SelectBuilder) ([]string, error) {
rows, err := s.SQLExecutor.QueryWith(q)
if err != nil {
return nil, err
}
defer rows.Close()
var userIDs []string
for rows.Next() {
r, err := s.scanUserID(rows)
if err != nil {
return nil, err
}
userIDs = append(userIDs, r)
}
return userIDs, nil
}