-
Notifications
You must be signed in to change notification settings - Fork 8
/
comment_mutations.go
72 lines (63 loc) · 1.68 KB
/
comment_mutations.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
package schema
import (
"context"
"errors"
"github.com/firstcontributions/backend/internal/gateway/session"
"github.com/firstcontributions/backend/internal/storemanager"
"github.com/firstcontributions/backend/pkg/authorizer"
)
func (m *Resolver) CreateComment(
ctx context.Context,
args struct {
Comment *CreateCommentInput
},
) (*Comment, error) {
session := session.FromContext(ctx)
if session == nil {
return nil, errors.New("unauthorized")
}
commentModelInput, err := args.Comment.ToModel()
if err != nil {
return nil, err
}
commentModelInput.CreatedBy = session.UserID()
ownership := &authorizer.Scope{
Users: []string{session.UserID()},
}
comment, err := storemanager.FromContext(ctx).StoriesStore.CreateComment(ctx, commentModelInput, ownership)
if err != nil {
return nil, err
}
return NewComment(comment), nil
}
func (m *Resolver) UpdateComment(
ctx context.Context,
args struct {
Comment *UpdateCommentInput
},
) (*Comment, error) {
session := session.FromContext(ctx)
if session == nil {
return nil, errors.New("unauthorized")
}
store := storemanager.FromContext(ctx)
id, err := ParseGraphqlID(args.Comment.ID)
if err != nil {
return nil, err
}
comment, err := store.StoriesStore.GetCommentByID(ctx, id.ID)
if err != nil {
return nil, err
}
if !authorizer.IsAuthorized(session.Permissions, comment.Ownership, authorizer.Comment, authorizer.OperationUpdate) {
return nil, errors.New("forbidden")
}
if err := store.StoriesStore.UpdateComment(ctx, id.ID, args.Comment.ToModel()); err != nil {
return nil, err
}
comment, err = store.StoriesStore.GetCommentByID(ctx, id.ID)
if err != nil {
return nil, err
}
return NewComment(comment), nil
}