-
Notifications
You must be signed in to change notification settings - Fork 16
/
db_logic.go
342 lines (312 loc) · 8.75 KB
/
db_logic.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
342
package main
import (
"context"
"fmt"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Queries
// getAllRecipes
func (db mongoDB) getAllRecipes() (interface{}, error) {
var results []RecipeModel
var err error
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
cur, err := db.recipes.Find(ctx, bson.D{}, options.Find())
if err != nil {
return nil, err
}
for cur.Next(ctx) {
var elem RecipeModel
err = cur.Decode(&elem)
if err != nil {
return nil, err
}
results = append(results, elem)
}
if err = cur.Err(); err != nil {
return nil, err
}
cur.Close(ctx)
return results, nil
}
// getRecipe
func (db mongoDB) getRecipe(_id string) (interface{}, error) {
var results RecipeModel
var err error
id, err := primitive.ObjectIDFromHex(_id)
if err != nil {
return nil, err
}
q := bson.M{"_id": id}
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
err = db.recipes.FindOne(ctx, q).Decode(&results)
if err != nil {
return nil, err
}
return results, nil
}
// searchRecipes
func (db mongoDB) searchRecipes(searchTerm string) (interface{}, error) {
var err error
var results []RecipeModel
q := bson.M{"$text": bson.M{"$search": searchTerm}}
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
cur, err := db.recipes.Find(ctx, q, options.Find())
if err != nil {
return nil, err
}
for cur.Next(ctx) {
var elem RecipeModel
err := cur.Decode(&elem)
if err != nil {
return nil, err
}
results = append(results, elem)
}
if err := cur.Err(); err != nil {
return nil, err
}
cur.Close(ctx)
return results, nil
}
// getCurrentUser
func (db mongoDB) getCurrentUser(username string) (interface{}, error) {
var err error
var results UserModel
q := bson.M{"username": username}
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
err = db.users.FindOne(ctx, q).Decode(&results)
if err != nil {
return nil, err
}
var favorites []RecipeModel
for e, _ := range results.Favorites {
var recipe RecipeModel
_id := results.Favorites[e].(primitive.ObjectID)
q := bson.M{"_id": _id}
if c, _ := db.recipes.CountDocuments(context.Background(), q); c > 0 {
ctx, _ = context.WithTimeout(context.Background(), 30*time.Second)
err := db.recipes.FindOne(ctx, q).Decode(&recipe)
if err != nil {
return nil, err
}
favorites = append(favorites, recipe)
}
}
favi := make([]interface{}, len(favorites))
for i, fav := range favorites {
favi[i] = fav
}
results.Favorites = favi
return results, nil
}
// getUserRecipes
func (db mongoDB) getUserRecipes(username string) (interface{}, error) {
var err error
var results []RecipeModel
q := bson.M{"username": username}
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
cur, err := db.recipes.Find(ctx, q, options.Find())
if err != nil {
return nil, err
}
for cur.Next(ctx) {
var elem RecipeModel
err := cur.Decode(&elem)
if err != nil {
return nil, err
}
results = append(results, elem)
}
if err := cur.Err(); err != nil {
return nil, err
}
cur.Close(ctx)
return results, nil
}
// Mutations
// addRecipe
func (db mongoDB) addRecipe(username string, name string, imageUrl string, category string, description string, instructions string) (interface{}, error) {
var err error
var results RecipeModel
results.ID = primitive.NewObjectID()
results.Name = name
results.ImageUrl = imageUrl
results.Category = category
results.Description = description
results.Instructions = instructions
results.CreatedDate = time.Now()
results.Username = username
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
_, err = db.recipes.InsertOne(ctx, results)
if err != nil {
return nil, err
}
return results, nil
}
// likeRecipe
func (db mongoDB) likeRecipe(_id string, username string) (interface{}, error) {
var err error
var results RecipeModel
id, err := primitive.ObjectIDFromHex(_id)
if err != nil {
return nil, err
}
q := bson.M{"_id": id}
q2 := bson.M{"$inc": bson.M{"likes": 1}}
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
err = db.recipes.FindOneAndUpdate(ctx, q, q2).Decode(&results)
if err != nil {
return nil, err
}
//results.Likes++
q = bson.M{"username": username}
q2 = bson.M{"$addToSet": bson.M{"favorites": id}}
ctx, _ = context.WithTimeout(context.Background(), 30*time.Second)
err = db.users.FindOneAndUpdate(ctx, q, q2).Err()
if err != nil {
return nil, err
}
return results, nil
}
// unlikeRecipe
func (db mongoDB) unlikeRecipe(_id string, username string) (interface{}, error) {
var err error
var results RecipeModel
id, err := primitive.ObjectIDFromHex(_id)
if err != nil {
return nil, err
}
q := bson.M{"_id": id}
q2 := bson.M{"$inc": bson.M{"likes": -1}}
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
err = db.recipes.FindOneAndUpdate(ctx, q, q2).Decode(&results)
if err != nil {
return nil, err
}
//results.Likes--
q = bson.M{"username": username}
q2 = bson.M{"$pull": bson.M{"favorites": id}}
ctx, _ = context.WithTimeout(context.Background(), 30*time.Second)
err = db.users.FindOneAndUpdate(ctx, q, q2).Err()
if err != nil {
return nil, err
}
return results, nil
}
// deleteUserRecipe
func (db mongoDB) deleteUserRecipe(_id string, user string) (interface{}, error) {
var err error
var results RecipeModel
id, err := primitive.ObjectIDFromHex(_id)
if err != nil {
return nil, err
}
q := bson.M{"_id": id, "username": user}
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
err = db.recipes.FindOneAndDelete(ctx, q).Decode(&results)
if err != nil {
return nil, err
}
q = bson.M{"favorites": id}
ctx, _ = context.WithTimeout(context.Background(), 30*time.Second)
cur, err := db.users.Find(ctx, q, options.Find())
if err != nil {
return nil, err
}
for cur.Next(ctx) {
var elem UserModel
err := cur.Decode(&elem)
if err != nil {
return nil, err
}
q = bson.M{"username": elem.Username}
q2 := bson.M{"$pull": bson.M{"favorites": id}}
ctx2, _ := context.WithTimeout(context.Background(), 30*time.Second)
err = db.users.FindOneAndUpdate(ctx2, q, q2).Err()
if err != nil {
return nil, err
}
}
if err := cur.Err(); err != nil {
return nil, err
}
cur.Close(ctx)
return results, nil
}
// updateUserRecipe
func (db mongoDB) updateUserRecipe(_id string, user string, name string, imageUrl string, category string, description string, instructions string) (interface{}, error) {
var err error
var results RecipeModel
id, err := primitive.ObjectIDFromHex(_id)
if err != nil {
return nil, err
}
q := bson.M{"_id": id, "username": user}
q2 := bson.M{"$set": bson.M{"name": name,
"imageUrl": imageUrl,
"category": category,
"description": description,
"instructions": instructions,
}}
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
err = db.recipes.FindOneAndUpdate(ctx, q, q2).Decode(&results)
if err != nil {
return nil, err
}
results.Name = name
results.ImageUrl = imageUrl
results.Category = category
results.Description = description
results.Instructions = instructions
return results, nil
}
// signinUser
func (db mongoDB) signinUser(username string, password string) (interface{}, error) {
var err error
var results UserModel
q := bson.M{"username": username}
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
if err = db.users.FindOne(ctx, q).Decode(&results); err != nil {
return nil, fmt.Errorf("User not found.")
}
if err = comparePassword(password, results.Password); err != nil {
return nil, fmt.Errorf("Invalid password.")
}
results.Token, err = createToken(results.Username, results.Email)
return results, nil
}
// signupUser
func (db mongoDB) signupUser(username string, password string, email string) (interface{}, error) {
var err error
var results UserModel
q := bson.M{"username": username}
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
if err := db.users.FindOne(ctx, q).Decode(&results); err == nil {
return nil, fmt.Errorf("User already exists.")
}
q = bson.M{"email": email}
ctx, _ = context.WithTimeout(context.Background(), 30*time.Second)
if err := db.users.FindOne(ctx, q).Decode(&results); err == nil {
return nil, fmt.Errorf("Email already exists.")
}
pass, err := generatePassword(password)
if err != nil {
return nil, err
}
results.ID = primitive.NewObjectID()
results.Username = username
results.Password = pass
results.Email = email
results.JoinDate = time.Now()
results.Favorites = make([]interface{}, 0)
ctx, _ = context.WithTimeout(context.Background(), 30*time.Second)
_, err = db.users.InsertOne(ctx, results)
if err != nil {
return nil, err
}
results.Token, err = createToken(results.Username, results.Email)
return results, nil
}