-
Notifications
You must be signed in to change notification settings - Fork 0
/
roles.go
73 lines (66 loc) · 1.55 KB
/
roles.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
package pgx
import (
"context"
"errors"
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/vlad-marlo/godo/internal/model"
"github.com/vlad-marlo/godo/internal/store"
"go.uber.org/zap"
)
type RoleRepository struct {
pool *pgxpool.Pool
log *zap.Logger
}
func NewRoleRepository(cli Client) *RoleRepository {
return &RoleRepository{
pool: cli.P(),
log: cli.L(),
}
}
func (repo *RoleRepository) Create(ctx context.Context, role *model.Role) error {
if role == nil {
return store.ErrNilReference
}
if err := repo.pool.QueryRow(
ctx,
`INSERT INTO roles(members, tasks, reviews, comments) VALUES ($1, $2, $3, $4) RETURNING id;`,
role.Members,
role.Tasks,
role.Reviews,
role.Comments,
).Scan(&role.ID); err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
switch pgErr.Code {
case pgerrcode.UniqueViolation:
return store.ErrUniqueViolation
default:
}
}
zap.L().Log(_unknownLevel, "store: role: create", traceError(err)...)
return unknown(err)
}
return nil
}
func (repo *RoleRepository) Get(ctx context.Context, role *model.Role) error {
if role == nil {
return store.ErrNilReference
}
if err := repo.pool.QueryRow(
ctx,
`SELECT id FROM roles WHERE members = $1 AND tasks = $2 AND reviews = $3 AND comments = $4`,
role.Members,
role.Tasks,
role.Reviews,
role.Comments,
).Scan(&role.ID); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return repo.Create(ctx, role)
}
return unknown(err)
}
return nil
}