194 lines
7 KiB
JavaScript
194 lines
7 KiB
JavaScript
const connection = require('./connection');
|
|
const parseText = require('./parseText');
|
|
|
|
async function getAllGames(parameters = {}) {
|
|
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.win_supported AS win,
|
|
m.mac_supported AS mac,
|
|
m.linux_supported AS linux,
|
|
m.cover_art_filename AS coverArt
|
|
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 = [],
|
|
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");
|
|
}
|
|
|
|
if (typeof parameters.linux !== 'undefined' && parseInt(parameters.linux) === 1) {
|
|
whereStatements.push("m.linux_supported = 1");
|
|
}
|
|
|
|
if (typeof parameters.appID !== 'undefined' && parameters.appID.length > 0) {
|
|
const appID = parameters.appID;
|
|
whereStatements.push("m.id LIKE ?");
|
|
queryParameters.push('%' + appID + '%');
|
|
}
|
|
|
|
if (typeof parameters.title !== 'undefined' && parameters.title.length > 0) {
|
|
const title = parameters.title;
|
|
whereStatements.push("m.game_title LIKE ?");
|
|
queryParameters.push('%' + title + '%');
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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,
|
|
m.cover_art_filename AS coverArt,
|
|
m.short_description AS shortDescription,
|
|
m.long_description_markdown AS longDescription
|
|
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,
|
|
short_description, long_description, long_description_markdown, cover_art_filename) VALUES
|
|
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
|
|
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(insertSql, queryParameters);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
module.exports = {
|
|
getAllGames,
|
|
getGame,
|
|
addNewGame,
|
|
editGame
|
|
}
|