-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
209 lines (174 loc) · 4.93 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package repositories
import (
"context"
// "database/sql"
"time"
"log"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/bartmika/growlog-server/internal/models"
)
type UserRepo struct {
dbpool *pgxpool.Pool
}
func NewUserRepo(dbpool *pgxpool.Pool) *UserRepo {
return &UserRepo{
dbpool: dbpool,
}
}
func (r *UserRepo) Insert(ctx context.Context, m *models.User) error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
query := `
INSERT INTO users (
uuid, tenant_id, email, first_name, last_name, password_algorithm, password_hash, state,
role_id, timezone, created_time, modified_time, joined_time, salt, was_email_activated,
pr_access_code, pr_expiry_time, old_id
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18
)`
err := r.dbpool.QueryRow(ctx, query).Scan(&m)
if err != nil {
log.Println("UserRepo|Insert|err", err)
return err
}
return nil
}
func (r *UserRepo) UpdateById(ctx context.Context, m *models.User) error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
query := `
UPDATE
users
SET
tenant_id = $1, email = $2, first_name = $3, last_name = $4, password_algorithm = $5, password_hash = $6, state = $7,
role_id = $8, timezone = $9, created_time = $10, modified_time = $11, joined_time = $12, salt = $13, was_email_activated = $14,
pr_access_code = $15, pr_expiry_time = $16
WHERE
id = $17`
err := r.dbpool.QueryRow(ctx, query).Scan(&m)
if err != nil {
log.Println("UserRepo|UpdateById|err", err)
return err
}
return nil
}
//
func (r *UserRepo) UpdateByEmail(ctx context.Context, m *models.User) error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
query := `
UPDATE
users
SET
tenant_id = $1, email = $2, first_name = $3, last_name = $4, password_algorithm = $5, password_hash = $6, state = $7,
role_id = $8, timezone = $9, created_time = $10, modified_time = $11, joined_time = $12, salt = $13, was_email_activated = $14,
pr_access_code = $15, pr_expiry_time = $16
WHERE
email = $2`
err := r.dbpool.QueryRow(ctx, query).Scan(&m)
if err != nil {
log.Println("UserRepo|UpdateByEmail|err", err)
return err
}
return nil
}
func (r *UserRepo) GetById(ctx context.Context, id uint64) (*models.User, error) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
m := new(models.User)
query := `
SELECT
id, uuid, tenant_id, email, first_name, last_name, password_algorithm, password_hash, state,
role_id, timezone, created_time, modified_time, joined_time, salt, was_email_activated, pr_access_code, pr_expiry_time
FROM
users
WHERE
id = $1`
err := r.dbpool.QueryRow(ctx, query).Scan(&m)
if err != nil {
log.Println("UserRepo|GetById|err", err)
return nil, err
}
return m, nil
}
func (r *UserRepo) GetByEmail(ctx context.Context, email string) (*models.User, error) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
m := new(models.User)
query := `
SELECT
id, uuid, tenant_id, email, first_name, last_name, password_algorithm, password_hash, state,
role_id, timezone, created_time, modified_time, joined_time, salt, was_email_activated, pr_access_code, pr_expiry_time
FROM
users
WHERE
email = $1`
err := r.dbpool.QueryRow(ctx, query).Scan(&m)
if err != nil {
log.Println("UserRepo|GetByEmail|err", err)
return nil, err
}
return m, nil
}
func (r *UserRepo) CheckIfExistsById(ctx context.Context, id uint64) (bool, error) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
var exists bool
query := `
SELECT
1
FROM
users
WHERE
id = $1`
err := r.dbpool.QueryRow(ctx, query).Scan(&exists)
if err != nil {
log.Println("UserRepo|CheckIfExistsById|err", err)
return false, err
}
return exists, nil
}
func (r *UserRepo) CheckIfExistsByEmail(ctx context.Context, email string) (bool, error) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
var exists bool
query := `
SELECT
1
FROM
users
WHERE
email = $1`
err := r.dbpool.QueryRow(ctx, query).Scan(&exists)
if err != nil {
log.Println("UserRepo|CheckIfExistsByEmail|err", err)
return false, err
}
return exists, nil
}
func (r *UserRepo) InsertOrUpdateById(ctx context.Context, m *models.User) error {
if m.Id == 0 {
return r.Insert(ctx, m)
}
doesExist, err := r.CheckIfExistsById(ctx, m.Id)
if err != nil {
return err
}
if doesExist == false {
return r.Insert(ctx, m)
}
return r.UpdateById(ctx, m)
}
func (r *UserRepo) InsertOrUpdateByEmail(ctx context.Context, m *models.User) error {
if m.Id == 0 {
return r.Insert(ctx, m)
}
doesExist, err := r.CheckIfExistsByEmail(ctx, m.Email)
if err != nil {
return err
}
if doesExist == false {
return r.Insert(ctx, m)
}
return r.UpdateByEmail(ctx, m)
}