gimm285-api-project/server/model/games.js

195 lines
7 KiB
JavaScript
Raw Normal View History

2023-04-18 14:41:16 -06:00
const connection = require('./connection');
2023-04-29 20:48:03 -06:00
const parseText = require('./parseText');
2023-04-18 14:41:16 -06:00
async function getAllGames(parameters = {}) {
2023-04-29 20:48:03 -06:00
let selectSql = `SELECT
2023-04-18 14:41:16 -06:00
m.id AS appID,
m.game_title AS title,
d.name AS developer,
p.name AS publisher,
m.release_date AS releaseDate,
m.short_description AS summary,
m.win_supported AS win,
m.mac_supported AS mac,
2023-04-29 20:48:03 -06:00
m.linux_supported AS linux,
m.cover_art_filename AS coverArt
2023-04-18 14:41:16 -06:00
FROM games_master_table m
INNER JOIN developers d ON m.developer_id = d.id
INNER JOIN publishers p ON m.publisher_id = p.id`,
2023-04-29 20:48:03 -06:00
whereStatements = [],
orderByStatements = [],
queryParameters = [];
if (typeof parameters.win !== 'undefined' && parseInt(parameters.win) === 1) {
whereStatements.push("m.win_supported = 1");
}
if (typeof parameters.mac !== 'undefined' && parseInt(parameters.mac) === 1) {
whereStatements.push("m.mac_supported = 1");
2023-04-18 14:41:16 -06:00
}
2023-04-29 20:48:03 -06:00
if (typeof parameters.linux !== 'undefined' && parseInt(parameters.linux) === 1) {
whereStatements.push("m.linux_supported = 1");
}
2023-04-18 14:41:16 -06:00
2023-04-29 20:48:03 -06:00
if (typeof parameters.appID !== 'undefined' && parameters.appID.length > 0) {
const appID = parameters.appID;
whereStatements.push("m.id LIKE ?");
queryParameters.push('%' + appID + '%');
}
2023-04-18 14:41:16 -06:00
2023-04-29 20:48:03 -06:00
if (typeof parameters.title !== 'undefined' && parameters.title.length > 0) {
const title = parameters.title;
whereStatements.push("m.game_title LIKE ?");
queryParameters.push('%' + title + '%');
}
2023-04-18 14:41:16 -06:00
2023-04-29 20:48:03 -06:00
if (typeof parameters.developer !== 'undefined' && parameters.developer.length > 0) {
const developer = parameters.developer;
whereStatements.push("d.name LIKE ?");
queryParameters.push('%' + developer + '%');
}
if (typeof parameters.publisher !== 'undefined' && parameters.publisher.length > 0) {
const publisher = parameters.publisher;
whereStatements.push("p.name LIKE ?");
queryParameters.push('%' + publisher + '%');
}
if (typeof parameters.releaseDate !== 'undefined' && parameters.releaseDate.length > 0) {
const releaseDate = parameters.releaseDate;
whereStatements.push("m.release_date LIKE ?");
queryParameters.push('%' + releaseDate + '%');
}
if (typeof parameters.orderBy !== 'undefined') {
const sort = parameters.orderBy;
if (sort === 'ASC') {
orderByStatements.push('m.id ASC');
} else if (sort === 'DESC') {
orderByStatements.push('m.id DESC')
}
}
//Dynamically add WHERE expressions to SELECT statements if needed
if (whereStatements.length > 0) {
selectSql = selectSql + ' WHERE ' + whereStatements.join(' AND ');
}
//Dynamically add ORDER BY expressions to SELECT statements if needed
if (orderByStatements.length > 0) {
selectSql = selectSql + ' ORDER BY ' + orderByStatements.join(', ');
}
//Dynamically add ORDER BY expressions to SELECT statements if needed
if (typeof parameters.limitNumber !== 'undefined' && parameters.limitNumber > 0 && parameters.limitNumber < 6) {
selectSql = selectSql + ' LIMIT ' + parameters.limitNumber;
}
return await connection.query(selectSql, queryParameters);
2023-04-18 14:41:16 -06:00
}
2023-04-29 20:48:03 -06:00
async function getGame(id) {
let selectSql = `SELECT
m.id AS appID,
m.game_title AS title,
d.name AS developer,
p.name AS publisher,
m.release_date AS releaseDate,
m.short_description AS summary,
m.long_description AS longDescription,
m.win_supported AS win,
m.mac_supported AS mac,
m.linux_supported AS linux,
2023-04-29 23:00:06 -06:00
m.cover_art_filename AS coverArt,
m.short_description AS shortDescription,
m.long_description_markdown AS longDescription
2023-04-29 20:48:03 -06:00
FROM games_master_table m
INNER JOIN developers d ON m.developer_id = d.id
INNER JOIN publishers p ON m.publisher_id = p.id`,
whereStatements = [],
queryParameters = [];
if (typeof id !== 'undefined' && id.length > 0) {
whereStatements.push("m.id LIKE ?");
queryParameters.push('%' + id + '%');
}
//Dynamically add WHERE expressions to SELECT statements if needed
if (whereStatements.length > 0) {
selectSql = selectSql + ' WHERE ' + whereStatements.join(' AND ');
}
return await connection.query(selectSql, queryParameters);
}
async function addNewGame(formInput, imagePath) {
console.log("Image: " + imagePath);
let parsedDescription = await parseText.parseMarkdown(formInput.longDescription);
let insertSql = `INSERT INTO games_master_table (game_title, developer_id, publisher_id, release_date,
win_supported, mac_supported, linux_supported,
2023-04-29 23:00:06 -06:00
short_description, long_description, long_description_markdown, cover_art_filename) VALUES
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
2023-04-29 20:48:03 -06:00
let queryParameters = [formInput.title,
formInput.developerID,
formInput.publisherID,
formInput.releaseDate,
formInput.win,
formInput.mac,
formInput.linux,
formInput.shortDescription,
parsedDescription,
2023-04-29 23:00:06 -06:00
formInput.longDescription,
2023-04-29 20:48:03 -06:00
imagePath];
2023-04-18 14:41:16 -06:00
2023-04-29 20:48:03 -06:00
return await connection.query(insertSql, queryParameters);
2023-04-18 14:41:16 -06:00
}
2023-04-29 23:00:06 -06:00
async function editGame(id, formInput, imagePath) {
console.log("Image: " + imagePath);
let updateSql = `UPDATE games_master_table
SET game_title = ?,
developer_id = ?,
publisher_id = ?,
release_date = ?,
win_supported = ?,
mac_supported = ?,
linux_supported = ?,
short_description = ?,
long_description = ?,
long_description_markdown = ?,
cover_art_filename = ?
WHERE id =` + id;
console.log(updateSql);
let parsedDescription = await parseText.parseMarkdown(formInput.longDescription);
let queryParameters = [formInput.title,
formInput.developerID,
formInput.publisherID,
formInput.releaseDate,
formInput.win,
formInput.mac,
formInput.linux,
formInput.shortDescription,
parsedDescription,
formInput.longDescription,
imagePath];
return await connection.query(updateSql, queryParameters);
}
2023-04-18 14:41:16 -06:00
module.exports = {
2023-04-29 20:48:03 -06:00
getAllGames,
getGame,
2023-04-29 23:00:06 -06:00
addNewGame,
editGame
2023-04-18 14:41:16 -06:00
}