forked from romshark/dgraph_graphql_go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mEditPost.go
70 lines (64 loc) · 1.52 KB
/
mEditPost.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
package resolver
import (
"context"
"github.com/romshark/dgraph_graphql_go/api/graph/auth"
"github.com/romshark/dgraph_graphql_go/store"
strerr "github.com/romshark/dgraph_graphql_go/store/errors"
)
// EditPost resolves Mutation.editPost
func (rsv *Resolver) EditPost(
ctx context.Context,
params struct {
Post string
Editor string
NewTitle *string
NewContents *string
},
) *Post {
if err := auth.Authorize(ctx, auth.IsOwner{
Owner: store.ID(params.Editor),
}); err != nil {
rsv.error(ctx, err)
return nil
}
// Validate input
if params.NewTitle == nil && params.NewContents == nil {
err := strerr.New(strerr.ErrInvalidInput, "no changes")
rsv.error(ctx, err)
return nil
}
if params.NewTitle != nil {
if err := rsv.validator.PostTitle(*params.NewTitle); err != nil {
err = strerr.Wrap(strerr.ErrInvalidInput, err)
rsv.error(ctx, err)
return nil
}
}
if params.NewContents != nil {
if err := rsv.validator.PostContents(*params.NewContents); err != nil {
err = strerr.Wrap(strerr.ErrInvalidInput, err)
rsv.error(ctx, err)
return nil
}
}
mutatedPost, _, err := rsv.str.EditPost(
ctx,
store.ID(params.Post),
store.ID(params.Editor),
params.NewTitle,
params.NewContents,
)
if err != nil {
rsv.error(ctx, err)
return nil
}
return &Post{
root: rsv,
uid: mutatedPost.UID,
id: store.ID(params.Post),
creation: mutatedPost.Creation,
title: mutatedPost.Title,
contents: mutatedPost.Contents,
authorUID: mutatedPost.Author.UID,
}
}