-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment.resolvers.go
341 lines (278 loc) · 8.49 KB
/
comment.resolvers.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
package graph
// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
// Code generated by github.com/99designs/gqlgen version v0.17.36
import (
"context"
"errors"
"fmt"
"time"
"github.com/dailytravel/x/community/graph/model"
"github.com/dailytravel/x/community/internal/utils"
"github.com/dailytravel/x/community/pkg/auth"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// ID is the resolver for the id field.
func (r *commentResolver) ID(ctx context.Context, obj *model.Comment) (string, error) {
return obj.ID.Hex(), nil
}
// UID is the resolver for the uid field.
func (r *commentResolver) UID(ctx context.Context, obj *model.Comment) (*string, error) {
if obj.UID == nil {
return nil, nil
}
uid := obj.UID.Hex()
return &uid, nil
}
// Body is the resolver for the body field.
func (r *commentResolver) Body(ctx context.Context, obj *model.Comment) (string, error) {
locale := auth.Locale(ctx)
if body, ok := obj.Body[*locale].(string); ok {
return body, nil
}
return obj.Body[obj.Locale].(string), nil
}
// Metadata is the resolver for the metadata field.
func (r *commentResolver) Metadata(ctx context.Context, obj *model.Comment) (map[string]interface{}, error) {
return obj.Metadata, nil
}
// Parent is the resolver for the parent field.
func (r *commentResolver) Parent(ctx context.Context, obj *model.Comment) (*model.Comment, error) {
var item *model.Comment
filter := bson.M{"parent": obj.Parent}
options := options.FindOne().SetProjection(bson.M{"_id": 1, "name": 1, "description": 1})
err := r.db.Collection(item.Collection()).FindOne(ctx, filter, options).Decode(&item)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return nil, nil
}
return nil, err
}
return item, nil
}
// Children is the resolver for the children field.
func (r *commentResolver) Children(ctx context.Context, obj *model.Comment) ([]*model.Comment, error) {
var items []*model.Comment
filter := bson.M{"parent": obj.ID}
options := options.Find().SetProjection(bson.M{"_id": 1, "name": 1, "description": 1})
cursor, err := r.db.Collection(obj.Collection()).Find(ctx, filter, options)
if err != nil {
return nil, err
}
if err := cursor.All(ctx, &items); err != nil {
return nil, err
}
return items, nil
}
// Object is the resolver for the object field.
func (r *commentResolver) Object(ctx context.Context, obj *model.Comment) (map[string]interface{}, error) {
panic(fmt.Errorf("not implemented: Object - object"))
}
// Reactions is the resolver for the reactions field.
func (r *commentResolver) Reactions(ctx context.Context, obj *model.Comment) ([]*model.Reaction, error) {
var items []*model.Reaction
filter := bson.M{"reactable._id": obj.ID, "reactable.type": obj.Collection()}
options := options.Find().SetProjection(bson.M{"_id": 1, "name": 1, "description": 1})
cursor, err := r.db.Collection("reactions").Find(ctx, filter, options)
if err != nil {
return nil, err
}
if err := cursor.All(ctx, &items); err != nil {
return nil, err
}
return items, nil
}
// Created is the resolver for the created field.
func (r *commentResolver) Created(ctx context.Context, obj *model.Comment) (string, error) {
return time.Unix(int64(obj.Created.T), 0).Format(time.RFC3339), nil
}
// Updated is the resolver for the updated field.
func (r *commentResolver) Updated(ctx context.Context, obj *model.Comment) (string, error) {
return time.Unix(int64(obj.Updated.T), 0).Format(time.RFC3339), nil
}
// Responded is the resolver for the responded field.
func (r *commentResolver) Responded(ctx context.Context, obj *model.Comment) (*string, error) {
panic(fmt.Errorf("not implemented: Responded - responded"))
}
// CreateComment is the resolver for the createComment field.
func (r *mutationResolver) CreateComment(ctx context.Context, input model.NewComment) (*model.Comment, error) {
uid, err := utils.UID(ctx)
if err != nil {
return nil, err
}
_id, err := primitive.ObjectIDFromHex(input.Object["id"].(string))
if err != nil {
return nil, err
}
item := &model.Comment{
UID: uid,
Locale: input.Locale,
Object: model.Object{
ID: _id,
Type: input.Object["type"].(string),
},
}
if input.Parent != nil {
parent, err := primitive.ObjectIDFromHex(*input.Parent)
if err != nil {
return nil, err
}
item.Parent = &parent
}
if input.Body != nil {
item.Body[input.Locale] = input.Body
}
res, err := r.db.Collection(item.Collection()).InsertOne(ctx, item, nil)
if err != nil {
return nil, err
}
item.ID = res.InsertedID.(primitive.ObjectID)
return item, nil
}
// UpdateComment is the resolver for the updateComment field.
func (r *mutationResolver) UpdateComment(ctx context.Context, id string, input model.UpdateComment) (*model.Comment, error) {
_id, err := primitive.ObjectIDFromHex(id)
if err != nil {
return nil, err
}
filter := bson.M{"_id": _id}
item := &model.Comment{}
err = r.db.Collection(item.Collection()).FindOne(ctx, filter).Decode(&item)
if err != nil {
return nil, err
}
if input.Body != nil {
item.Body[input.Locale] = input.Body
}
if input.Metadata != nil {
for k, v := range input.Metadata {
item.Metadata[k] = v
}
}
if input.Status != nil {
item.Status = *input.Status
}
// Update the contact
if _, err := r.db.Collection(item.Collection()).UpdateOne(ctx, filter, bson.M{"$set": item}, nil); err != nil {
return nil, err
}
return item, nil
}
// DeleteComment is the resolver for the deleteComment field.
func (r *mutationResolver) DeleteComment(ctx context.Context, id string) (map[string]interface{}, error) {
var item *model.Comment
uid, err := utils.UID(ctx)
if err != nil {
return nil, err
}
_id, err := primitive.ObjectIDFromHex(id)
if err != nil {
return nil, err
}
// Find the comment by _id and delete it
update := bson.M{
"$set": bson.M{
"deleted_at": time.Now(),
"updated_by": uid,
},
}
result := r.db.Collection("comments").FindOneAndUpdate(
ctx,
bson.M{"_id": _id},
update,
)
err = result.Decode(&item)
if err != nil {
return nil, err
}
return map[string]interface{}{
"success": true,
}, nil
}
// DeleteComments is the resolver for the deleteComments field.
func (r *mutationResolver) DeleteComments(ctx context.Context, ids []string) (map[string]interface{}, error) {
uid, err := utils.UID(ctx)
if err != nil {
return nil, err
}
var deletedCount int64
// Convert string IDs to ObjectIDs
objectIDs := make([]primitive.ObjectID, len(ids))
for i, id := range ids {
_id, err := primitive.ObjectIDFromHex(id)
if err != nil {
return nil, err
}
objectIDs[i] = _id
}
// Update all comments with the given IDs
update := bson.M{
"$set": bson.M{
"deleted_at": time.Now(),
"updated_by": uid,
},
}
result, err := r.db.Collection("comments").UpdateMany(
ctx,
bson.M{"_id": bson.M{"$in": objectIDs}},
update,
)
if err != nil {
return nil, err
}
if result.MatchedCount > 0 {
deletedCount = result.ModifiedCount
}
return map[string]interface{}{
"deletedCount": deletedCount,
}, nil
}
// Comments is the resolver for the comments field.
func (r *queryResolver) Comments(ctx context.Context, stages map[string]interface{}) (*model.Comments, error) {
var items []*model.Comment
//find all items
cur, err := r.db.Collection("comments").Find(ctx, nil)
if err != nil {
return nil, err
}
for cur.Next(ctx) {
var item *model.Comment
if err := cur.Decode(&item); err != nil {
return nil, err
}
items = append(items, item)
}
//get total count
count, err := r.db.Collection("comments").CountDocuments(ctx, nil)
if err != nil {
return nil, err
}
return &model.Comments{
Count: int(count),
Data: items,
}, nil
}
// Comment is the resolver for the comment field.
func (r *queryResolver) Comment(ctx context.Context, id string) (*model.Comment, error) {
var item *model.Comment
col := r.db.Collection(item.Collection())
_id, err := primitive.ObjectIDFromHex(id)
if err != nil {
return nil, err
}
filter := bson.M{"_id": _id}
err = col.FindOne(ctx, filter).Decode(&item)
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return nil, fmt.Errorf("no document found for filter %v", filter)
}
return nil, err
}
return item, nil
}
// Comment returns CommentResolver implementation.
func (r *Resolver) Comment() CommentResolver { return &commentResolver{r} }
type commentResolver struct{ *Resolver }