-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccounts.go
133 lines (107 loc) · 2.96 KB
/
accounts.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
package sql
import (
"context"
"errors"
"{{ cookiecutter.group_name }}/{{ cookiecutter.project_name }}/db/models"
"{{ cookiecutter.group_name }}/{{ cookiecutter.project_name }}/db/queries"
"{{ cookiecutter.group_name }}/{{ cookiecutter.project_name }}/internal/utils"
"github.com/jackc/pgx/v5"
"github.com/rs/zerolog/log"
)
func (q *Queries) GetAccounts(ctx context.Context) ([]models.Account, error) {
ctx, span := utils.TracerWithContext(ctx, "db.GetAccounts")
d := []models.Account{}
rows, err := q.db.Query(ctx, queries.GET_ACCOUNTS_QUERY)
if err != nil {
return d, err
}
for rows.Next() {
a := models.Account{}
r := models.Role{
Permissions: []models.Permission{},
}
if err := rows.Scan(&a.ID, &a.Email, &a.Password, &r.ID, &r.Name, &a.IsActive, &a.CreatedAt, &a.UpdatedAt); err != nil {
return d, err
}
a.Role = r
d = append(d, a)
}
span.End()
return d, nil
}
func (q *Queries) GetAccountByEmail(ctx context.Context, email string) (models.Account, error) {
ctx, span := utils.TracerWithContext(ctx, "db.GetAccountByEmail")
d := models.Account{}
rows, err := q.db.Query(ctx, queries.GET_ACCOUNTS_BY_EMAIL_QUERY, email)
if err != nil {
return d, err
}
for rows.Next() {
a := models.Account{}
r := models.Role{
Permissions: []models.Permission{},
}
if err := rows.Scan(&a.ID, &a.Email, &a.Password, &r.ID, &r.Name, &a.IsActive, &a.CreatedAt, &a.UpdatedAt); err != nil {
log.Error().Err(err).Send()
}
a.Role = r
d = a
}
if d.ID == 0 {
return d, errors.New("no records found")
}
span.End()
return d, nil
}
func (q *Queries) GetAccountById(ctx context.Context, id int) (models.Account, error) {
ctx, span := utils.TracerWithContext(ctx, "db.GetAccountById")
d := models.Account{}
rows, err := q.db.Query(ctx, queries.GET_ACCOUNTS_BY_ID_QUERY, id)
if err != nil {
return d, err
}
for rows.Next() {
a := models.Account{}
r := models.Role{
Permissions: []models.Permission{},
}
if err := rows.Scan(&a.ID, &a.Email, &a.Password, &r.ID, &r.Name, &a.IsActive, &a.CreatedAt, &a.UpdatedAt); err != nil {
log.Error().Err(err).Send()
}
a.Role = r
d = a
}
if d.ID == 0 {
return d, errors.New("no records found")
}
span.End()
return d, nil
}
func (q *Queries) CreateAccount(ctx context.Context, email string, password string) (*models.Account, error) {
ctx, span := utils.TracerWithContext(ctx, "db.CreateAccount")
d := models.Account{}
_, err := q.db.Exec(ctx, `
insert into accounts (email, password)
values (@email, @password)
returning *;
`, pgx.StrictNamedArgs{
"email": email,
"password": password,
})
if err != nil {
return nil, err
}
//for rows.Next() {
//a := models.Account{}
//r := models.Role{
//Permissions: []models.Permission{},
//}
//if err := rows.Scan(&a.ID, &a.Email, &a.Password, &r.ID, &r.Name, &a.IsActive, &a.CreatedAt, &a.UpdatedAt); err != nil {
//log.Error().Err(err).Send()
//}
//a.Role = r
//d = a
//}
span.End()
return &d, nil
}