Combine game and media controller and router

This commit is contained in:
Nikola Petrov 2024-07-17 02:19:16 +02:00
parent 9175cb8b5f
commit c88b27e5d5
5 changed files with 154 additions and 177 deletions

View File

@ -1,111 +0,0 @@
import { type Request, type Response } from "express";
import UserModel, { values } from '../models/userModel';
import MediaModel, { Table } from '../models/mediaModel';
export default {
list: function (req: Request, res: Response) {
MediaModel.find(Table.games)
.then(games => {
return res.json(games);
})
.catch(err => {
return res.status(500).json({
message: 'Error when getting games.',
error: err
});
});
},
create: async function (req: Request, res: Response) {
var gameCode = req.body.code;
const twitch_client_id = await UserModel.getValue(values.twitch_client_id);
const twitch_client_secret = await UserModel.getValue(values.twitch_client_secret);
if (!twitch_client_id || !twitch_client_secret) {
return res.status(500).json({ message: 'Error when creating game' });
}
try {
const gameFound = await MediaModel.findOne(Table.games, gameCode);
if (gameFound) {
return res.status(409).json({ message: 'Game already exists' });
}
const uri = "https://id.twitch.tv/oauth2/token?client_id=" + twitch_client_id + "&client_secret=" + twitch_client_secret + "&grant_type=client_credentials";
var response = await fetch(uri, { method: 'POST' });
const mData = await response.json();
const mheaders: HeadersInit = {
'Accept': 'application/json',
'Client-ID': twitch_client_id,
'Authorization': 'Bearer ' + mData.access_token
}
gameCode = parseInt(gameCode)
response = await fetch(
"https://api.igdb.com/v4/games",
{
method: 'POST',
headers: mheaders,
body: `fields name, first_release_date; where id = ${gameCode};`
}
)
const gameData = await response.json()
if (gameData.length == 0) {
return res.status(404).json({ message: 'wrong code' });
}
const date = new Date(gameData[0].first_release_date * 1000);
const options: Intl.DateTimeFormatOptions = { day: 'numeric', month: 'short', year: 'numeric' }
const dateStr = date.toLocaleDateString(undefined, options);
response = await fetch(
"https://api.igdb.com/v4/covers",
{
method: 'POST',
headers: mheaders,
body: `fields image_id; where game = ${gameCode};`
}
)
const coverData = await response.json()
const game = {
code: gameCode,
title: gameData[0].name,
released: dateStr,
webImg: `https://images.igdb.com/igdb/image/upload/t_cover_big/${coverData[0].image_id}.jpg`,
};
const savedGame = await MediaModel.save(Table.games, game.code, game.title, game.released, game.webImg);
return res.status(201).json(game);
} catch (error) {
return res.status(500).json({ message: 'Error when creating game', error: error });
}
},
remove: async function (req: Request, res: Response) {
const code = req.body.code;
try {
const movie = await MediaModel.findOneAndDelete(Table.games, code);
if (!movie) {
return res.status(404).json({ message: 'No such game' });
}
return res.status(204).json();
}
catch (err) {
return res.status(500).json({ message: 'Error when deleting the game.' });
}
}
};

View File

@ -2,18 +2,151 @@ import { type Request, type Response } from "express";
import UserModel, { values } from '../models/userModel';
import MediaModel, { Table } from '../models/mediaModel';
/**
* mediaController.js
*
* @description :: Server-side logic for managing movies and series.
*/
function fromStringToTable(value: string): (Table | undefined) {
if (value.localeCompare("games") == 0) return Table.games;
if (value.localeCompare("movies") == 0) return Table.movies;
if (value.localeCompare("series") == 0) return Table.series;
return;
}
async function createMed(req: Request, res: Response) {
const mediaCode: string = req.body.code;
const omdb_key = await UserModel.getValue(values.omdb_key);
if (!omdb_key) {
return res.status(500).json({ message: 'Error when creating media' });
}
// remove the tt in front in DB its stored as number
const sub = mediaCode.substring(2);
const cleanCode = parseInt(sub);
try {
const seriesFound = await MediaModel.findOne(Table.series, cleanCode);
if (seriesFound.length != 0) {
return res.status(409).json({ message: 'Media already exists' });
}
const moviesFound = await MediaModel.findOne(Table.movies, cleanCode);
if (moviesFound.length != 0) {
return res.status(409).json({ message: 'Media already exists' });
}
const uri = `http://www.omdbapi.com/?i=${mediaCode}&apikey=${omdb_key}`;
const mJson = await fetch(uri);
const mData = await mJson.json();
if (mData.Response == 'False') {
return res.status(404).json({ message: 'wrong code' });
}
const media = {
code: mediaCode,
title: mData.Title,
released: mData.Released,
webImg: mData.Poster,
};
if (mData.Type.localeCompare("movie") == 0) {
const savedMedia = await MediaModel.save(Table.movies, cleanCode, mData.Title, mData.Released, mData.Poster);
}
else if (mData.Type.localeCompare("series") == 0) {
const savedMedia = await MediaModel.save(Table.series, cleanCode, mData.Title, mData.Released, mData.Poster);
}
return res.status(201).json(media);
} catch (err) {
return res.status(500).json({ message: 'Error when creating media' });
}
}
async function createGame(req: Request, res: Response) {
var gameCode = req.body.code;
const twitch_client_id = await UserModel.getValue(values.twitch_client_id);
const twitch_client_secret = await UserModel.getValue(values.twitch_client_secret);
if (!twitch_client_id || !twitch_client_secret) {
return res.status(500).json({ message: 'Error when creating game' });
}
try {
const gameFound = await MediaModel.findOne(Table.games, gameCode);
if (gameFound) {
return res.status(409).json({ message: 'Game already exists' });
}
const uri = "https://id.twitch.tv/oauth2/token?client_id=" + twitch_client_id + "&client_secret=" + twitch_client_secret + "&grant_type=client_credentials";
var response = await fetch(uri, { method: 'POST' });
const mData = await response.json();
const mheaders: HeadersInit = {
'Accept': 'application/json',
'Client-ID': twitch_client_id,
'Authorization': 'Bearer ' + mData.access_token
}
gameCode = parseInt(gameCode)
response = await fetch(
"https://api.igdb.com/v4/games",
{
method: 'POST',
headers: mheaders,
body: `fields name, first_release_date; where id = ${gameCode};`
}
)
const gameData = await response.json()
if (gameData.length == 0) {
return res.status(404).json({ message: 'wrong code' });
}
const date = new Date(gameData[0].first_release_date * 1000);
const options: Intl.DateTimeFormatOptions = { day: 'numeric', month: 'short', year: 'numeric' }
const dateStr = date.toLocaleDateString(undefined, options);
response = await fetch(
"https://api.igdb.com/v4/covers",
{
method: 'POST',
headers: mheaders,
body: `fields image_id; where game = ${gameCode};`
}
)
const coverData = await response.json()
const game = {
code: gameCode,
title: gameData[0].name,
released: dateStr,
webImg: `https://images.igdb.com/igdb/image/upload/t_cover_big/${coverData[0].image_id}.jpg`,
};
const savedGame = await MediaModel.save(Table.games, game.code, game.title, game.released, game.webImg);
return res.status(201).json(game);
} catch (error) {
return res.status(500).json({ message: 'Error when creating game', error: error });
}
}
export default {
/**
* mediaController.list()
*/
list: function (req: Request, res: Response) {
const mediaTable = req.baseUrl.includes('movies') ? Table.movies : Table.series;
const mediaTable = fromStringToTable(req.params.mediaType);
if (!mediaTable) {
return res.status(500).json({
message: 'Error when getting media.'
});
}
MediaModel.find(mediaTable)
.then(media => {
@ -27,48 +160,12 @@ export default {
});
},
/**
* mediaController.create()
*/
create: async function (req: Request, res: Response) {
const mediaCode: string = req.body.code;
const omdb_key = await UserModel.getValue(values.omdb_key);
if (!omdb_key) {
return res.status(500).json({ message: 'Error when creating media' });
}
// remove the tt in front in DB its stored as number
const sub = mediaCode.substring(2);
const cleanCode = parseInt(sub);
try {
const mediaTable = req.baseUrl.includes('movies') ? Table.movies : Table.series;
const mediaFound = await MediaModel.findOne(mediaTable, cleanCode);
if (mediaFound.length != 0) {
return res.status(409).json({ message: 'Media already exists' });
}
const uri = `http://www.omdbapi.com/?i=${mediaCode}&apikey=${omdb_key}`;
const mJson = await fetch(uri);
const mData = await mJson.json();
if (mData.Response == 'False') {
return res.status(404).json({ message: 'wrong code' });
}
const media = {
code: mediaCode,
title: mData.Title,
released: mData.Released,
webImg: mData.Poster,
};
const savedMedia = await MediaModel.save(mediaTable, cleanCode, mData.Title, mData.Released, mData.Poster);
return res.status(201).json(media);
} catch (err) {
return res.status(500).json({ message: 'Error when creating media' });
if (mediaCode.startsWith("t")) {
return await createMed(req, res);
} else {
return await createGame(req, res);
}
},
@ -76,6 +173,13 @@ export default {
* mediaController.delete()
*/
remove: async function (req: Request, res: Response) {
const mediaTable = fromStringToTable(req.params.mediaType);
if (!mediaTable) {
return res.status(500).json({
message: 'Error when deleting the media.'
});
}
const code = req.body.code;
try {

View File

@ -1,14 +1,11 @@
import express, { type Request, type Response } from "express";
import checkAuthenticated from '../../miscellaneous/checkAuthenticated';
import mediaRouter from './mediaRouter';
import gameRouter from './gameRouter';
import cashTransactionRouter from './cashTransactionRouter';
const router = express.Router();
router.use('/games', gameRouter);
router.use('/movies', mediaRouter);
router.use('/series', mediaRouter);
router.use('/media', mediaRouter);
router.use('/cash', checkAuthenticated, cashTransactionRouter);
router.get('/', function (req: Request, res: Response) {

View File

@ -1,13 +0,0 @@
import express from "express";
import gameController from '../../controllers/gameController';
import checkAuthenticated from '../../miscellaneous/checkAuthenticated';
const router = express.Router();
router.get('/', gameController.list);
router.post('/', checkAuthenticated, gameController.create);
router.delete('/', checkAuthenticated, gameController.remove);
export default router;

View File

@ -4,10 +4,10 @@ import checkAuthenticated from '../../miscellaneous/checkAuthenticated';
const router = express.Router();
router.get('/', mediaController.list);
router.get('/:mediaType', mediaController.list);
router.post('/', checkAuthenticated, mediaController.create);
router.post('/:mediaType', checkAuthenticated, mediaController.create);
router.delete('/', checkAuthenticated, mediaController.remove);
router.delete('/:mediaType', checkAuthenticated, mediaController.remove);
export default router;