gimm285-api-project/server/index.js

323 lines
12 KiB
JavaScript
Raw Normal View History

2023-04-18 14:41:16 -06:00
//Libraries
const express = require('express');
const multer = require('multer');
const mysql = require('mysql2');
const games = require('./model/games');
2023-04-29 20:48:03 -06:00
const developers = require('./model/developers');
const publishers = require('./model/publishers');
2023-04-18 14:41:16 -06:00
const {request, response} = require("express");
2023-04-29 20:48:03 -06:00
const {check, checkSchema, validationResult} = require("express-validator");
2023-04-29 23:00:06 -06:00
const path = require('path');
2023-05-02 06:01:30 -06:00
const cors = require('cors');
2023-04-18 14:41:16 -06:00
//Setup defaults for script
const app = express();
2023-05-02 06:01:30 -06:00
app.use(cors());
app.use(express.static('public'));
2023-04-29 23:00:06 -06:00
const storage = multer.diskStorage({
//Logic where to upload files
destination: function (request, file, callback) {
callback(null, 'public/images/')
},
//Logic to name the file when uploaded
filename: function (request, file, callback) {
callback(null, file.originalname + '-' + Date.now() + path.extname(file.originalname))
}
})
2023-04-29 20:48:03 -06:00
const upload = multer({
2023-04-29 23:00:06 -06:00
storage: storage,
2023-04-29 20:48:03 -06:00
//Validation for file upload
fileFilter: (request, file, callback) => {
const allowedFileMimeTypes = ["image/png", "image/jpg", "image/jpeg"];
callback(null, allowedFileMimeTypes.includes(file.mimetype));
}
});
2023-04-18 14:41:16 -06:00
const port = 8787;
//The * in app.* needs to match the method type of the request
app.get('/games', upload.none(),
async (request, response) => {
let result = {};
try {
result = await games.getAllGames(request.query);
} catch (error) {
return response
.status(500) //Error code when something goes wrong with the server
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({message: 'Something went wrong with the server.'});
}
//Default response object
response
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({'data': result});
});
2023-04-29 20:48:03 -06:00
app.get('/games/:id', upload.none(),
async (request, response) => {
console.log("Getting data for game " + request.params.id);
let result = {};
try {
result = await games.getGame(request.params.id);
} catch (error) {
return response
.status(500)
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({message: 'Something went wrong with the server!'})
}
response
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({'data': result})
});
app.get('/developers', upload.none(),
2023-04-18 14:41:16 -06:00
async (request, response) => {
let result = {};
try {
2023-04-29 20:48:03 -06:00
result = await developers.getDeveloperNames();
2023-04-18 14:41:16 -06:00
} catch (error) {
return response
.status(500) //Error code when something goes wrong with the server
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({message: 'Something went wrong with the server.'});
}
2023-04-29 20:48:03 -06:00
//Default response object
response
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({'data': result});
});
app.get('/developers/:id', upload.none(),
async (request, response) => {
console.log("Getting data for developer " + request.params.id);
let result = {};
try {
result = await developers.getDeveloper(request.params.id);
} catch (error) {
return response
.status(500)
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({message: 'Something went wrong with the server!'})
}
response
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({'data': result})
});
app.get('/publishers', upload.none(),
async (request, response) => {
let result = {};
try {
result = await publishers.getPublisherNames();
} catch (error) {
return response
.status(500) //Error code when something goes wrong with the server
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({message: 'Something went wrong with the server.'});
}
//Default response object
response
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({'data': result});
});
app.get('/publishers/:id', upload.none(),
async (request, response) => {
console.log("Getting data for publisher " + request.params.id);
let result = {};
try {
result = await publishers.getPublisher(request.params.id);
} catch (error) {
return response
.status(500)
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({message: 'Something went wrong with the server!'})
}
2023-04-18 14:41:16 -06:00
response
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
2023-04-29 20:48:03 -06:00
.json({'data': result})
});
app.post('/games', upload.single('coverArt'),
check('title', 'You must enter a title.').isLength({min: 3}),
check('releaseDate', 'You must select a release date.').isDate(["YYYY-MM-DD"]),
check('win', 'Invalid Windows option.').isIn([0, 1]),
check('mac', 'Invalid MacOS option.').isIn([0, 1]),
check('linux', 'Invalid Linux option.').isIn([0, 1]),
2023-05-02 10:10:55 -06:00
check('shortDescription', 'You must enter a short description of a valid length.').isLength({min:5, max:300}),
check('longDescription', 'You must enter a long description.').isLength({min:5}),
2023-04-29 20:48:03 -06:00
checkSchema({
'coverArt': {
2023-04-29 23:00:06 -06:00
optional: true,
2023-04-29 20:48:03 -06:00
custom: {
2023-04-29 23:00:06 -06:00
options: (value, {req, path}) => !!req.file[path],
2023-04-29 20:48:03 -06:00
errorMessage: 'Please upload an image file.',
},
},
}),
async (request, response) => {
2023-04-29 23:00:06 -06:00
console.log(request.file);
2023-04-29 20:48:03 -06:00
const errors = validationResult(request);
2023-04-29 23:15:37 -06:00
if (!errors.isEmpty()) {
2023-04-29 20:48:03 -06:00
return response
.status(400)
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({
message: 'Request fields or files are invalid.',
errors: errors.array(),
});
} else {
console.log(request.query);
let result = {};
try {
result = await games.addNewGame(request.query, request.file.filename);
} catch (error) {
console.log(error);
return response
.status(500) //Error code when something goes wrong with the server
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({message: 'Something went wrong with the server.'});
}
response
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({message: 'Game added successfully!'});
}
});
app.post('/developers', upload.none(),
check('name', 'You must enter a name.').isLength({min: 3}),
2023-04-29 23:15:37 -06:00
check('description', 'You must enter a description').isLength({min: 5}),
2023-05-02 10:10:55 -06:00
check('homepage', 'You must enter a valid homepage URL.').isURL(),
2023-04-29 20:48:03 -06:00
async (request, response) => {
const errors = validationResult(request);
2023-04-29 23:15:37 -06:00
if (!errors.isEmpty()) {
2023-04-29 20:48:03 -06:00
return response
.status(400)
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({
message: 'Request fields or files are invalid.',
errors: errors.array(),
});
} else {
console.log(request.query);
let result = {};
try {
result = await developers.addNewDeveloper(request.query);
} catch (error) {
console.log(error);
return response
.status(500) //Error code when something goes wrong with the server
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({message: 'Something went wrong with the server.'});
}
response
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({message: 'Developer added successfully!'});
}
});
app.post('/publishers', upload.none(),
check('name', 'You must enter a name.').isLength({min: 3}),
2023-04-29 23:15:37 -06:00
check('description', 'You must enter a description').isLength({min: 5}),
2023-05-02 10:10:55 -06:00
check('homepage', 'You must enter a valid homepage URL.').isURL(),
2023-04-29 20:48:03 -06:00
async (request, response) => {
const errors = validationResult(request);
2023-04-29 23:15:37 -06:00
if (!errors.isEmpty()) {
2023-04-29 20:48:03 -06:00
return response
.status(400)
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({
message: 'Request fields or files are invalid.',
errors: errors.array(),
});
} else {
console.log(request.query);
let result = {};
try {
result = await publishers.addNewPublisher(request.query);
} catch (error) {
console.log(error);
return response
.status(500) //Error code when something goes wrong with the server
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({message: 'Something went wrong with the server.'});
}
response
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({message: 'Publisher added successfully!'});
}
2023-04-18 14:41:16 -06:00
});
2023-04-29 23:00:06 -06:00
app.put('/games/:id', upload.single('coverArt'),
check('title', 'You must enter a title.').isLength({min: 3}),
check('releaseDate', 'You must select a release date.').isDate(["YYYY-MM-DD"]),
check('win', 'Invalid Windows option.').isIn([0, 1]),
check('mac', 'Invalid MacOS option.').isIn([0, 1]),
check('linux', 'Invalid Linux option.').isIn([0, 1]),
check('shortDescription', 'You must enter a description'),
checkSchema({
'coverArt': {
optional: true,
custom: {
options: (value, {req, path}) => !!req.file[path],
errorMessage: 'Please upload an image file.',
},
},
}),
async (request, response) => {
const errors = validationResult(request);
2023-04-29 23:15:37 -06:00
if (!errors.isEmpty()) {
2023-04-29 23:00:06 -06:00
return response
.status(400)
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({
message: 'Request fields or files are invalid.',
errors: errors.array(),
});
} else {
console.log(request.query);
let result = {};
try {
result = await games.editGame(request.query.id, request.query, request.file.filename);
} catch (error) {
console.log(error);
return response
.status(500) //Error code when something goes wrong with the server
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({message: 'Something went wrong with the server.'});
}
response
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({message: 'Game added successfully!'});
}
});
2023-04-29 23:15:37 -06:00
app.delete('/games/:id', upload.none(),
async (request, response) => {
let result = {};
try {
result = await games.deleteGame(request.query.id);
} catch (error) {
console.log(error);
return response
.status(500) //Error code when something goes wrong with the server
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({message: 'Something went wrong with the server.'});
}
response
.setHeader('Access-Control-Allow-Origin', '*') //Prevent CORS error
.json({message: 'Game deleted successfully!'});
});
2023-04-18 14:41:16 -06:00
app.listen(port, () => {
console.log(`Application listening at http://localhost:${port}`);
})