forked from alexsmaldone/good-game-guides
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreation.js
291 lines (257 loc) · 7.83 KB
/
creation.js
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
const db = require("./db/models");
const { sequelize } = require("./db/models");
const { check, validationResult } = require("express-validator");
async function findStatusShelfEntries(userId, statusId) {
return await db.StatusShelf.findAll({
where: {
userId,
statusId
},
include: [
{
model: db.Status
},
{
model: db.GameGuide,
include: db.Review
}
]
})
}
async function addStatusShelfEntry(statusId, gameguideId, userId) {
// Check if gameGuide is in any of the 3 status shelves:
const guideStatusCheck = await db.StatusShelf.findAll({
where: {
gameguideId,
userId,
},
});
// If it is in a status shelf already:
if (guideStatusCheck) {
// Change it to be in the selected status shelf
entries[0].statusId = statusId;
} else {
// If the guide isn't in any status shelf:
await db.StatusShelf.create({ statusId, gameguideId, userId });
}
}
async function findCustomShelfEntries(userId, name) {
const result = await db.CustomShelf.findAll({
where: {
userId,
name
},
include: {
model: db.GameGuide,
include: [
{
model: db.Review,
},
{
model: db.StatusShelf,
include: db.Status
}
]
}
})
if (result) {
return result
} else return null
}
async function checkIfCustomNameExists(name, userId) {
const shelf = await findCustomShelfEntries(userId, name);
if (shelf && shelf.length) return true;
else return false;
}
async function addCustomShelfName(name, userId) {
if (name) {
if (/^[\w\-]+$/.test(name)) {
const exists = await checkIfCustomNameExists(name, userId);
if (!exists) {
await db.CustomShelf.create({ name, userId });
return "success";
} else {
return "There is already a shelf with this name";
}
} else {
return "Shelf name can only contain 1 or more lowercase letters, uppercase letters, 0-9, -, or _";
}
} else {
return "Please provide a value for the shelf name";
}
}
async function addGuideToCustomShelf(name, userId, gameGuideId) {
const shelf = await findCustomShelfEntries(userId, name);
// Check if one entry w/ name & if gameguideId is null.
if (shelf.length === 1 && shelf[0].gameGuideId === null) {
shelf[0].gameGuideId = gameGuideId;
} else if (shelf) {
await db.CustomShelf.create({ name, userId, gameGuideId });
} else {
throw new Error("something broke with adding guide to custom shelf");
}
}
async function checkCountOfShelfEntries(shelf, userId) {
let count;
// If shelf is a custom shelf name:
if (typeof shelf === "string") {
result = await db.CustomShelf.findAndCountAll({
where: {
userId,
name: shelf
}
})
if (result.count === 1) {
const one = await db.CustomShelf.findOne({
where: {
userId,
name: shelf
}
})
if (!one.gameGuideId) {
result.count = 0
}
}
count = result;
}
// If shelf is a status shelf id:
if (typeof shelf === "number") {
count = await db.StatusShelf.findAndCountAll({
where: {
userId,
statusId: shelf
}
})
}
return count.count
}
async function allStatusShelfEntries(userId) {
return await db.StatusShelf.findAll({
where: {
userId
},
include: [
{
model: db.Status
},
{
model: db.GameGuide,
include: {
model: db.Review
}
}
]
})
}
// async function findAvgRating(gameGuideId) {
// return db.Review.findOne({
// where: {
// gameGuideId
// },
// attributes: [sequelize.fn('AVG', sequelize.col('rating'))]
// });
// };
async function checkIfOnlyOneCustomShelfEntry(userId, name) {
const guides = findCustomShelfEntries(userId, name)
if (guides && guides.length === 1) {
return true;
} else return false;
}
async function countGuidesOnShelves(userId, whichShelves) {
if (whichShelves === 'all' || typeof whichShelves === 'number') {
let count1 = await checkCountOfShelfEntries(1, userId);
let count2 = await checkCountOfShelfEntries(2, userId);
let count3 = await checkCountOfShelfEntries(3, userId);
if (whichShelves === 'all') {
return count1 + count2 + count3
}
if (whichShelves === 1) return count1;
else if (whichShelves === 2) return count2;
else if (whichShelves === 3) return count3;
} else if (whichShelves === 'customShelves') {
let customCounts = [];
let customShelves = await db.CustomShelf.findAll({
where: {
userId
},
attributes: [[sequelize.fn('distinct', sequelize.col('name')), 'name']],
raw: true,
})
for (let shelf of customShelves) {
let shelfName = shelf.name
const result = await checkCountOfShelfEntries(shelfName, userId);
customCounts.push({ count: result, name: shelfName })
}
return customCounts;
}
}
async function statusAndAllCounts(userId) {
const all = await countGuidesOnShelves(userId, 'all');
const one = await countGuidesOnShelves(userId, 1);
const two = await countGuidesOnShelves(userId, 2);
const three = await countGuidesOnShelves(userId, 3);
return {all, one, two, three}
}
async function customCounts(userId) {
// returns objects in array with shelf.name and shelf.count
const customs = await countGuidesOnShelves(userId, 'customShelves')
return customs
}
// ------------------------- RATING FUNCTIONS ---------------------------------
function findAverageRating(reviews) {
// Takes in array of all reviews for a gameguide
let sum = 0
for (let review of reviews) {
sum += review.rating
}
return Math.round(sum / reviews.length)
}
function makeRatingObj(rating) {
let ratingArr = {1: false, 2: false, 3: false, 4: false, 5: false}
for (let i = 1; i <= 5; i++) {
if (i <= rating) {
ratingArr[`${i}`] = true
}
}
return ratingArr
}
function makeRatingArrsForAllReviews(reviews) {
// Replaces rating number with a ratingArr
for (let review of reviews) {
let rating = review.rating
review.rating = makeRatingObj(rating)
}
return reviews;
}
function makeRatingArrsForGameGuideReviews(guides, userId) {
for (let guide of guides) {
let reviews;
if (guide.GameGuide.Reviews) {
for (let review of guide.GameGuide.Reviews) {
if (review.userId === userId) {
let rating = review.rating
review.rating = makeRatingObj(rating)
reviews = review
}
}
}
guide.GameGuide.Reviews = reviews
}
return guides;
}
module.exports = {
addStatusShelfEntry,
findStatusShelfEntries,
findCustomShelfEntries,
addCustomShelfName,
checkIfCustomNameExists,
addGuideToCustomShelf,
checkCountOfShelfEntries,
allStatusShelfEntries,
customCounts,
statusAndAllCounts,
findAverageRating,
makeRatingObj,
makeRatingArrsForAllReviews,
makeRatingArrsForGameGuideReviews
}