/
store.go
310 lines (266 loc) · 5.33 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package internal
import (
"context"
"fmt"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"nimona.io"
)
var ErrNotFound = fmt.Errorf("not found")
type Store interface {
PutUser(context.Context, *User) error
GetUser(context.Context, string) (*User, error)
PutProfile(context.Context, *Profile) error
GetProfile(context.Context, nimona.KeygraphID) (*Profile, error)
PutNote(context.Context, *Note) error
GetNotes(context.Context, string, int, int) ([]*Note, error)
PutFollow(context.Context, *Follow) error
GetFollowers(context.Context, nimona.KeygraphID) ([]*Follow, error)
GetFollowees(context.Context, nimona.KeygraphID) ([]*Follow, error)
PutSession(context.Context, *Session) error
GetSession(context.Context, string) (*Session, error)
}
type SQLStore struct {
db *gorm.DB
}
func NewSQLStore(gdb *gorm.DB) Store {
gdb.AutoMigrate(
&User{},
&Profile{},
&Follow{},
&Note{},
&Session{},
)
return &SQLStore{
db: gdb,
}
}
// PutUser will create or update a user
func (s *SQLStore) PutUser(
ctx context.Context,
req *User,
) error {
if req == nil {
return fmt.Errorf("failed to put user: nil request")
}
err := s.db.
WithContext(ctx).
Clauses(
clause.OnConflict{
UpdateAll: true,
},
).
Create(req).
Error
if err != nil {
return fmt.Errorf("failed to put user: %w", err)
}
return nil
}
func (s *SQLStore) GetUser(
ctx context.Context,
username string,
) (*User, error) {
if username == "" {
return nil, fmt.Errorf("failed to get user: nil request")
}
var user User
err := s.db.
WithContext(ctx).
Where("username = ?", username).
First(&user).
Error
if err != nil {
return nil, fmt.Errorf("failed to get user: %w", err)
}
// TODO: improve not found check
if user.KeygraphID.IsEmpty() {
return nil, ErrNotFound
}
return &user, nil
}
func (s *SQLStore) PutProfile(
ctx context.Context,
req *Profile,
) error {
if req == nil {
return fmt.Errorf("failed to put profile: nil request")
}
err := s.db.
WithContext(ctx).
Clauses(
clause.OnConflict{
UpdateAll: true,
},
).
Create(req).
Error
if err != nil {
return fmt.Errorf("failed to put profile: %w", err)
}
return nil
}
func (s *SQLStore) GetProfile(
ctx context.Context,
keygraphID nimona.KeygraphID,
) (*Profile, error) {
if keygraphID.IsEmpty() {
return nil, fmt.Errorf("failed to get profile: nil request")
}
var profile Profile
err := s.db.
WithContext(ctx).
Where("keygraph_id = ?", keygraphID).
First(&profile).
Error
if err != nil {
return nil, ErrNotFound
}
return &profile, nil
}
func (s *SQLStore) PutNote(
ctx context.Context,
req *Note,
) error {
if req == nil {
return fmt.Errorf("failed to put note: nil request")
}
err := s.db.
WithContext(ctx).
Clauses(
clause.OnConflict{
UpdateAll: true,
},
).
Create(req).
Error
if err != nil {
return fmt.Errorf("failed to put note: %w", err)
}
return nil
}
func (s *SQLStore) GetNotes(
ctx context.Context,
identityNRI string,
// TODO: add filters
offset int,
limit int,
) ([]*Note, error) {
query := s.db.
WithContext(ctx).
Preload("Profile")
if identityNRI != "" {
query = query.Where("identity = ?", identityNRI)
}
var notes []*Note
err := query.
Order("created_at DESC").
Offset(offset).
Limit(limit).
Find(¬es).
Error
if err != nil {
return nil, fmt.Errorf("failed to get notes: %w", err)
}
return notes, nil
}
func (s *SQLStore) PutFollow(
ctx context.Context,
req *Follow,
) error {
if req == nil {
return fmt.Errorf("failed to put follow: nil request")
}
err := s.db.
WithContext(ctx).
Clauses(
clause.OnConflict{
UpdateAll: true,
},
).
Create(req).
Error
if err != nil {
return fmt.Errorf("failed to put follow: %w", err)
}
return nil
}
func (s *SQLStore) GetFollowers(
ctx context.Context,
followee nimona.KeygraphID,
) ([]*Follow, error) {
if followee.IsEmpty() {
return nil, fmt.Errorf("failed to get followers: nil request")
}
var follows []*Follow
err := s.db.
WithContext(ctx).
Where("followee = ?", followee).
Find(&follows).
Error
if err != nil {
return nil, fmt.Errorf("failed to get follows: %w", err)
}
return follows, nil
}
func (s *SQLStore) GetFollowees(
ctx context.Context,
follower nimona.KeygraphID,
) ([]*Follow, error) {
if follower.IsEmpty() {
return nil, fmt.Errorf("failed to get followees: nil request")
}
var follows []*Follow
err := s.db.
WithContext(ctx).
Where("follower = ?", follower).
Find(&follows).
Error
if err != nil {
return nil, fmt.Errorf("failed to get follows: %w", err)
}
return follows, nil
}
func (s *SQLStore) PutSession(
ctx context.Context,
req *Session,
) error {
if req == nil {
return fmt.Errorf("failed to put session: nil request")
}
err := s.db.
WithContext(ctx).
Clauses(
clause.OnConflict{
UpdateAll: true,
},
).
Create(req).
Error
if err != nil {
return fmt.Errorf("failed to put session: %w", err)
}
return nil
}
func (s *SQLStore) GetSession(
ctx context.Context,
id string,
) (*Session, error) {
if id == "" {
return nil, fmt.Errorf("nil request")
}
var session Session
err := s.db.
WithContext(ctx).
Where("id = ?", id).
First(&session).
Error
if err != nil {
return nil, fmt.Errorf("failed to get session: %w", err)
}
// TODO: improve not found check
if session.ID == "" {
return nil, ErrNotFound
}
return &session, nil
}