-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_data.go
78 lines (67 loc) · 1.88 KB
/
auth_data.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
package store
import (
"context"
sq "github.com/Masterminds/squirrel"
"github.com/google/uuid"
"github.com/l-orlov/slim-fairy/bot/internal/model"
)
// CreateAuthDataTx creates model.AuthData in transaction
func (s *Storage) CreateAuthDataTx(ctx context.Context, tx Tx, record *model.AuthData) error {
return s.createAuthData(ctx, tx, record)
}
// createAuthData creates model.AuthData
func (s *Storage) createAuthData(ctx context.Context, db Querier, record *model.AuthData) error {
query := psql().
Insert(record.DbTable()).
SetMap(authDataAttrs(record)).
Suffix("RETURNING " + asteriskAuthData)
err := Getx(ctx, db, record, query)
if err != nil {
return dbError(err)
}
return nil
}
// UpdateAuthDataPassword updates password in model.AuthData
func (s *Storage) UpdateAuthDataPassword(ctx context.Context, record *model.AuthData) error {
query := psql().
Update(record.DbTable()).
Where(sq.Eq{
"source_id": record.SourceID,
"source_type": record.SourceType,
}).
Set("password", record.Password).
Suffix("RETURNING " + asteriskAuthData)
err := Getx(ctx, s.pool, record, query)
if err != nil {
return dbError(err)
}
return nil
}
// GetAuthDataBySourceIDAndType gets model.AuthData by source_id, source_type
func (s *Storage) GetAuthDataBySourceIDAndType(
ctx context.Context,
sourceID uuid.UUID,
sourceType model.AuthDataSourceType,
) (*model.AuthData, error) {
record := &model.AuthData{}
query := psql().
Select(asteriskAuthData).
From(record.DbTable()).
Where(sq.Eq{
"source_id": sourceID,
"source_type": sourceType,
}).
Limit(1)
err := Getx(ctx, s.pool, record, query)
if err != nil {
return nil, dbError(err)
}
return record, nil
}
func authDataAttrs(record *model.AuthData) map[string]interface{} {
return map[string]interface{}{
"source_id": record.SourceID,
"source_type": record.SourceType,
"password": record.Password,
}
}