forked from TiagoDanin/MyDatabaseGameAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
384 lines (328 loc) · 8.5 KB
/
database.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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
const {v4: uuidv4} = require('uuid')
const mysql = require('mysql2/promise')
let connection
const connect = async () => {
connection = await mysql.createConnection(`mysql://${process.env.MYSQLHOST}/gamedb`)
console.log('[!] Database is ready!')
connection.query(`
USE gamedb;
`)
}
const getAllGames = async () => {
const [rows] = await connection.query('SELECT * FROM game;')
return rows
}
const getAllGamesByGender = async (paramaters) => {
if (!paramaters.genderId) {
return getAllGames()
}
const [rows] = await connection.query(`
SELECT game.*
FROM gamedb.game, gamedb.game_genero
WHERE game.id = game_genero.game_id AND game_genero.genero_id = ?;
`, [paramaters.genderId])
return rows
}
const getAllUsers = async () => {
const [rows] = await connection.query('SELECT * FROM usuario;')
return rows
}
const getAllGamesSoon = async () => {
const [rows] = await connection.query(`
SELECT *
FROM gamedb.game
WHERE lacamento_data >= current_date()
ORDER BY lacamento_data ASC;
`)
return rows
}
const getAllGamesTop = async () => {
const [rows] = await connection.query(`
SELECT game.*,
(SELECT ((SUM(nota) / 3) / COUNT(*)) # 1 = Não gostei, 2 = Neutro, 3 = Gostei
FROM gamedb.nota
WHERE game.id = nota.game_id) AS nota_geral
FROM gamedb.game
ORDER BY nota_geral DESC
LIMIT 5;
`)
return rows
}
const getAllUsersTops = async () => {
const [rows] = await connection.query(`
SELECT usuario.*,
(SELECT COUNT(*)
FROM gamedb.comentario, gamedb.game
WHERE
comentario.usuario_id = usuario.id AND
comentario.game_id = game.id) AS total
FROM gamedb.usuario
ORDER BY total DESC
LIMIT 5;
`)
return rows
}
const getAllGamesTopComments = async () => {
const [rows] = await connection.query(`
SELECT game.*,
(SELECT COUNT(*)
FROM gamedb.comentario
WHERE comentario.game_id = game.id) AS total
FROM gamedb.game
ORDER BY total DESC
LIMIT 5;
`)
return rows
}
const getUserComments = async () => {
const [rows] = await connection.query(`
SELECT
comentario.id,
comentario.texto,
game.nome
FROM gamedb.comentario, gamedb.game
WHERE
comentario.usuario_id = 1 AND
comentario.game_id = game.id;
`)
return rows
}
const getSearchGames = async (paramaters) => {
const text = `%${paramaters.text}%`
const [rows] = await connection.query(`
SELECT *
FROM gamedb.game
WHERE nome LIKE ?;
`, [text])
return rows
}
const isLoginValid = async (paramaters) => {
const [rows] = await connection.query(`
SELECT *
FROM gamedb.usuario
WHERE nome_de_usuario = ? AND password = ?;
`, [paramaters.username, paramaters.password])
return rows
}
const createLogin = async (paramaters) => {
const [rows] = await connection.query(`
INSERT INTO gamedb.usuario (nome_de_usuario, password, nome, email)
VALUES (?, ?, ?, ?);
`, [
paramaters.username,
paramaters.password,
paramaters.name ? paramaters.name : null,
paramaters.email
])
return rows
}
const getOsGame = async (paramaters) => {
const [rows] = await connection.query(`
SELECT sistema_operacional.nome
FROM gamedb.game, gamedb.game_sistema_operacional, gamedb.sistema_operacional
WHERE game.id = ? AND
game.id = game_sistema_operacional.game_id AND
game_sistema_operacional.sistema_operacional_id = sistema_operacional.id;
`, [paramaters.gameId])
return rows
}
const getGendersGame = async (paramaters) => {
const [rows] = await connection.query(`
SELECT genero.nome
FROM gamedb.game, gamedb.game_genero, gamedb.genero
WHERE game.id = ? AND
game.id = game_genero.game_id AND
game_genero.genero_id = genero.id;
`, [paramaters.gameId])
return rows
}
const getDistributorGame = async (paramaters) => {
const [rows] = await connection.query(`
SELECT empresa.nome
FROM gamedb.empresa, gamedb.distribuidora, gamedb.game
WHERE game.id = ? AND
game.id = distribuidora.game_id AND
distribuidora.empresa_id = empresa.id;
`, [paramaters.gameId])
return rows
}
const getDeveloperGame = async (paramaters) => {
const [rows] = await connection.query(`
SELECT empresa.nome
FROM gamedb.empresa, gamedb.desenvolvedor, gamedb.game
WHERE game.id = ? AND
game.id = desenvolvedor.game_id AND
desenvolvedor.empresa_id = empresa.id;
`, [paramaters.gameId])
return rows
}
const getRateGame = async (paramaters) => {
const [rows] = await connection.query(`SELECT fnObterNota(?) as nota;`, [paramaters.gameId])
return rows[0]
}
const createComment = async (paramaters) => {
const [rows] = await connection.query(`
INSERT INTO gamedb.comentario (game_id, usuario_id, texto)
VALUES (?, ?, ?);
`, [paramaters.gameId, paramaters.userId, paramaters.text])
return rows
}
const listCommentsGame = async (paramaters) => {
const [rows] = await connection.query(`
SELECT
comentario.id,
comentario.texto,
usuario.nome_de_usuario AS nome
FROM gamedb.comentario, gamedb.usuario
WHERE
comentario.game_id = ? AND
usuario.id = comentario.usuario_id;
`, [paramaters.gameId])
return rows
}
const getDistributorGames = async (paramaters) => {
const [rows] = await connection.query(`
SELECT game.*
FROM gamedb.distribuidora, gamedb.game
WHERE game.id = distribuidora.game_id AND
distribuidora.empresa_id = 1;
`, [paramaters.id])
return rows
}
const getDeveloperGames = async (paramaters) => {
const [rows] = await connection.query(`
SELECT game.*
FROM gamedb.desenvolvedor, gamedb.game
WHERE game.id = desenvolvedor.game_id AND
desenvolvedor.empresa_id = 1;
`, [paramaters.id])
return rows
}
const getBusinesses = async () => {
const [rows] = await connection.query(`SELECT * FROM gamedb.empresa;`)
return rows
}
const getAllGenders = async () => {
const [rows] = await connection.query('SELECT * FROM gamedb.genero;')
return rows
}
const getAllOs = async () => {
const [rows] = await connection.query('SELECT * FROM gamedb.sistema_operacional;')
return rows
}
const createBusiness = async (paramaters) => {
const [rows] = await connection.query(`
INSERT INTO gamedb.empresa (nome) VALUES (?);
`, [paramaters.text])
return rows
}
const createGender = async (paramaters) => {
const [rows] = await connection.query(`
INSERT INTO gamedb.genero (nome) VALUES (?);
`, [paramaters.text])
return rows
}
const createGame = async (paramaters) => {
let gameId = 0;
const {
userId,
name,
description,
releaseDate,
steamId,
genders,
developers,
distributions,
os
} = paramaters
const [rows] = await connection.query(`
INSERT INTO gamedb.game (nome, descricao, lacamento_data, steam_id)
VALUES (?, ?, ?, ?);
`, [name, description, releaseDate, steamId ? steamId : null])
gameId = rows.insertId
for (id of genders.split(',')) {
await connection.query(`
INSERT INTO gamedb.game_genero (game_id, genero_id) VALUES (?, ?);
`, [gameId, id])
}
for (id of developers.split(',')) {
await connection.query(`
INSERT INTO gamedb.desenvolvedor (game_id, empresa_id) VALUES (?, ?);
`, [gameId, id])
}
for (id of developers.split(',')) {
await connection.query(`
INSERT INTO gamedb.desenvolvedor (game_id, empresa_id) VALUES (?, ?);
`, [gameId, id])
}
for (id of distributions.split(',')) {
await connection.query(`
INSERT INTO gamedb.distribuidora (game_id, empresa_id) VALUES (?, ?);
`, [gameId, id])
}
for (id of os.split(',')) {
await connection.query(`
INSERT INTO gamedb.game_sistema_operacional (game_id, sistema_operacional_id) VALUES (?, ?);
`, [gameId, id])
}
await connection.query(`
INSERT INTO gamedb.game_editado_por (usuario_id, game_id) VALUES (?, ?);
`, [userId, gameId])
return rows
}
const sendRateGame = async (paramaters) => {
const {
gameId,
userId,
rate
} = paramaters
const [rows] = await connection.query(`
SELECT nota FROM gamedb.nota
WHERE usuario_id = ?
AND game_id = ?;
`, [userId, gameId])
if (rows.length <= 0) {
await connection.query(`
INSERT INTO gamedb.nota (game_id, usuario_id, nota)
VALUES (?, ?, ?);
`, [gameId, userId, rate])
} else {
await connection.query(`
UPDATE gamedb.nota
SET nota = ?
WHERE usuario_id = ?
AND game_id = ?;
`, [rate, userId, gameId])
}
return getRateGame(paramaters)
}
module.exports = {
createGame,
connect,
getAllGames,
getAllUsers,
getAllGamesSoon,
getAllGenders,
getAllGamesByGender,
getAllGamesTop,
getAllUsersTops,
getAllGamesTopComments,
getUserComments,
getSearchGames,
isLoginValid,
createLogin,
getOsGame,
getGendersGame,
getDistributorGame,
getDeveloperGame,
getRateGame,
createComment,
createGender,
listCommentsGame,
getDistributorGames,
getDeveloperGames,
getBusinesses,
createBusiness,
getAllOs,
sendRateGame
}