-
Notifications
You must be signed in to change notification settings - Fork 11
/
user_accountStore.go
157 lines (123 loc) · 3.55 KB
/
user_accountStore.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package storage
import (
_ "embed"
"github.com/google/uuid"
"go.uber.org/zap"
"github.com/cmsgov/easi-app/pkg/authentication"
"github.com/cmsgov/easi-app/pkg/sqlqueries"
"github.com/cmsgov/easi-app/pkg/sqlutils"
)
// UserAccountGetByCommonName gets a user account by a give username
func (s *Store) UserAccountGetByCommonName(commonName string) (*authentication.UserAccount, error) {
user := &authentication.UserAccount{}
stmt, err := s.db.PrepareNamed(sqlqueries.UserAccount.GetByCommonName)
if err != nil {
return nil, err
}
defer stmt.Close()
arg := map[string]interface{}{
"common_name": commonName,
}
err = stmt.Get(user, arg)
if err != nil {
if err.Error() == "sql: no rows in result set" { //EXPECT THERE TO BE NULL results, don't treat this as an error
return nil, nil
}
return nil, err
}
return user, nil
}
// UserAccountGetByUsername gets a user account by a give username
func (s *Store) UserAccountGetByUsername(username string) (*authentication.UserAccount, error) {
user := &authentication.UserAccount{}
stmt, err := s.db.PrepareNamed(sqlqueries.UserAccount.GetByUsername)
if err != nil {
return nil, err
}
defer stmt.Close()
arg := map[string]interface{}{
"username": username,
}
err = stmt.Get(user, arg)
if err != nil {
if err.Error() == "sql: no rows in result set" { //EXPECT THERE TO BE NULL results, don't treat this as an error
return nil, nil
}
return nil, err
}
return user, nil
}
// UserAccountGetByID gets a User account from the database by its internal id.
func (s *Store) UserAccountGetByID(np sqlutils.NamedPreparer, id uuid.UUID) (*authentication.UserAccount, error) {
user := &authentication.UserAccount{}
stmt, err := np.PrepareNamed(sqlqueries.UserAccount.GetByID)
if err != nil {
return nil, err
}
defer stmt.Close()
arg := map[string]interface{}{
"id": id,
}
err = stmt.Get(user, arg)
if err != nil {
return nil, err
}
return user, nil
}
// UserAccountGetByIDLOADER gets multiple User account from the database by its internal id.
func (s *Store) UserAccountGetByIDLOADER(
_ *zap.Logger,
paramTableJSON string,
) ([]*authentication.UserAccount, error) {
var userSlice []*authentication.UserAccount
stmt, err := s.db.PrepareNamed(sqlqueries.UserAccount.GetByIDLoader)
if err != nil {
return nil, err
}
defer stmt.Close()
arg := map[string]interface{}{
"paramTableJSON": paramTableJSON,
}
err = stmt.Select(&userSlice, arg)
if err != nil {
return nil, err
}
return userSlice, nil
}
// UserAccountCreate creates a new user account for a given username
func (s *Store) UserAccountCreate(np sqlutils.NamedPreparer, userAccount *authentication.UserAccount) (*authentication.UserAccount, error) {
user := &authentication.UserAccount{}
if userAccount.ID == uuid.Nil {
userAccount.ID = uuid.New()
}
stmt, err := np.PrepareNamed(sqlqueries.UserAccount.Create)
if err != nil {
return nil, err
}
defer stmt.Close()
err = stmt.Get(user, userAccount)
if err != nil {
return nil, err
}
return user, nil
}
// UserAccountUpdateByUserName updates an existing user account for a given username
func (s *Store) UserAccountUpdateByUserName(np sqlutils.NamedPreparer, userAccount *authentication.UserAccount) (
*authentication.UserAccount,
error,
) {
user := &authentication.UserAccount{}
if userAccount.ID == uuid.Nil {
userAccount.ID = uuid.New()
}
stmt, err := np.PrepareNamed(sqlqueries.UserAccount.UpdateByUsername)
if err != nil {
return nil, err
}
defer stmt.Close()
err = stmt.Get(user, userAccount)
if err != nil {
return nil, err
}
return user, nil
}