1
|
|
|
package controllers |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"memnixrest/app/database" |
5
|
|
|
"memnixrest/app/models" |
6
|
|
|
"memnixrest/pkg/queries" |
7
|
|
|
"net/http" |
8
|
|
|
"strings" |
9
|
|
|
|
10
|
|
|
"github.com/gofiber/fiber/v2" |
11
|
|
|
) |
12
|
|
|
|
13
|
|
|
// GetTodayCard method |
14
|
|
|
// @Description Get next today card |
15
|
|
|
// @Summary get a card |
16
|
|
|
// @Tags Card |
17
|
|
|
// @Produce json |
18
|
|
|
// @Success 200 {object} models.Card |
19
|
|
|
// @Router /v1/cards/today [get] |
20
|
|
|
func GetTodayCard(c *fiber.Ctx) error { |
21
|
|
|
//db := database.DBConn // DB Conn |
22
|
|
|
|
23
|
|
|
res := *new(models.ResponseHTTP) |
24
|
|
|
auth := CheckAuth(c, models.PermUser) // Check auth |
25
|
|
|
if !auth.Success { |
26
|
|
|
return c.Status(http.StatusUnauthorized).JSON(models.ResponseHTTP{ |
27
|
|
|
Success: false, |
28
|
|
|
Message: auth.Message, |
29
|
|
|
Data: nil, |
30
|
|
|
Count: 0, |
31
|
|
|
}) |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if res = queries.FetchNextTodayCard(c, &auth.User); !res.Success { |
35
|
|
|
return c.Status(http.StatusInternalServerError).JSON(models.ResponseHTTP{ |
36
|
|
|
Success: false, |
37
|
|
|
Message: res.Message, |
38
|
|
|
Data: nil, |
39
|
|
|
Count: 0, |
40
|
|
|
}) |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
44
|
|
|
Success: true, |
45
|
|
|
Message: "Get todays card", |
46
|
|
|
Data: res.Data, |
47
|
|
|
Count: 1, |
48
|
|
|
}) |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// GetNextCard method |
52
|
|
|
// @Description Get next card |
53
|
|
|
// @Summary get a card |
54
|
|
|
// @Tags Card |
55
|
|
|
// @Produce json |
56
|
|
|
// @Success 200 {object} models.Card |
57
|
|
|
// @Router /v1/cards/next [get] |
58
|
|
|
func GetNextCard(c *fiber.Ctx) error { |
59
|
|
|
//db := database.DBConn // DB Conn |
60
|
|
|
|
61
|
|
|
res := *new(models.ResponseHTTP) |
62
|
|
|
auth := CheckAuth(c, models.PermUser) // Check auth |
63
|
|
|
if !auth.Success { |
64
|
|
|
return c.Status(http.StatusUnauthorized).JSON(models.ResponseHTTP{ |
65
|
|
|
Success: false, |
66
|
|
|
Message: auth.Message, |
67
|
|
|
Data: nil, |
68
|
|
|
Count: 0, |
69
|
|
|
}) |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if res = queries.FetchNextCard(c, &auth.User); !res.Success { |
73
|
|
|
return c.Status(http.StatusInternalServerError).JSON(models.ResponseHTTP{ |
74
|
|
|
Success: false, |
75
|
|
|
Message: res.Message, |
76
|
|
|
Data: nil, |
77
|
|
|
Count: 0, |
78
|
|
|
}) |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
82
|
|
|
Success: true, |
83
|
|
|
Message: "Get todays card", |
84
|
|
|
Data: res.Data, |
85
|
|
|
Count: 1, |
86
|
|
|
}) |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// GetAllCards method |
91
|
|
|
// @Description Get every cards. Shouldn't really be used |
92
|
|
|
// @Summary get all cards |
93
|
|
|
// @Tags Card |
94
|
|
|
// @Produce json |
95
|
|
|
// @Success 200 {array} models.Card |
96
|
|
|
// @Router /v1/cards/ [get] |
97
|
|
|
func GetAllCards(c *fiber.Ctx) error { |
98
|
|
|
db := database.DBConn // DB Conn |
99
|
|
|
|
100
|
|
|
auth := CheckAuth(c, models.PermUser) // Check auth |
101
|
|
|
if !auth.Success { |
102
|
|
|
return c.Status(http.StatusUnauthorized).JSON(models.ResponseHTTP{ |
103
|
|
|
Success: false, |
104
|
|
|
Message: auth.Message, |
105
|
|
|
Data: nil, |
106
|
|
|
Count: 0, |
107
|
|
|
}) |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
var cards []models.Card |
111
|
|
|
|
112
|
|
|
if res := db.Joins("Deck").Find(&cards); res.Error != nil { |
113
|
|
|
|
114
|
|
|
return c.Status(http.StatusInternalServerError).JSON(models.ResponseHTTP{ |
115
|
|
|
Success: false, |
116
|
|
|
Message: "Failed to get all cards", |
117
|
|
|
Data: nil, |
118
|
|
|
Count: 0, |
119
|
|
|
}) |
120
|
|
|
} |
121
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
122
|
|
|
Success: true, |
123
|
|
|
Message: "Get All cards", |
124
|
|
|
Data: cards, |
125
|
|
|
Count: len(cards), |
126
|
|
|
}) |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// GetCardByID method to get a card by id |
131
|
|
|
// @Description Get a card by tech id |
132
|
|
|
// @Summary get a card |
133
|
|
|
// @Tags Card |
134
|
|
|
// @Produce json |
135
|
|
|
// @Param id path int true "Card ID" |
136
|
|
|
// @Success 200 {object} models.Card |
137
|
|
|
// @Router /v1/cards/id/{id} [get] |
138
|
|
|
func GetCardByID(c *fiber.Ctx) error { |
139
|
|
|
db := database.DBConn // DB Conn |
140
|
|
|
|
141
|
|
|
auth := CheckAuth(c, models.PermAdmin) // Check auth |
142
|
|
|
if !auth.Success { |
143
|
|
|
return c.Status(http.StatusUnauthorized).JSON(models.ResponseHTTP{ |
144
|
|
|
Success: false, |
145
|
|
|
Message: auth.Message, |
146
|
|
|
Data: nil, |
147
|
|
|
Count: 0, |
148
|
|
|
}) |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// Params |
152
|
|
|
id := c.Params("id") |
153
|
|
|
|
154
|
|
|
card := new(models.Card) |
155
|
|
|
|
156
|
|
|
if err := db.Joins("Deck").First(&card, id).Error; err != nil { |
157
|
|
|
return c.Status(http.StatusServiceUnavailable).JSON(models.ResponseHTTP{ |
158
|
|
|
Success: false, |
159
|
|
|
Message: err.Error(), |
160
|
|
|
Data: nil, |
161
|
|
|
Count: 0, |
162
|
|
|
}) |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
166
|
|
|
Success: true, |
167
|
|
|
Message: "Success get card by ID.", |
168
|
|
|
Data: *card, |
169
|
|
|
Count: 1, |
170
|
|
|
}) |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// GetCardsFromDeck method to get cards from deck |
174
|
|
|
// @Description Get every cards from a deck |
175
|
|
|
// @Summary get a list of card |
176
|
|
|
// @Tags Card |
177
|
|
|
// @Produce json |
178
|
|
|
// @Param deckID path int true "Deck ID" |
179
|
|
|
// @Success 200 {array} models.Card |
180
|
|
|
// @Router /v1/cards/deck/{deckID} [get] |
181
|
|
|
func GetCardsFromDeck(c *fiber.Ctx) error { |
182
|
|
|
db := database.DBConn // DB Conn |
183
|
|
|
|
184
|
|
|
// Params |
185
|
|
|
id := c.Params("deckID") |
186
|
|
|
|
187
|
|
|
var cards []models.Card |
188
|
|
|
|
189
|
|
|
if err := db.Joins("Deck").Where("cards.deck_id = ?", id).Find(&cards).Error; err != nil { |
190
|
|
|
return c.Status(http.StatusServiceUnavailable).JSON(models.ResponseHTTP{ |
191
|
|
|
Success: false, |
192
|
|
|
Message: err.Error(), |
193
|
|
|
Data: nil, |
194
|
|
|
Count: 0, |
195
|
|
|
}) |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
199
|
|
|
Success: true, |
200
|
|
|
Message: "Success get cards from deck.", |
201
|
|
|
Data: cards, |
202
|
|
|
Count: len(cards), |
203
|
|
|
}) |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
// POST |
207
|
|
|
|
208
|
|
|
// CreateNewCard |
209
|
|
|
func CreateNewCard(c *fiber.Ctx) error { |
210
|
|
|
db := database.DBConn // DB Conn |
211
|
|
|
|
212
|
|
|
card := new(models.Card) |
213
|
|
|
|
214
|
|
|
if err := c.BodyParser(&card); err != nil { |
215
|
|
|
return c.Status(http.StatusBadRequest).JSON(models.ResponseHTTP{ |
216
|
|
|
Success: false, |
217
|
|
|
Message: err.Error(), |
218
|
|
|
Data: nil, |
219
|
|
|
Count: 0, |
220
|
|
|
}) |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
db.Preload("Deck").Create(card) |
224
|
|
|
|
225
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
226
|
|
|
Success: true, |
227
|
|
|
Message: "Success register a card", |
228
|
|
|
Data: *card, |
229
|
|
|
Count: 1, |
230
|
|
|
}) |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
// PostResponse method |
234
|
|
|
// @Description Post a response and check it |
235
|
|
|
// @Summary post a response |
236
|
|
|
// @Tags Card |
237
|
|
|
// @Produce json |
238
|
|
|
// @Success 200 {array} models.Card |
239
|
|
|
// @Router /v1/cards/response [post] |
240
|
|
|
func PostResponse(c *fiber.Ctx) error { |
241
|
|
|
db := database.DBConn // DB Conn |
242
|
|
|
|
243
|
|
|
auth := CheckAuth(c, models.PermUser) // Check auth |
244
|
|
|
if !auth.Success { |
245
|
|
|
return c.Status(http.StatusUnauthorized).JSON(models.ResponseHTTP{ |
246
|
|
|
Success: false, |
247
|
|
|
Message: auth.Message, |
248
|
|
|
Data: nil, |
249
|
|
|
Count: 0, |
250
|
|
|
}) |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
response := new(models.CardResponse) |
254
|
|
|
card := new(models.Card) |
255
|
|
|
|
256
|
|
|
if err := c.BodyParser(&response); err != nil { |
257
|
|
|
return c.Status(http.StatusBadRequest).JSON(models.ResponseHTTP{ |
258
|
|
|
Success: false, |
259
|
|
|
Message: err.Error(), |
260
|
|
|
Data: nil, |
261
|
|
|
Count: 0, |
262
|
|
|
}) |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
if err := db.Joins("Deck").First(&card, response.CardID).Error; err != nil { |
266
|
|
|
return c.Status(http.StatusServiceUnavailable).JSON(models.ResponseHTTP{ |
267
|
|
|
Success: false, |
268
|
|
|
Message: err.Error(), |
269
|
|
|
Data: nil, |
270
|
|
|
Count: 0, |
271
|
|
|
}) |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
access := queries.CheckAccess(c, &auth.User, card) |
275
|
|
|
|
276
|
|
|
if access.Permission < models.AccessStudent { |
277
|
|
|
return c.Status(http.StatusServiceUnavailable).JSON(models.ResponseHTTP{ |
278
|
|
|
Success: false, |
279
|
|
|
Message: "You don't have the permission to answer this deck!", |
280
|
|
|
Data: nil, |
281
|
|
|
Count: 0, |
282
|
|
|
}) |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
validation := new(models.CardResponseValidation) |
286
|
|
|
|
287
|
|
|
if strings.EqualFold(strings.TrimSpace(response.Response), strings.TrimSpace(card.Answer)) { |
288
|
|
|
validation.Validate = true |
289
|
|
|
validation.Message = "Correct answer" |
290
|
|
|
} else { |
291
|
|
|
validation.Validate = false |
292
|
|
|
validation.Message = "Incorrect answer" |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
_ = queries.PostMem(c, auth.User, *card, *validation) |
296
|
|
|
|
297
|
|
|
validation.Answer = card.Answer |
298
|
|
|
|
299
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
300
|
|
|
Success: true, |
301
|
|
|
Message: "Success post response", |
302
|
|
|
Data: *validation, |
303
|
|
|
Count: 1, |
304
|
|
|
}) |
305
|
|
|
} |
306
|
|
|
|