This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
/
tCreatePost.go
144 lines (130 loc) · 2.68 KB
/
tCreatePost.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
package dgraph
import (
"context"
"encoding/json"
"time"
"github.com/dgraph-io/dgo/protos/api"
"github.com/pkg/errors"
"github.com/romshark/dgraph_graphql_go/store"
strerr "github.com/romshark/dgraph_graphql_go/store/errors"
)
// CreatePost creates a new post
func (str *impl) CreatePost(
ctx context.Context,
creationTime time.Time,
authorID store.ID,
title string,
contents string,
) (
result store.Post,
err error,
) {
result.Title = title
result.Contents = contents
result.Creation = creationTime
// Prepare
result.ID = store.NewID()
// Begin transaction
txn, close := str.txn(&err)
if err != nil {
return
}
defer close()
// Ensure author exists
var qr struct {
ByID []UID `json:"byId"`
Author []UID `json:"author"`
}
err = txn.QueryVars(
ctx,
`query User(
$id: string,
$authorId: string
) {
byId(func: eq(Post.id, $id)) { uid }
author(func: eq(User.id, $authorId)) { uid }
}`,
map[string]string{
"$id": string(result.ID),
"$authorId": string(authorID),
},
&qr,
)
if err != nil {
return
}
if len(qr.ByID) > 0 {
err = errors.Errorf("duplicate Post.id: %s", result.ID)
return
}
if len(qr.Author) < 1 {
err = strerr.Newf(
strerr.ErrInvalidInput,
"author not found",
)
return
}
result.Author = &store.User{
GraphNode: store.GraphNode{
UID: qr.Author[0].NodeID,
},
}
// Create new post
var newPostJSON []byte
newPostJSON, err = json.Marshal(struct {
ID string `json:"Post.id"`
Author UID `json:"Post.author"`
Title string `json:"Post.title"`
Contents string `json:"Post.contents"`
Creation time.Time `json:"Post.creation"`
}{
Author: UID{NodeID: result.Author.UID},
ID: string(result.ID),
Title: title,
Contents: contents,
Creation: creationTime,
})
if err != nil {
return
}
var postCreationMut map[string]string
postCreationMut, err = txn.Mutation(ctx, &api.Mutation{
SetJson: newPostJSON,
})
if err != nil {
return
}
result.UID = postCreationMut["blank-0"]
// Update author (User.posts -> new post)
var updatedAuthorJSON []byte
updatedAuthorJSON, err = json.Marshal(struct {
UID string `json:"uid"`
Posts UID `json:"User.posts"`
}{
UID: result.Author.UID,
Posts: UID{NodeID: result.UID},
})
if err != nil {
return
}
_, err = txn.Mutation(ctx, &api.Mutation{
SetJson: updatedAuthorJSON,
})
if err != nil {
return
}
// Add the new post to the global Index
var newPostsIndexJSON []byte
newPostsIndexJSON, err = json.Marshal(struct {
UID UID `json:"posts"`
}{
UID: UID{NodeID: result.UID},
})
if err != nil {
return
}
_, err = txn.Mutation(ctx, &api.Mutation{
SetJson: newPostsIndexJSON,
})
return
}