1
|
|
|
package controllers |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"memnixrest/app/database" |
5
|
|
|
"memnixrest/app/models" |
6
|
|
|
"memnixrest/pkg/queries" |
7
|
|
|
"net/http" |
8
|
|
|
|
9
|
|
|
"github.com/gofiber/fiber/v2" |
10
|
|
|
) |
11
|
|
|
|
12
|
|
|
// GET |
13
|
|
|
|
14
|
|
|
// GetAllRatings method to get all ratings |
15
|
|
|
// @Description Get all ratings. Admin Only |
16
|
|
|
// @Summary get a list of ratings |
17
|
|
|
// @Tags Rating |
18
|
|
|
// @Produce json |
19
|
|
|
// @Success 200 {array} models.Rating |
20
|
|
|
// @Router /v1/ratings [get] |
21
|
|
|
func GetAllRatings(c *fiber.Ctx) error { |
22
|
|
|
db := database.DBConn // DB Conn |
23
|
|
|
|
24
|
|
|
auth := CheckAuth(c, models.PermAdmin) // 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
|
|
|
var ratings []models.Rating |
35
|
|
|
|
36
|
|
|
if res := db.Joins("User").Joins("Deck").Find(&ratings); res.Error != nil { |
37
|
|
|
|
38
|
|
|
return c.Status(http.StatusInternalServerError).JSON(models.ResponseHTTP{ |
39
|
|
|
Success: false, |
40
|
|
|
Message: "Failed to get all ratings", |
41
|
|
|
Data: nil, |
42
|
|
|
Count: 0, |
43
|
|
|
}) |
44
|
|
|
} |
45
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
46
|
|
|
Success: true, |
47
|
|
|
Message: "Get all ratings", |
48
|
|
|
Data: ratings, |
49
|
|
|
Count: len(ratings), |
50
|
|
|
}) |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// GetAllRatings method to get all ratings |
54
|
|
|
// @Description Get all ratings from a deckID. Admin Only |
55
|
|
|
// @Summary get a list of ratings |
56
|
|
|
// @Tags Rating |
57
|
|
|
// @Produce json |
58
|
|
|
// @Success 200 {array} models.Rating |
59
|
|
|
// @Router /v1/ratings/deck/{deckID} [get] |
60
|
|
|
func GetAllRatingsByDeck(c *fiber.Ctx) error { |
61
|
|
|
db := database.DBConn // DB Conn |
62
|
|
|
|
63
|
|
|
auth := CheckAuth(c, models.PermAdmin) // Check auth |
64
|
|
|
if !auth.Success { |
65
|
|
|
return c.Status(http.StatusUnauthorized).JSON(models.ResponseHTTP{ |
66
|
|
|
Success: false, |
67
|
|
|
Message: auth.Message, |
68
|
|
|
Data: nil, |
69
|
|
|
Count: 0, |
70
|
|
|
}) |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
deckID := c.Params("deckID") |
74
|
|
|
var ratings []models.Rating |
75
|
|
|
|
76
|
|
|
if res := db.Joins("User").Joins("Deck").Where("ratings.deck_id = ? ", deckID).Find(&ratings); res.Error != nil { |
77
|
|
|
|
78
|
|
|
return c.Status(http.StatusInternalServerError).JSON(models.ResponseHTTP{ |
79
|
|
|
Success: false, |
80
|
|
|
Message: "Failed to get all ratings", |
81
|
|
|
Data: nil, |
82
|
|
|
Count: 0, |
83
|
|
|
}) |
84
|
|
|
} |
85
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
86
|
|
|
Success: true, |
87
|
|
|
Message: "Get all ratings", |
88
|
|
|
Data: ratings, |
89
|
|
|
Count: len(ratings), |
90
|
|
|
}) |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// GetAllRatings method |
94
|
|
|
// @Description Get ratings from a deckID. |
95
|
|
|
// @Summary get a rating |
96
|
|
|
// @Tags Rating |
97
|
|
|
// @Produce json |
98
|
|
|
// @Success 200 {object} models.Rating |
99
|
|
|
// @Router /v1/ratings/deck/{deckID}/user [get] |
100
|
|
|
func GetRatingsByDeck(c *fiber.Ctx) error { |
101
|
|
|
db := database.DBConn // DB Conn |
102
|
|
|
|
103
|
|
|
auth := CheckAuth(c, models.PermUser) // Check auth |
104
|
|
|
if !auth.Success { |
105
|
|
|
return c.Status(http.StatusUnauthorized).JSON(models.ResponseHTTP{ |
106
|
|
|
Success: false, |
107
|
|
|
Message: auth.Message, |
108
|
|
|
Data: nil, |
109
|
|
|
Count: 0, |
110
|
|
|
}) |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
deckID := c.Params("deckID") |
114
|
|
|
var ratings []models.Rating |
115
|
|
|
|
116
|
|
|
if res := db.Joins("User").Joins("Deck").Where("ratings.deck_id = ? AND ratings.user_id = ?", deckID, auth.User.ID).First(&ratings); res.Error != nil { |
117
|
|
|
|
118
|
|
|
return c.Status(http.StatusInternalServerError).JSON(models.ResponseHTTP{ |
119
|
|
|
Success: false, |
120
|
|
|
Message: "Failed to get a rating", |
121
|
|
|
Data: nil, |
122
|
|
|
Count: 0, |
123
|
|
|
}) |
124
|
|
|
} |
125
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
126
|
|
|
Success: true, |
127
|
|
|
Message: "Get a rating", |
128
|
|
|
Data: ratings, |
129
|
|
|
Count: len(ratings), |
130
|
|
|
}) |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// GetAverageRatingByDeck method |
134
|
|
|
// @Description Get average rating from a deckID. |
135
|
|
|
// @Summary get an average rating |
136
|
|
|
// @Tags Rating |
137
|
|
|
// @Produce json |
138
|
|
|
// @Success 200 {object} integer |
139
|
|
|
// @Router /v1/ratings/deck/{deckID}/average [get] |
140
|
|
|
func GetAverageRatingByDeck(c *fiber.Ctx) error { |
141
|
|
|
db := database.DBConn // DB Conn |
142
|
|
|
|
143
|
|
|
auth := CheckAuth(c, models.PermUser) // Check auth |
144
|
|
|
if !auth.Success { |
145
|
|
|
return c.Status(http.StatusUnauthorized).JSON(models.ResponseHTTP{ |
146
|
|
|
Success: false, |
147
|
|
|
Message: auth.Message, |
148
|
|
|
Data: nil, |
149
|
|
|
Count: 0, |
150
|
|
|
}) |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
deckID := c.Params("deckID") |
154
|
|
|
var averageValue float32 |
155
|
|
|
|
156
|
|
|
if res := db.Table("ratings").Select("AVG(value)").Where("ratings.deck_id = ? ", deckID).Find(&averageValue); res.Error != nil { |
157
|
|
|
|
158
|
|
|
return c.Status(http.StatusInternalServerError).JSON(models.ResponseHTTP{ |
159
|
|
|
Success: false, |
160
|
|
|
Message: "Failed to get average rating", |
161
|
|
|
Data: nil, |
162
|
|
|
Count: 0, |
163
|
|
|
}) |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
167
|
|
|
Success: true, |
168
|
|
|
Message: "Get average rating", |
169
|
|
|
Data: averageValue, |
170
|
|
|
Count: 1, |
171
|
|
|
}) |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// GetRatingByDeckAndUser method |
175
|
|
|
// @Description Get a rating by user & deck |
176
|
|
|
// @Summary get a rating |
177
|
|
|
// @Tags Rating |
178
|
|
|
// @Produce json |
179
|
|
|
// @Success 200 {object} models.Rating |
180
|
|
|
// @Router /v1/ratings/deck/{deckID}/user/{userID} [get] |
181
|
|
|
func GetRatingByDeckAndUser(c *fiber.Ctx) error { |
182
|
|
|
db := database.DBConn // DB Conn |
183
|
|
|
|
184
|
|
|
auth := CheckAuth(c, models.PermAdmin) // Check auth |
185
|
|
|
if !auth.Success { |
186
|
|
|
return c.Status(http.StatusUnauthorized).JSON(models.ResponseHTTP{ |
187
|
|
|
Success: false, |
188
|
|
|
Message: auth.Message, |
189
|
|
|
Data: nil, |
190
|
|
|
Count: 0, |
191
|
|
|
}) |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
deckID := c.Params("deckID") |
195
|
|
|
userID := c.Params("userID") |
196
|
|
|
|
197
|
|
|
rating := new(models.Rating) |
198
|
|
|
|
199
|
|
|
if res := db.Joins("User").Joins("Deck").Where("ratings.deck_id = ? AND ratings.user_id = ?", deckID, userID).First(&rating); res.Error != nil { |
200
|
|
|
|
201
|
|
|
return c.Status(http.StatusInternalServerError).JSON(models.ResponseHTTP{ |
202
|
|
|
Success: false, |
203
|
|
|
Message: "Failed to get a rating", |
204
|
|
|
Data: nil, |
205
|
|
|
Count: 0, |
206
|
|
|
}) |
207
|
|
|
} |
208
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
209
|
|
|
Success: true, |
210
|
|
|
Message: "Get a rating", |
211
|
|
|
Data: rating, |
212
|
|
|
Count: 1, |
213
|
|
|
}) |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
// POST |
217
|
|
|
|
218
|
|
|
// RateDeck method |
219
|
|
|
// @Description Rate a deck |
220
|
|
|
// @Summary rate a deck |
221
|
|
|
// @Tags Rating |
222
|
|
|
// @Produce json |
223
|
|
|
// @Accept json |
224
|
|
|
// @Param rating body models.Rating true "Rating to create or update" |
225
|
|
|
// @Success 200 |
226
|
|
|
// @Router /v1/rating/new [post] |
227
|
|
|
func RateDeck(c *fiber.Ctx) error { |
228
|
|
|
|
229
|
|
|
rating := new(models.Rating) |
230
|
|
|
|
231
|
|
|
// Check auth |
232
|
|
|
auth := CheckAuth(c, models.PermUser) |
233
|
|
|
if !auth.Success { |
234
|
|
|
return c.Status(http.StatusUnauthorized).JSON(models.ResponseHTTP{ |
235
|
|
|
Success: false, |
236
|
|
|
Message: auth.Message, |
237
|
|
|
Data: nil, |
238
|
|
|
Count: 0, |
239
|
|
|
}) |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
if err := c.BodyParser(&rating); err != nil { |
243
|
|
|
return c.Status(http.StatusBadRequest).JSON(models.ResponseHTTP{ |
244
|
|
|
Success: false, |
245
|
|
|
Message: err.Error(), |
246
|
|
|
Data: nil, |
247
|
|
|
Count: 0, |
248
|
|
|
}) |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
rating.UserID = auth.User.ID |
252
|
|
|
|
253
|
|
|
if err := queries.GenerateRating(c, rating); !err.Success { |
254
|
|
|
return c.Status(http.StatusInternalServerError).JSON(models.ResponseHTTP{ |
255
|
|
|
Success: false, |
256
|
|
|
Message: err.Message, |
257
|
|
|
Data: nil, |
258
|
|
|
Count: 0, |
259
|
|
|
}) |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return c.Status(http.StatusOK).JSON(models.ResponseHTTP{ |
263
|
|
|
Success: true, |
264
|
|
|
Message: "Success rating the deck", |
265
|
|
|
Data: rating, |
266
|
|
|
Count: 1, |
267
|
|
|
}) |
268
|
|
|
} |
269
|
|
|
|