-
Notifications
You must be signed in to change notification settings - Fork 3
/
resolver.go
370 lines (305 loc) · 9.15 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package drophere_go
//go:generate go run github.com/99designs/gqlgen
import (
"context"
"errors"
"time"
"github.com/bccfilkom/drophere-go/domain"
) // THIS CODE IS A STARTING POINT ONLY. IT WILL NOT BE UPDATED WITH SCHEMA CHANGES.
var (
errUnauthenticated = errors.New("Access denied")
errUnauthorized = errors.New("You are not allowed to do this operation")
)
type authenticator interface {
GetAuthenticatedUser(context.Context) *domain.User
}
// Resolver resolves given query from client
type Resolver struct {
linkSvc domain.LinkService
userSvc domain.UserService
authenticator authenticator
}
// NewResolver func
func NewResolver(
userSvc domain.UserService,
authenticator authenticator,
linkSvc domain.LinkService,
) *Resolver {
return &Resolver{
linkSvc: linkSvc,
userSvc: userSvc,
authenticator: authenticator,
}
}
// Mutation returns a group of resolvers for mutation query
func (r *Resolver) Mutation() MutationResolver {
return &mutationResolver{r}
}
// Query returns a group of resolvers for query
func (r *Resolver) Query() QueryResolver {
return &queryResolver{r}
}
type mutationResolver struct{ *Resolver }
// Register resolver
func (r *mutationResolver) Register(ctx context.Context, email string, password string, name string) (*Token, error) {
user, err := r.userSvc.Register(email, name, password)
if err != nil {
return nil, err
}
userCreds, err := r.userSvc.Auth(user.Email, password)
if err != nil {
return nil, err
}
return &Token{LoginToken: userCreds.Token}, nil
}
// Login resolver
func (r *mutationResolver) Login(ctx context.Context, email string, password string) (*Token, error) {
userCreds, err := r.userSvc.Auth(email, password)
if err != nil {
return nil, err
}
return &Token{LoginToken: userCreds.Token}, nil
}
// RequestPasswordRecovery resolver
func (r *mutationResolver) RequestPasswordRecovery(ctx context.Context, email string) (*Message, error) {
err := r.userSvc.RequestPasswordRecovery(email)
if err != nil {
return nil, err
}
return &Message{"Recover Password instruction has been sent to your email"}, nil
}
// RecoverPassword resolver
func (r *mutationResolver) RecoverPassword(ctx context.Context, email, recoverToken, newPassword string) (*Token, error) {
err := r.userSvc.RecoverPassword(email, recoverToken, newPassword)
if err != nil {
return nil, err
}
userCreds, err := r.userSvc.Auth(email, newPassword)
if err != nil {
return nil, err
}
return &Token{LoginToken: userCreds.Token}, nil
}
// UpdatePassword resolver
func (r *mutationResolver) UpdatePassword(ctx context.Context, oldPassword string, newPassword string) (*Message, error) {
user := r.authenticator.GetAuthenticatedUser(ctx)
if user == nil {
return nil, errUnauthenticated
}
_, err := r.userSvc.Update(user.ID, nil, &newPassword, &oldPassword)
if err != nil {
return nil, err
}
return &Message{Message: "You password successfully updated"}, nil
}
// UpdateProfile resolver
func (r *mutationResolver) UpdateProfile(ctx context.Context, newName string) (*Message, error) {
user := r.authenticator.GetAuthenticatedUser(ctx)
if user == nil {
return nil, errUnauthenticated
}
_, err := r.userSvc.Update(user.ID, &newName, nil, nil)
if err != nil {
return nil, err
}
return &Message{Message: "Your profile successfully updated"}, nil
}
// CreateLink resolver
func (r *mutationResolver) CreateLink(ctx context.Context, title string, slug string, description *string, deadline *time.Time, password *string, providerID *int) (*Link, error) {
user := r.authenticator.GetAuthenticatedUser(ctx)
if user == nil {
return nil, errUnauthenticated
}
desc := ""
if description != nil {
desc = *description
}
var providerIDUintPtr *uint
if providerID != nil {
providerIDUint := uint(*providerID)
providerIDUintPtr = &providerIDUint
}
l, err := r.linkSvc.CreateLink(title, slug, desc, deadline, password, user, providerIDUintPtr)
if err != nil {
return nil, err
}
return formatLink(*l), nil
}
// UpdateLink resolver
func (r *mutationResolver) UpdateLink(ctx context.Context, linkID int, title string, slug string, description *string, deadline *time.Time, password *string, providerID *int) (*Link, error) {
user := r.authenticator.GetAuthenticatedUser(ctx)
if user == nil {
return nil, errUnauthenticated
}
l, err := r.linkSvc.FetchLink(uint(linkID))
if err != nil {
return nil, err
}
if l.UserID != user.ID {
return nil, errUnauthorized
}
var providerIDUintPtr *uint
if providerID != nil {
providerIDUint := uint(*providerID)
providerIDUintPtr = &providerIDUint
}
l, err = r.linkSvc.UpdateLink(
uint(linkID),
title,
slug,
description,
deadline,
password,
providerIDUintPtr,
)
if err != nil {
return nil, err
}
return formatLink(*l), nil
}
// DeleteLink resolver
func (r *mutationResolver) DeleteLink(ctx context.Context, linkID int) (*Message, error) {
user := r.authenticator.GetAuthenticatedUser(ctx)
if user == nil {
return nil, errUnauthenticated
}
l, err := r.linkSvc.FetchLink(uint(linkID))
if err != nil {
return nil, err
}
if l.UserID != user.ID {
return nil, errUnauthorized
}
err = r.linkSvc.DeleteLink(uint(linkID))
if err != nil {
return nil, err
}
return &Message{Message: "Link Deleted!"}, nil
}
// CheckLinkPassword resolver
func (r *mutationResolver) CheckLinkPassword(ctx context.Context, linkID int, password string) (*Message, error) {
// this is for public use, no need to check user auth
l, err := r.linkSvc.FetchLink(uint(linkID))
if err != nil {
return nil, err
}
msg := "Invalid Password"
if r.linkSvc.CheckLinkPassword(l, password) {
msg = "Valid Password"
}
return &Message{Message: msg}, nil
}
// ConnectStorageProvider resolver
func (r *mutationResolver) ConnectStorageProvider(ctx context.Context, providerID int, providerToken string) (*Message, error) {
user := r.authenticator.GetAuthenticatedUser(ctx)
if user == nil {
return nil, errUnauthenticated
}
err := r.userSvc.ConnectStorageProvider(user.ID, uint(providerID), providerToken)
if err != nil {
return nil, err
}
return &Message{Message: "Storage Provider successfully connected"}, nil
}
// DisconnectStorageProvider resolver
func (r *mutationResolver) DisconnectStorageProvider(ctx context.Context, providerID int) (*Message, error) {
user := r.authenticator.GetAuthenticatedUser(ctx)
if user == nil {
return nil, errUnauthenticated
}
err := r.userSvc.DisconnectStorageProvider(user.ID, uint(providerID))
if err != nil {
return nil, err
}
return &Message{Message: "Storage Provider disconnected"}, nil
}
type queryResolver struct{ *Resolver }
// Links resolver
func (r *queryResolver) Links(ctx context.Context) ([]*Link, error) {
user := r.authenticator.GetAuthenticatedUser(ctx)
if user == nil {
return nil, errUnauthenticated
}
links, err := r.linkSvc.ListLinks(user.ID)
if err != nil {
return nil, err
}
return formatLinks(links), nil
}
// Me resolver
func (r *queryResolver) Me(ctx context.Context) (*User, error) {
user := r.authenticator.GetAuthenticatedUser(ctx)
if user == nil {
return nil, errUnauthenticated
}
uscs, err := r.userSvc.ListStorageProviders(user.ID)
if err != nil {
return nil, err
}
// map from domain.UserStorageProviderCredential to StorageProvider
storageProviders := make([]*StorageProvider, len(uscs))
for i, usc := range uscs {
storageProviders[i] = &StorageProvider{
ID: int(usc.ID),
ProviderID: int(usc.ProviderID),
Email: usc.Email,
Photo: usc.Photo,
}
}
return &User{
ID: int(user.ID),
Email: user.Email,
Name: user.Name,
ConnectedStorageProviders: storageProviders,
}, nil
}
// Link resolver
func (r *queryResolver) Link(ctx context.Context, slug string) (*Link, error) {
// this is for public use, no need to check user auth
link, err := r.linkSvc.FindLinkBySlug(slug)
if err != nil {
return nil, err
}
return formatLink(*link), nil
}
func formatLink(link domain.Link) *Link {
formattedLink := &Link{
ID: int(link.ID),
Title: link.Title,
IsProtected: link.IsProtected(),
Slug: &link.Slug,
Description: &link.Description,
Deadline: link.Deadline,
}
if link.UserStorageCredential != nil {
formattedLink.StorageProvider = &StorageProvider{
ID: int(link.UserStorageCredential.ID),
ProviderID: int(link.UserStorageCredential.ProviderID),
Email: link.UserStorageCredential.Email,
Photo: link.UserStorageCredential.Photo,
}
}
return formattedLink
}
func formatLinks(links []domain.Link) []*Link {
formattedLinks := make([]*Link, len(links))
for i, link := range links {
formattedLinks[i] = &Link{
ID: int(link.ID),
Title: link.Title,
IsProtected: link.IsProtected(),
Slug: &links[i].Slug,
Description: &links[i].Description,
Deadline: link.Deadline,
}
if link.UserStorageCredential != nil {
formattedLinks[i].StorageProvider = &StorageProvider{
ID: int(link.UserStorageCredential.ID),
ProviderID: int(link.UserStorageCredential.ProviderID),
Email: link.UserStorageCredential.Email,
Photo: link.UserStorageCredential.Photo,
}
}
}
return formattedLinks
}