1
|
|
|
package controllers |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"fmt" |
5
|
|
|
"github.com/gofiber/fiber/v2" |
6
|
|
|
"github.com/memnix/memnixrest/app/models" |
7
|
|
|
"github.com/memnix/memnixrest/app/queries" |
8
|
|
|
"github.com/memnix/memnixrest/pkg/core" |
9
|
|
|
"github.com/memnix/memnixrest/pkg/database" |
10
|
|
|
"github.com/memnix/memnixrest/pkg/utils" |
11
|
|
|
"net/http" |
12
|
|
|
"strconv" |
13
|
|
|
) |
14
|
|
|
|
15
|
|
|
// GetTodayCard method |
16
|
|
|
// @Description Get next today card |
17
|
|
|
// @Summary gets a card |
18
|
|
|
// @Tags Card |
19
|
|
|
// @Produce json |
20
|
|
|
// @Success 200 {object} models.Card |
21
|
|
|
// @Deprecated |
22
|
|
|
// @Router /v1/cards/today/one [get] |
23
|
|
|
func GetTodayCard(c *fiber.Ctx) error { |
24
|
|
|
res := new(models.ResponseHTTP) |
25
|
|
|
|
26
|
|
|
auth := CheckAuth(c, models.PermUser) // Check auth |
27
|
|
|
if !auth.Success { |
28
|
|
|
return queries.AuthError(c, &auth) |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if res = queries.FetchNextTodayCard(auth.User.ID); !res.Success { |
32
|
|
|
log := models.CreateLog(fmt.Sprintf("Error from %s on GetTodayCard: %s", auth.User.Email, res.Message), models.LogQueryGetError).SetType(models.LogTypeError).AttachIDs(auth.User.ID, 0, 0) |
33
|
|
|
_ = log.SendLog() |
34
|
|
|
return queries.RequestError(c, http.StatusInternalServerError, res.Message) |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
38
|
|
|
Success: true, |
39
|
|
|
Message: "Get today's card", |
40
|
|
|
Data: res.Data, |
41
|
|
|
Count: 1, |
42
|
|
|
}) |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// GetAllTodayCard method |
46
|
|
|
// @Description Get all today card |
47
|
|
|
// @Summary gets a list of card |
48
|
|
|
// @Tags Card |
49
|
|
|
// @Produce json |
50
|
|
|
// @Success 200 {array} models.Card |
51
|
|
|
// @Router /v1/cards/today [get] |
52
|
|
|
func GetAllTodayCard(c *fiber.Ctx) error { |
53
|
|
|
res := new(models.ResponseHTTP) |
54
|
|
|
|
55
|
|
|
auth := CheckAuth(c, models.PermUser) // Check auth |
56
|
|
|
if !auth.Success { |
57
|
|
|
return queries.AuthError(c, &auth) |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if res = queries.FetchTodayCard(auth.User.ID); !res.Success { |
61
|
|
|
log := models.CreateLog(fmt.Sprintf("Error on GetAllTodayCard: %s", res.Message), models.LogQueryGetError).SetType(models.LogTypeError).AttachIDs(auth.User.ID, 0, 0) |
62
|
|
|
_ = log.SendLog() |
63
|
|
|
return queries.RequestError(c, http.StatusInternalServerError, res.Message) |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
67
|
|
|
Success: true, |
68
|
|
|
Message: "Get today's cards", |
69
|
|
|
Data: res.Data, |
70
|
|
|
Count: res.Count, |
71
|
|
|
}) |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// GetTrainingCardsByDeck method |
75
|
|
|
// @Description Get training cards |
76
|
|
|
// @Summary gets a list of cards |
77
|
|
|
// @Tags Card |
78
|
|
|
// @Produce json |
79
|
|
|
// @Success 200 {array} models.Card |
80
|
|
|
// @Router /v1/cards/{deckID}/training [get] |
81
|
|
|
func GetTrainingCardsByDeck(c *fiber.Ctx) error { |
82
|
|
|
res := new(models.ResponseHTTP) |
83
|
|
|
|
84
|
|
|
auth := CheckAuth(c, models.PermUser) // Check auth |
85
|
|
|
if !auth.Success { |
86
|
|
|
return queries.AuthError(c, &auth) |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
deckID := c.Params("deckID") |
90
|
|
|
deckIdInt, _ := strconv.ParseInt(deckID, 10, 32) |
91
|
|
|
|
92
|
|
|
access := queries.CheckAccess(auth.User.ID, uint(deckIdInt), models.AccessStudent) |
93
|
|
|
if !access.Success { |
94
|
|
|
log := models.CreateLog(fmt.Sprintf("Forbidden from %s on deck %d - GetTodayCard: %s", auth.User.Email, deckIdInt, res.Message), models.LogPermissionForbidden).SetType(models.LogTypeWarning).AttachIDs(auth.User.ID, uint(deckIdInt), 0) |
95
|
|
|
_ = log.SendLog() |
96
|
|
|
return queries.RequestError(c, http.StatusForbidden, utils.ErrorForbidden) |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if res = queries.FetchTrainingCards(auth.User.ID, uint(deckIdInt)); !res.Success { |
100
|
|
|
log := models.CreateLog(fmt.Sprintf("Error on GetTrainingCardsByDeck: %s from %s", res.Message, auth.User.Email), models.LogQueryGetError).SetType(models.LogTypeError).AttachIDs(auth.User.ID, uint(deckIdInt), 0) |
101
|
|
|
_ = log.SendLog() |
102
|
|
|
return queries.RequestError(c, http.StatusInternalServerError, res.Message) |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
106
|
|
|
Success: true, |
107
|
|
|
Message: "Get today's card", |
108
|
|
|
Data: res.Data, |
109
|
|
|
Count: res.Count, |
110
|
|
|
}) |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// GetNextCard method |
114
|
|
|
// @Description Get next card |
115
|
|
|
// @Summary gets a card |
116
|
|
|
// @Tags Card |
117
|
|
|
// @Deprecated |
118
|
|
|
// @Produce json |
119
|
|
|
// @Success 200 {object} models.Card |
120
|
|
|
// @Router /v1/cards/next [get] |
121
|
|
|
func GetNextCard(c *fiber.Ctx) error { |
122
|
|
|
|
123
|
|
|
res := new(models.ResponseHTTP) |
124
|
|
|
auth := CheckAuth(c, models.PermUser) // Check auth |
125
|
|
|
if !auth.Success { |
126
|
|
|
return queries.AuthError(c, &auth) |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if res = queries.FetchNextCard(auth.User.ID, 0); !res.Success { |
130
|
|
|
log := models.CreateLog(fmt.Sprintf("Error on GetNextCard: %s from %s", res.Message, auth.User.Email), models.LogQueryGetError).SetType(models.LogTypeError).AttachIDs(auth.User.ID, 0, 0) |
131
|
|
|
_ = log.SendLog() |
132
|
|
|
return queries.RequestError(c, http.StatusInternalServerError, res.Message) |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
136
|
|
|
Success: true, |
137
|
|
|
Message: "Get next card", |
138
|
|
|
Data: res.Data, |
139
|
|
|
Count: 1, |
140
|
|
|
}) |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// GetNextCardByDeck method |
144
|
|
|
// @Description Get next card by deckID |
145
|
|
|
// @Summary get a card |
146
|
|
|
// @Tags Card |
147
|
|
|
// @Produce json |
148
|
|
|
// @Deprecated |
149
|
|
|
// @Success 200 {object} models.Card |
150
|
|
|
// @Router /v1/cards/{deckID}/next [get] |
151
|
|
|
func GetNextCardByDeck(c *fiber.Ctx) error { |
152
|
|
|
|
153
|
|
|
deckID := c.Params("deckID") |
154
|
|
|
deckIDInt, _ := strconv.Atoi(deckID) |
155
|
|
|
res := new(models.ResponseHTTP) |
156
|
|
|
auth := CheckAuth(c, models.PermUser) // Check auth |
157
|
|
|
if !auth.Success { |
158
|
|
|
return queries.AuthError(c, &auth) |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if res = queries.FetchNextCard(auth.User.ID, uint(deckIDInt)); !res.Success { |
162
|
|
|
log := models.CreateLog(fmt.Sprintf("Error on GetNextCardByDeck: %s from %s on deck %d", res.Message, auth.User.Email, deckIDInt), models.LogQueryGetError).SetType(models.LogTypeError).AttachIDs(auth.User.ID, uint(deckIDInt), 0) |
163
|
|
|
_ = log.SendLog() |
164
|
|
|
return queries.RequestError(c, http.StatusInternalServerError, res.Message) |
165
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
169
|
|
|
Success: true, |
170
|
|
|
Message: "Get next card by deck", |
171
|
|
|
Data: res.Data, |
172
|
|
|
Count: 1, |
173
|
|
|
}) |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// GetAllCards method |
177
|
|
|
// @Description Get every card. Shouldn't really be used |
178
|
|
|
// @Summary gets all cards |
179
|
|
|
// @Tags Card |
180
|
|
|
// @Produce json |
181
|
|
|
// @Success 200 {array} models.Card |
182
|
|
|
// @Router /v1/cards/ [get] |
183
|
|
|
func GetAllCards(c *fiber.Ctx) error { |
184
|
|
|
db := database.DBConn // DB Conn |
185
|
|
|
|
186
|
|
|
auth := CheckAuth(c, models.PermAdmin) // Check auth |
187
|
|
|
if !auth.Success { |
188
|
|
|
return queries.AuthError(c, &auth) |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
var cards []models.Card |
192
|
|
|
|
193
|
|
|
if res := db.Joins("Deck").Find(&cards); res.Error != nil { |
194
|
|
|
return queries.RequestError(c, http.StatusInternalServerError, utils.ErrorRequestFailed) |
195
|
|
|
} |
196
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
197
|
|
|
Success: true, |
198
|
|
|
Message: "Get All cards", |
199
|
|
|
Data: cards, |
200
|
|
|
Count: len(cards), |
201
|
|
|
}) |
202
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// GetCardByID method to get a card by id |
206
|
|
|
// @Description Get a card by tech id |
207
|
|
|
// @Summary gets a card |
208
|
|
|
// @Tags Card |
209
|
|
|
// @Produce json |
210
|
|
|
// @Param id path int true "Card ID" |
211
|
|
|
// @Success 200 {object} models.Card |
212
|
|
|
// @Router /v1/cards/id/{id} [get] |
213
|
|
|
func GetCardByID(c *fiber.Ctx) error { |
214
|
|
|
db := database.DBConn // DB Conn |
215
|
|
|
|
216
|
|
|
auth := CheckAuth(c, models.PermAdmin) // Check auth |
217
|
|
|
if !auth.Success { |
218
|
|
|
return queries.AuthError(c, &auth) |
219
|
|
|
} |
220
|
|
|
// Params |
221
|
|
|
id := c.Params("id") |
222
|
|
|
|
223
|
|
|
card := new(models.Card) |
224
|
|
|
|
225
|
|
|
if err := db.Joins("Deck").Joins("Mcq").First(&card, id).Error; err != nil { |
226
|
|
|
return queries.RequestError(c, http.StatusInternalServerError, err.Error()) |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
230
|
|
|
Success: true, |
231
|
|
|
Message: "Success get card by ID.", |
232
|
|
|
Data: *card, |
233
|
|
|
Count: 1, |
234
|
|
|
}) |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
// GetCardsFromDeck method to get cards from deck |
238
|
|
|
// @Description Get every card from a deck |
239
|
|
|
// @Summary gets a list of card |
240
|
|
|
// @Tags Card |
241
|
|
|
// @Produce json |
242
|
|
|
// @Param deckID path int true "Deck ID" |
243
|
|
|
// @Success 200 {array} models.Card |
244
|
|
|
// @Router /v1/cards/deck/{deckID} [get] |
245
|
|
|
func GetCardsFromDeck(c *fiber.Ctx) error { |
246
|
|
|
db := database.DBConn // DB Conn |
247
|
|
|
|
248
|
|
|
// Params |
249
|
|
|
id := c.Params("deckID") |
250
|
|
|
deckID, _ := strconv.ParseUint(id, 10, 32) |
251
|
|
|
|
252
|
|
|
auth := CheckAuth(c, models.PermUser) // Check auth |
253
|
|
|
if !auth.Success { |
254
|
|
|
return queries.AuthError(c, &auth) |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
if res := queries.CheckAccess(auth.User.ID, uint(deckID), models.AccessEditor); !res.Success { |
258
|
|
|
log := models.CreateLog(fmt.Sprintf("Forbidden from %s on deck %d - GetCardsFromDeck: %s", auth.User.Email, deckID, res.Message), models.LogPermissionForbidden).SetType(models.LogTypeWarning).AttachIDs(auth.User.ID, uint(deckID), 0) |
259
|
|
|
_ = log.SendLog() |
260
|
|
|
return queries.RequestError(c, http.StatusForbidden, utils.ErrorForbidden) |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
var cards []models.Card |
264
|
|
|
|
265
|
|
|
if err := db.Joins("Deck").Joins("Mcq").Where("cards.deck_id = ?", id).Find(&cards).Error; err != nil { |
266
|
|
|
log := models.CreateLog(fmt.Sprintf("Error on GetCardsFromDeck: %s from %s on %d", err.Error(), auth.User.Email, deckID), models.LogQueryGetError).SetType(models.LogTypeError).AttachIDs(auth.User.ID, uint(deckID), 0) |
267
|
|
|
_ = log.SendLog() |
268
|
|
|
return queries.RequestError(c, http.StatusInternalServerError, err.Error()) |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
272
|
|
|
Success: true, |
273
|
|
|
Message: "Success get cards from deck.", |
274
|
|
|
Data: cards, |
275
|
|
|
Count: len(cards), |
276
|
|
|
}) |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
// POST |
280
|
|
|
|
281
|
|
|
// CreateNewCard method |
282
|
|
|
// @Description Create a new card |
283
|
|
|
// @Summary creates a card |
284
|
|
|
// @Tags Card |
285
|
|
|
// @Produce json |
286
|
|
|
// @Accept json |
287
|
|
|
// @Param card body models.Card true "Card to create" |
288
|
|
|
// @Success 200 |
289
|
|
|
// @Router /v1/cards/new [post] |
290
|
|
|
func CreateNewCard(c *fiber.Ctx) error { |
291
|
|
|
db := database.DBConn // DB Conn |
292
|
|
|
card := new(models.Card) |
293
|
|
|
|
294
|
|
|
auth := CheckAuth(c, models.PermUser) // Check auth |
295
|
|
|
if !auth.Success { |
296
|
|
|
return queries.AuthError(c, &auth) |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
if err := c.BodyParser(&card); err != nil { |
300
|
|
|
log := models.CreateLog(fmt.Sprintf("Error on CreateNewCard: %s from %s", err.Error(), auth.User.Email), models.LogBodyParserError).SetType(models.LogTypeError).AttachIDs(auth.User.ID, 0, 0) |
301
|
|
|
_ = log.SendLog() |
302
|
|
|
return queries.RequestError(c, http.StatusBadRequest, err.Error()) |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
if res := queries.CheckAccess(auth.User.ID, card.DeckID, models.AccessEditor); !res.Success { |
306
|
|
|
log := models.CreateLog(fmt.Sprintf("Forbidden from %s on deck %d - CreateNewCard: %s", auth.User.Email, card.DeckID, res.Message), models.LogPermissionForbidden).SetType(models.LogTypeWarning).AttachIDs(auth.User.ID, card.DeckID, 0) |
307
|
|
|
_ = log.SendLog() |
308
|
|
|
return queries.RequestError(c, http.StatusForbidden, utils.ErrorForbidden) |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
if res := queries.CheckCardLimit(auth.User.Permissions, card.DeckID); !res { |
312
|
|
|
log := models.CreateLog(fmt.Sprintf("Forbidden from %s on deck %d - CreateNewCard: This deck has reached his limit", auth.User.Email, card.DeckID), models.LogDeckCardLimit).SetType(models.LogTypeWarning).AttachIDs(auth.User.ID, card.DeckID, 0) |
313
|
|
|
_ = log.SendLog() |
314
|
|
|
return queries.RequestError(c, http.StatusForbidden, "This deck has reached his limit ! You can't add more card to it.") |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
if card.NotValidate() { |
318
|
|
|
log := models.CreateLog(fmt.Sprintf("BadRequest from %s on deck %d - CreateNewCard: BadRequest", auth.User.Email, card.DeckID), models.LogBadRequest).SetType(models.LogTypeWarning).AttachIDs(auth.User.ID, card.DeckID, 0) |
319
|
|
|
_ = log.SendLog() |
320
|
|
|
return queries.RequestError(c, http.StatusBadRequest, utils.ErrorQALen) |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
_, ok := card.ValidateMCQ(&auth.User) |
324
|
|
|
if !ok { |
325
|
|
|
return queries.RequestError(c, http.StatusInternalServerError, utils.ErrorRequestFailed) |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
db.Create(card) |
329
|
|
|
|
330
|
|
|
log := models.CreateLog(fmt.Sprintf("Created: %d - %s", card.ID, card.Question), models.LogCardCreated).SetType(models.LogTypeInfo).AttachIDs(auth.User.ID, card.DeckID, card.ID) |
331
|
|
|
_ = log.SendLog() |
332
|
|
|
|
333
|
|
|
if res := queries.UpdateSubUsers(card, &auth.User); res != nil { |
334
|
|
|
return queries.RequestError(c, http.StatusInternalServerError, utils.ErrorRequestFailed) |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
338
|
|
|
Success: true, |
339
|
|
|
Message: "Success register a card", |
340
|
|
|
Data: *card, |
341
|
|
|
Count: 1, |
342
|
|
|
}) |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
// PostResponse method |
346
|
|
|
// @Description Post a response and check it |
347
|
|
|
// @Summary posts a response |
348
|
|
|
// @Tags Card |
349
|
|
|
// @Produce json |
350
|
|
|
// @Success 200 |
351
|
|
|
// @Accept json |
352
|
|
|
// @Router /v1/cards/response [post] |
353
|
|
|
func PostResponse(c *fiber.Ctx) error { |
354
|
|
|
db := database.DBConn // DB Conn |
355
|
|
|
|
356
|
|
|
auth := CheckAuth(c, models.PermUser) // Check auth |
357
|
|
|
if !auth.Success { |
358
|
|
|
return queries.AuthError(c, &auth) |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
response := new(models.CardResponse) |
362
|
|
|
card := new(models.Card) |
363
|
|
|
|
364
|
|
|
if err := c.BodyParser(&response); err != nil { |
365
|
|
|
log := models.CreateLog(fmt.Sprintf("Error on PostResponse: %s from %s", err.Error(), auth.User.Email), models.LogBodyParserError).SetType(models.LogTypeError).AttachIDs(auth.User.ID, 0, 0) |
366
|
|
|
_ = log.SendLog() |
367
|
|
|
return queries.RequestError(c, http.StatusBadRequest, err.Error()) |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
if err := db.Joins("Deck").First(&card, response.CardID).Error; err != nil { |
371
|
|
|
log := models.CreateLog(fmt.Sprintf("Error on PostResponse: %s from %s", err.Error(), auth.User.Email), models.LogQueryGetError).SetType(models.LogTypeError).AttachIDs(auth.User.ID, 0, 0) |
372
|
|
|
_ = log.SendLog() |
373
|
|
|
return queries.RequestError(c, http.StatusServiceUnavailable, err.Error()) |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
res := queries.CheckAccess(auth.User.ID, card.Deck.ID, models.AccessStudent) |
377
|
|
|
if !res.Success { |
378
|
|
|
log := models.CreateLog(fmt.Sprintf("Forbidden from %s on deck %d - PostResponse: %s", auth.User.Email, card.DeckID, res.Message), models.LogPermissionForbidden).SetType(models.LogTypeWarning).AttachIDs(auth.User.ID, card.DeckID, 0) |
379
|
|
|
_ = log.SendLog() |
380
|
|
|
return queries.RequestError(c, http.StatusForbidden, utils.ErrorForbidden) |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
validation := new(models.CardResponseValidation) |
384
|
|
|
|
385
|
|
|
if core.ValidateAnswer(response.Response, card) { |
386
|
|
|
validation.SetCorrect() |
387
|
|
|
} else { |
388
|
|
|
validation.SetIncorrect() |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
//TODO: Add error handling |
392
|
|
|
_ = queries.PostMem(&auth.User, card, validation, response.Training) |
393
|
|
|
|
394
|
|
|
validation.Answer = card.Answer |
395
|
|
|
|
396
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
397
|
|
|
Success: true, |
398
|
|
|
Message: "Success post response", |
399
|
|
|
Data: *validation, |
400
|
|
|
Count: 1, |
401
|
|
|
}) |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
// PUT |
405
|
|
|
|
406
|
|
|
// UpdateCardByID method |
407
|
|
|
// @Description Edit a card |
408
|
|
|
// @Summary edits a card |
409
|
|
|
// @Tags Card |
410
|
|
|
// @Produce json |
411
|
|
|
// @Success 200 |
412
|
|
|
// @Accept json |
413
|
|
|
// @Param card body models.Card true "card to edit" |
414
|
|
|
// @Router /v1/cards/{cardID}/edit [put] |
415
|
|
|
func UpdateCardByID(c *fiber.Ctx) error { |
416
|
|
|
db := database.DBConn // DB Conn |
417
|
|
|
|
418
|
|
|
// Params |
419
|
|
|
id := c.Params("id") |
420
|
|
|
cardID, _ := strconv.ParseUint(id, 10, 32) |
421
|
|
|
|
422
|
|
|
auth := CheckAuth(c, models.PermUser) // Check auth |
423
|
|
|
if !auth.Success { |
424
|
|
|
return queries.AuthError(c, &auth) |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
card := new(models.Card) |
428
|
|
|
|
429
|
|
|
if err := db.First(&card, id).Error; err != nil { |
430
|
|
|
|
431
|
|
|
log := models.CreateLog(fmt.Sprintf("Error on UpdateCardByID: %s from %s", err.Error(), auth.User.Email), models.LogQueryGetError).SetType(models.LogTypeError).AttachIDs(auth.User.ID, 0, uint(cardID)) |
432
|
|
|
_ = log.SendLog() |
433
|
|
|
return queries.RequestError(c, http.StatusInternalServerError, err.Error()) |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
if res := queries.CheckAccess(auth.User.ID, card.DeckID, models.AccessEditor); !res.Success { |
437
|
|
|
log := models.CreateLog(fmt.Sprintf("Forbidden from %s on deck %d - UpdateCardByID: %s", auth.User.Email, card.DeckID, res.Message), models.LogPermissionForbidden).SetType(models.LogTypeWarning).AttachIDs(auth.User.ID, card.DeckID, uint(cardID)) |
438
|
|
|
_ = log.SendLog() |
439
|
|
|
return queries.RequestError(c, http.StatusForbidden, utils.ErrorForbidden) |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
if err := UpdateCard(c, card, &auth.User); !err.Success { |
443
|
|
|
log := models.CreateLog(fmt.Sprintf("Error on UpdateCardByID: %s from %s", err.Message, auth.User.Email), models.LogBadRequest).SetType(models.LogTypeError).AttachIDs(auth.User.ID, 0, uint(cardID)) |
444
|
|
|
_ = log.SendLog() |
445
|
|
|
return queries.RequestError(c, http.StatusBadRequest, err.Message) |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
log := models.CreateLog(fmt.Sprintf("Edited: %d - %s", card.ID, card.Question), models.LogCardEdited).SetType(models.LogTypeInfo).AttachIDs(auth.User.ID, card.DeckID, card.ID) |
449
|
|
|
_ = log.SendLog() |
450
|
|
|
|
451
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
452
|
|
|
Success: true, |
453
|
|
|
Message: "Success update card by ID", |
454
|
|
|
Data: *card, |
455
|
|
|
Count: 1, |
456
|
|
|
}) |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
// UpdateCard function |
460
|
|
|
func UpdateCard(c *fiber.Ctx, card *models.Card, user *models.User) *models.ResponseHTTP { |
461
|
|
|
db := database.DBConn |
462
|
|
|
|
463
|
|
|
deckId := card.DeckID |
464
|
|
|
|
465
|
|
|
res := new(models.ResponseHTTP) |
466
|
|
|
|
467
|
|
|
if err := c.BodyParser(&card); err != nil { |
468
|
|
|
res.GenerateError(err.Error()) |
469
|
|
|
return res |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
if deckId != card.DeckID { |
473
|
|
|
res.GenerateError(utils.ErrorBreak) |
474
|
|
|
return res |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
if card.NotValidate() { |
478
|
|
|
res.GenerateError(utils.ErrorQALen) |
479
|
|
|
return res |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
shouldUpdateMcq := false |
483
|
|
|
mcq := new(models.Mcq) |
484
|
|
|
|
485
|
|
|
mcq, ok := card.ValidateMCQ(user) |
486
|
|
|
if !ok { |
487
|
|
|
res.GenerateError(utils.ErrorRequestFailed) |
488
|
|
|
return res |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
shouldUpdateMcq = mcq != nil |
492
|
|
|
|
493
|
|
|
db.Save(card) |
494
|
|
|
|
495
|
|
|
if shouldUpdateMcq { |
496
|
|
|
mcq.UpdateLinkedAnswers() |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
res.GenerateSuccess("Success update card", nil, 0) |
500
|
|
|
return res |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
// DeleteCardById method |
504
|
|
|
// @Description Delete a card |
505
|
|
|
// @Summary deletes a card |
506
|
|
|
// @Tags Card |
507
|
|
|
// @Produce json |
508
|
|
|
// @Success 200 |
509
|
|
|
// @Router /v1/cards/{cardID} [delete] |
510
|
|
|
func DeleteCardById(c *fiber.Ctx) error { |
511
|
|
|
db := database.DBConn // DB Conn |
512
|
|
|
id := c.Params("id") |
513
|
|
|
cardID, _ := strconv.ParseUint(id, 10, 32) |
514
|
|
|
|
515
|
|
|
auth := CheckAuth(c, models.PermUser) // Check auth |
516
|
|
|
if !auth.Success { |
517
|
|
|
return queries.AuthError(c, &auth) |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
card := new(models.Card) |
521
|
|
|
|
522
|
|
|
if err := db.First(&card, id).Error; err != nil { |
523
|
|
|
return queries.RequestError(c, http.StatusServiceUnavailable, err.Error()) |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
if res := queries.CheckAccess(auth.User.ID, card.DeckID, models.AccessOwner); !res.Success { |
527
|
|
|
log := models.CreateLog(fmt.Sprintf("Forbidden from %s on deck %d - DeleteCardById: %s", auth.User.Email, card.DeckID, res.Message), models.LogPermissionForbidden).SetType(models.LogTypeWarning).AttachIDs(auth.User.ID, card.DeckID, uint(cardID)) |
528
|
|
|
_ = log.SendLog() |
529
|
|
|
return queries.RequestError(c, http.StatusForbidden, utils.ErrorForbidden) |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
var memDates []models.MemDate |
533
|
|
|
|
534
|
|
|
if err := db.Joins("Card").Where("mem_dates.card_id = ?", card.ID).Find(&memDates).Error; err != nil { |
535
|
|
|
log := models.CreateLog(fmt.Sprintf("Error on DeleteCardById: %s from %s", err.Error(), auth.User.Email), models.LogQueryGetError).SetType(models.LogTypeError).AttachIDs(auth.User.ID, 0, uint(cardID)) |
536
|
|
|
_ = log.SendLog() |
537
|
|
|
return queries.RequestError(c, http.StatusInternalServerError, utils.ErrorRequestFailed) |
538
|
|
|
// TODO: Error |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
db.Unscoped().Delete(memDates) |
542
|
|
|
|
543
|
|
|
db.Delete(card) |
544
|
|
|
|
545
|
|
|
log := models.CreateLog(fmt.Sprintf("Deleted: %d - %s", card.ID, card.Question), models.LogCardDeleted).SetType(models.LogTypeInfo).AttachIDs(auth.User.ID, card.DeckID, card.ID) |
546
|
|
|
_ = log.SendLog() |
547
|
|
|
|
548
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
549
|
|
|
Success: true, |
550
|
|
|
Message: "Success delete card by ID", |
551
|
|
|
Data: *card, |
552
|
|
|
Count: 1, |
553
|
|
|
}) |
554
|
|
|
|
555
|
|
|
} |
556
|
|
|
|