This repository has been archived by the owner on May 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
store.go
99 lines (87 loc) · 1.95 KB
/
store.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
package keys
import (
"context"
"github.com/jackc/pgx"
"github.com/jackc/pgx/pgtype"
"github.com/joyent/triton-service-groups/convert"
)
type Store struct {
pool *pgx.ConnPool
}
// NewStore returns a new store object.
func NewStore(pool *pgx.ConnPool) *Store {
return &Store{
pool: pool,
}
}
// FindByID finds an account by a specific ID.
func (s *Store) FindByID(ctx context.Context, keyID string) (*Key, error) {
var (
id pgtype.UUID
name string
fingerprint string
material string
createdAt pgtype.Timestamp
updatedAt pgtype.Timestamp
)
query := `
SELECT id, name, fingerprint, material, created_at, updated_at
FROM tsg_keys
WHERE id = $1 AND archived = false;
`
err := s.pool.QueryRowEx(ctx, query, nil, keyID).Scan(
&id,
&name,
&fingerprint,
&material,
&createdAt,
&updatedAt,
)
if err != nil {
return nil, err
}
key := New(s)
key.ID = convert.BytesToUUID(id.Bytes)
key.Name = name
key.Fingerprint = fingerprint
key.Material = material
key.CreatedAt = createdAt.Time
key.UpdatedAt = updatedAt.Time
return key, nil
}
// FindByName finds an account by a specific account_name.
func (s *Store) FindByName(ctx context.Context, keyName string, accountID string) (*Key, error) {
var (
id pgtype.UUID
name string
fingerprint string
material string
createdAt pgtype.Timestamp
updatedAt pgtype.Timestamp
)
query := `
SELECT id, name, fingerprint, material, created_at, updated_at
FROM tsg_keys
WHERE name = $1 AND account_id = $2 AND archived = false;
`
err := s.pool.QueryRowEx(ctx, query, nil, keyName, accountID).Scan(
&id,
&name,
&fingerprint,
&material,
&createdAt,
&updatedAt,
)
if err != nil {
return nil, err
}
key := New(s)
key.ID = convert.BytesToUUID(id.Bytes)
key.AccountID = accountID
key.Name = name
key.Fingerprint = fingerprint
key.Material = material
key.CreatedAt = createdAt.Time
key.UpdatedAt = updatedAt.Time
return key, nil
}