-
Notifications
You must be signed in to change notification settings - Fork 0
/
share.resolvers.go
160 lines (129 loc) · 4.66 KB
/
share.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
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"
"fmt"
"time"
"github.com/dailytravel/x/community/graph/model"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
// CreateShare is the resolver for the createShare field.
func (r *mutationResolver) CreateShare(ctx context.Context, input model.ShareInput) (*model.Share, error) {
panic(fmt.Errorf("not implemented: CreateShare - createShare"))
}
// UpdateShare is the resolver for the updateShare field.
func (r *mutationResolver) UpdateShare(ctx context.Context, id string, input model.ShareUpdateInput) (*model.Share, error) {
panic(fmt.Errorf("not implemented: UpdateShare - updateShare"))
}
// DeleteShare is the resolver for the deleteShare field.
func (r *mutationResolver) DeleteShare(ctx context.Context, id string) (bool, error) {
objID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return false, err
}
// Delete the share from the MongoDB collection
result, err := r.db.Collection("shares").DeleteOne(ctx, bson.M{"_id": objID})
if err != nil {
return false, err
}
// Check if any document was deleted
if result.DeletedCount == 0 {
return false, nil
}
return true, nil
}
// DeleteShares is the resolver for the deleteShares field.
func (r *mutationResolver) DeleteShares(ctx context.Context, ids []string) (bool, error) {
// Convert string IDs to ObjectIDs
objIDs := make([]primitive.ObjectID, 0, len(ids))
for _, id := range ids {
objID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return false, err
}
objIDs = append(objIDs, objID)
}
// Build the filter to match multiple IDs
filter := bson.M{"_id": bson.M{"$in": objIDs}}
// Delete the shares from the MongoDB collection
result, err := r.db.Collection("shares").DeleteMany(ctx, filter)
if err != nil {
return false, err
}
// Check if any documents were deleted
if result.DeletedCount == 0 {
return false, nil
}
return true, nil
}
// Share is the resolver for the share field.
func (r *queryResolver) Share(ctx context.Context, id string) (*model.Share, error) {
// Parse string ID to MongoDB ObjectID
objID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return nil, fmt.Errorf("invalid ID format: %v", err)
}
// Query the MongoDB collection
var share *model.Share
err = r.db.Collection("shares").FindOne(ctx, bson.M{"_id": objID}).Decode(&share)
if err != nil {
if err == mongo.ErrNoDocuments {
return nil, fmt.Errorf("Share with ID %s not found", id)
}
return nil, err
}
return share, nil
}
// Shares is the resolver for the shares field.
func (r *queryResolver) Shares(ctx context.Context, stages map[string]interface{}) (*model.Shares, error) {
pipeline := bson.A{}
// Add additional stages to the pipeline
for key, value := range stages {
stage := bson.D{{Key: key, Value: value}}
pipeline = append(pipeline, stage)
}
cursor, err := r.db.Collection("shares").Aggregate(ctx, pipeline)
if err != nil {
return nil, err
}
defer cursor.Close(ctx)
var items []*model.Share
if err := cursor.All(ctx, &items); err != nil {
return nil, err
}
return &model.Shares{
Count: int(cursor.RemainingBatchLength()),
Data: items,
}, nil
}
// ID is the resolver for the id field.
func (r *shareResolver) ID(ctx context.Context, obj *model.Share) (string, error) {
return obj.ID.Hex(), nil
}
// UID is the resolver for the uid field.
func (r *shareResolver) UID(ctx context.Context, obj *model.Share) (string, error) {
return obj.UID.Hex(), nil
}
// Object is the resolver for the object field.
func (r *shareResolver) Object(ctx context.Context, obj *model.Share) (map[string]interface{}, error) {
panic(fmt.Errorf("not implemented: Object - object"))
}
// Metadata is the resolver for the metadata field.
func (r *shareResolver) Metadata(ctx context.Context, obj *model.Share) (map[string]interface{}, error) {
return obj.Metadata, nil
}
// Created is the resolver for the created field.
func (r *shareResolver) Created(ctx context.Context, obj *model.Share) (string, error) {
return time.Unix(int64(obj.Created.T), 0).Format(time.RFC3339), nil
}
// Updated is the resolver for the updated field.
func (r *shareResolver) Updated(ctx context.Context, obj *model.Share) (string, error) {
return time.Unix(int64(obj.Updated.T), 0).Format(time.RFC3339), nil
}
// Share returns ShareResolver implementation.
func (r *Resolver) Share() ShareResolver { return &shareResolver{r} }
type shareResolver struct{ *Resolver }