-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathresolver.go
338 lines (321 loc) · 9.68 KB
/
resolver.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package server
//go:generate go run github.com/99designs/gqlgen
import (
"context"
"errors"
"github.com/jinzhu/gorm"
gonanoid "github.com/matoous/go-nanoid/v2"
) // THIS CODE IS A STARTING POINT ONLY. IT WILL NOT BE UPDATED WITH SCHEMA CHANGES.
type CtxUserID string
const (
// MsgNotAuthenticated is the constant for Not Authenticated message
MsgNotAuthenticated string = "NotAuthenticated"
// CtxUserIDKey holds the key for 'userid' value
CtxUserIDKey CtxUserID = "userid"
// IDSize is the size of the UIDs generated for DB columns
IDSize int = 4
)
// Resolver holds the Query, mutation and subscription resolvers
type Resolver struct {
DB *gorm.DB
}
// Mutation returns an instance of mutationResolver
func (r *Resolver) Mutation() MutationResolver {
return &mutationResolver{r}
}
// Query returns an instance of queryResolver
func (r *Resolver) Query() QueryResolver {
return &queryResolver{r}
}
// Subscription returns an instance of subscriptionResolver
func (r *Resolver) Subscription() SubscriptionResolver {
return &subscriptionResolver{r}
}
type mutationResolver struct{ *Resolver }
func (r *mutationResolver) CreateTodo(ctx context.Context, title string, notes []string, labels []*string, color *string, isCheckboxMode *bool) (*Todo, error) {
if userID := ctx.Value(CtxUserIDKey); userID != "" {
userID := userID.(string)
newTodoID, _ := gonanoid.New(IDSize)
todo := Todo{
ID: newTodoID,
Title: title,
UserID: userID,
Notes: make([]*Note, len(notes)),
}
if color != nil {
todo.Color = *color
}
if isCheckboxMode != nil {
todo.IsCheckboxMode = *isCheckboxMode
}
for index, note := range notes {
newNoteID, _ := gonanoid.New(IDSize)
todo.Notes[index] = &Note{
ID: newNoteID,
Text: note,
IsCompleted: false,
}
}
if err := r.DB.Where("id in (?)", labels).Find(&todo.Labels).Error; err != nil { // Load the related labels
return nil, err
}
if err := r.DB.Create(&todo).Error; err != nil {
return nil, err
}
return &todo, nil
}
return nil, errors.New(MsgNotAuthenticated)
}
func (r *mutationResolver) UpdateTodo(ctx context.Context, id string, title *string, notes []*NotesInput, labels []*string, color *string, isCheckboxMode *bool) (*Todo, error) {
if userID := ctx.Value(CtxUserIDKey); userID != "" {
userID := userID.(string)
todo := Todo{
ID: id,
UserID: userID,
Labels: []*Label{},
Notes: []*Note{},
}
if err := r.DB.Where("user_id = ?", userID).Preload("Notes").Preload("Labels").Find(&todo).Error; err != nil {
return nil, err
}
if title != nil {
todo.Title = *title
}
if color != nil {
todo.Color = *color
}
if isCheckboxMode != nil {
todo.IsCheckboxMode = *isCheckboxMode
}
if notes != nil {
nts := make([]*Note, len(notes))
for index, note := range notes {
newNoteID, _ := gonanoid.New(IDSize)
nts[index] = &Note{
ID: newNoteID,
Text: note.Text,
IsCompleted: note.IsCompleted,
}
}
// Updating Association just updates the references, won't clear the data. So, manually deleting the notes
if len(todo.Notes) > 0 {
notesIDs := make([]string, len(todo.Notes))
for index, noteItem := range todo.Notes {
notesIDs[index] = noteItem.ID
}
r.DB.Where("id in (?)", notesIDs).Delete(Note{})
}
todo.Notes = nts
}
if labels != nil {
lbls := []*Label{}
r.DB.Where("id in (?)", labels).Find(&lbls)
r.DB.Model(&todo).Association("Labels").Clear()
todo.Labels = lbls
}
if err := r.DB.Save(&todo).Error; err != nil {
return nil, err
}
return &todo, nil
}
return nil, errors.New(MsgNotAuthenticated)
}
func (r *mutationResolver) DeleteTodo(ctx context.Context, id string) (*Todo, error) {
if userID := ctx.Value(CtxUserIDKey); userID != "" {
userID := userID.(string)
todo := Todo{
ID: id,
UserID: userID,
Notes: []*Note{},
}
if err := r.DB.Where("user_id = ?", userID).Preload("Notes").Find(&todo).Error; err != nil { // Only load associated notes
return nil, err
}
if err := r.DB.Model(&todo).Association("Labels").Clear().Error; err != nil {
return nil, err
}
if err := r.DB.Delete(todo).Error; err != nil {
return nil, err
}
return &todo, nil
}
return nil, errors.New(MsgNotAuthenticated)
}
func (r *mutationResolver) CopyTodo(ctx context.Context, sourceID string) (*Todo, error) {
if userID := ctx.Value(CtxUserIDKey); userID != "" {
userID := userID.(string)
todo := Todo{
ID: sourceID,
UserID: userID,
Labels: []*Label{},
Notes: []*Note{},
}
if err := r.DB.Where("user_id = ?", userID).Preload("Notes").Preload("Labels").First(&todo).Error; err != nil {
return nil, err
}
todo.ID, _ = gonanoid.New(IDSize)
for _, note := range todo.Notes {
note.ID, _ = gonanoid.New(IDSize)
}
if err := r.DB.Create(&todo).Error; err != nil {
return nil, err
}
return &todo, nil
}
return nil, errors.New(MsgNotAuthenticated)
}
func (r *mutationResolver) CreateLabel(ctx context.Context, name string) (*Label, error) {
if userID := ctx.Value(CtxUserIDKey); userID != "" {
userID := userID.(string)
newLabelID, _ := gonanoid.New(IDSize)
label := Label{
ID: newLabelID,
Name: name,
UserID: userID,
}
if err := r.DB.Create(&label).Error; err != nil {
return nil, err
}
return &label, nil
}
return nil, errors.New(MsgNotAuthenticated)
}
func (r *mutationResolver) DeleteLabel(ctx context.Context, id string) (*Label, error) {
panic("not implemented")
}
func (r *mutationResolver) UpdateUser(ctx context.Context, listMode *bool, darkMode *bool) (*User, error) {
if userID := ctx.Value(CtxUserIDKey); userID != "" {
userID := userID.(string)
user := User{
ID: userID,
}
if err := r.DB.First(&user).Error; err != nil {
return nil, err
}
if listMode != nil {
user.ListMode = *listMode
}
if darkMode != nil {
user.DarkMode = *darkMode
}
if err := r.DB.Save(&user).Error; err != nil {
return nil, err
}
return &user, nil
}
return nil, errors.New(MsgNotAuthenticated)
}
type queryResolver struct{ *Resolver }
func (r *queryResolver) Todos(ctx context.Context) ([]*Todo, error) {
if userID := ctx.Value(CtxUserIDKey); userID != "" {
userID := userID.(string)
todos := []*Todo{}
if err := r.DB.Where("user_id = ?", userID).Preload("Notes").Preload("Labels").Find(&todos).Error; err != nil {
return nil, err
}
return todos, nil
}
return nil, errors.New(MsgNotAuthenticated)
}
func (r *queryResolver) Labels(ctx context.Context) ([]*Label, error) {
if userID := ctx.Value(CtxUserIDKey); userID != "" {
userID := userID.(string)
labels := []*Label{}
if err := r.DB.Where("user_id = ?", userID).Preload("Todos").Find(&labels).Error; err != nil {
return nil, err
}
return labels, nil
}
return nil, errors.New(MsgNotAuthenticated)
}
func (r *queryResolver) User(ctx context.Context) (*User, error) {
if userID := ctx.Value(CtxUserIDKey); userID != "" {
userID := userID.(string)
user := User{
ID: userID,
}
if err := r.DB.First(&user).Error; err != nil {
return nil, err
}
return &user, nil
}
return nil, errors.New(MsgNotAuthenticated)
}
type subscriptionResolver struct{ *Resolver }
func (r *subscriptionResolver) TodoStream(ctx context.Context) (<-chan *TodoAction, error) {
if userID := ctx.Value(CtxUserIDKey); userID != "" {
userID := userID.(string)
todoAction := make(chan *TodoAction, 1)
callbackCreateID, _ := gonanoid.New(6)
callbackUpdateID, _ := gonanoid.New(6)
callbackDeleteID, _ := gonanoid.New(6)
r.DB.Callback().Create().Register(callbackCreateID, func(scope *gorm.Scope) {
createdTodo, ok := scope.Value.(*Todo)
if ok && scope.TableName() == "todos" && createdTodo.UserID == userID {
todoAction <- &TodoAction{
Action: ActionCreated,
Todo: createdTodo,
}
}
})
r.DB.Callback().Update().Register(callbackUpdateID, func(scope *gorm.Scope) {
updatedTodo, ok := scope.Value.(*Todo)
if ok && scope.TableName() == "todos" && updatedTodo.UserID == userID {
todoAction <- &TodoAction{
Action: ActionUpdated,
Todo: updatedTodo,
}
}
})
r.DB.Callback().Delete().Register(callbackDeleteID, func(scope *gorm.Scope) {
deletedTodo, ok := scope.Value.(Todo)
if ok && scope.TableName() == "todos" && deletedTodo.UserID == userID {
todoAction <- &TodoAction{
Action: ActionDeleted,
Todo: &deletedTodo,
}
}
})
go func() {
<-ctx.Done()
r.DB.Callback().Create().Remove(callbackCreateID)
r.DB.Callback().Update().Remove(callbackUpdateID)
r.DB.Callback().Delete().Remove(callbackDeleteID)
}()
return todoAction, nil
}
return nil, errors.New(MsgNotAuthenticated)
}
func (r *subscriptionResolver) LabelStream(ctx context.Context) (<-chan *LabelAction, error) {
if userID := ctx.Value(CtxUserIDKey); userID != "" {
userID := userID.(string)
labelAction := make(chan *LabelAction, 1)
callbackCreateID, _ := gonanoid.New(6)
callbackUpdateID, _ := gonanoid.New(6)
r.DB.Callback().Create().Register(callbackCreateID, func(scope *gorm.Scope) {
createdLabel, ok := scope.Value.(*Label)
if ok && scope.TableName() == "labels" && createdLabel.UserID == userID {
labelAction <- &LabelAction{
Action: ActionCreated,
Label: createdLabel,
}
}
})
r.DB.Callback().Update().Register(callbackUpdateID, func(scope *gorm.Scope) {
updatedLabel, ok := scope.Value.(*Label)
if ok && scope.TableName() == "labels" && updatedLabel.UserID == userID {
labelAction <- &LabelAction{
Action: ActionUpdated,
Label: updatedLabel,
}
}
})
go func() {
<-ctx.Done()
r.DB.Callback().Create().Remove(callbackCreateID)
r.DB.Callback().Update().Remove(callbackUpdateID)
}()
return labelAction, nil
}
return nil, errors.New(MsgNotAuthenticated)
}