Move media to sql

This commit is contained in:
Nikola Petrov 2024-07-16 20:18:46 +02:00
parent 71a2245ce2
commit e89a8536be
3 changed files with 88 additions and 31 deletions

View File

@ -1,11 +1,11 @@
import { type Request, type Response } from "express";
import UserModel, { values } from '../models/userModel';
import { GameModel } from '../models/mediaModel';
import MediaModel, { Table } from '../models/mediaModel';
export default {
list: function (req: Request, res: Response) {
GameModel.find({}, { __v: 0 })
MediaModel.find(Table.games)
.then(games => {
return res.json(games);
})
@ -28,7 +28,7 @@ export default {
}
try {
const gameFound = await GameModel.findOne({ code: gameCode });
const gameFound = await MediaModel.findOne(Table.games, gameCode);
if (gameFound) {
return res.status(409).json({ message: 'Game already exists' });
}
@ -73,14 +73,14 @@ export default {
)
const coverData = await response.json()
const game = new GameModel({
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 game.save();
const savedGame = await MediaModel.save(Table.games, game.code, game.title, game.released, game.webImg);
return res.status(201).json(game);
} catch (error) {
@ -90,10 +90,10 @@ export default {
},
remove: async function (req: Request, res: Response) {
const id = req.body.code;
const code = req.body.code;
try {
const movie = await GameModel.findOneAndDelete({ code: id });
const movie = await MediaModel.findOneAndDelete(Table.games, code);
if (!movie) {
return res.status(404).json({ message: 'No such game' });
}

View File

@ -1,6 +1,6 @@
import { type Request, type Response } from "express";
import UserModel, { values } from '../models/userModel';
import { MovieModel, SeriesModel } from '../models/mediaModel';
import MediaModel, { Table } from '../models/mediaModel';
/**
* mediaController.js
@ -13,9 +13,9 @@ export default {
* mediaController.list()
*/
list: function (req: Request, res: Response) {
const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel;
const mediaTable = req.baseUrl.includes('movies') ? Table.movies : Table.series;
MediaModel.find({}, { __v: 0 })
MediaModel.find(mediaTable)
.then(media => {
return res.json(media);
})
@ -40,8 +40,8 @@ export default {
}
try {
const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel;
const mediaFound = await MediaModel.findOne({ code: mediaCode });
const mediaTable = req.baseUrl.includes('movies') ? Table.movies : Table.series;
const mediaFound = await MediaModel.findOne(mediaTable, mediaCode);
if (mediaFound) {
return res.status(409).json({ message: 'Media already exists' });
}
@ -54,14 +54,14 @@ export default {
return res.status(404).json({ message: 'wrong code' });
}
const media = new MediaModel({
const media = {
code: mediaCode,
title: mData.Title,
released: mData.Released,
webImg: mData.Poster,
});
};
const savedMedia = await media.save();
const savedMedia = await MediaModel.save(mediaTable, mediaCode, mData.Title, mData.Released, mData.Poster);
return res.status(201).json(media);
} catch (err) {
return res.status(500).json({ message: 'Error when creating media' });
@ -72,12 +72,11 @@ export default {
* mediaController.delete()
*/
remove: async function (req: Request, res: Response) {
const id = req.body.code;
const code = req.body.code;
try {
const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel;
const media = await MediaModel.findOneAndDelete({ code: id });
const mediaTable = req.baseUrl.includes('movies') ? Table.movies : Table.series;
const media = await MediaModel.findOneAndDelete(mediaTable, code);
if (!media) {
return res.status(404).json({ message: 'No such media' });
}

View File

@ -1,14 +1,72 @@
import mongoose, { Schema } from 'mongoose';
import { type ResultSetHeader, type RowDataPacket, type QueryOptions } from "mysql2"
import pool from '../miscellaneous/db'
const mediaSchema = new Schema({
'title': String,
'released': String,
'webImg': String,
'code': String
});
const MovieModel = mongoose.model('movie', mediaSchema);
const SeriesModel = mongoose.model('series', mediaSchema);
const GameModel = mongoose.model('game', mediaSchema);
interface Media extends RowDataPacket {
id?: number;
code?: number;
title?: string;
released?: string;
webImg?: string;
}
export { MovieModel, SeriesModel, GameModel };
export enum Table {
movies = "movies",
series = "series",
games = "games",
}
async function save(table: Table, code: number, title: string, released: string, webImg: string): Promise<number> {
try {
const options: QueryOptions = {
sql: "INSERT INTO " + table + " (code, title, released, webImg) VALUES (?,?,?,?)",
values: [code, title, released, webImg]
};
const [result, fields] = await pool.query<ResultSetHeader>(options);
return result.affectedRows;
}
catch (err) {
console.log(err);
}
return 0;
}
async function findOneAndDelete(table: Table, code: number): Promise<number> {
try {
const [result, fields] = await pool.query<ResultSetHeader>("DELETE FROM " + table + " WHERE code = ?;", [code]);
return result.affectedRows;
}
catch (err) {
console.log(err);
}
return 0;
}
async function findOne(table: Table, code: number): Promise<Media[]> {
try {
const [rows, fields] = await pool.query<Media[]>("SELECT * FROM " + table + " WHERE code = ?;", [code]);
return rows;
}
catch (err) {
console.log(err);
}
return [];
}
async function find(table: Table): Promise<Media[]> {
try {
const [rows, fields] = await pool.query<Media[]>("SELECT * FROM " + table + ";");
return rows;
}
catch (err) {
console.log(err);
}
return [];
}
export default {
save,
findOneAndDelete,
findOne,
find
};