UP controller -> ts

This commit is contained in:
Nikola Petrov 2024-07-16 15:13:10 +02:00
parent b4b18cd44d
commit 905ab263de
5 changed files with 197 additions and 194 deletions

View File

@ -1,9 +1,11 @@
var cashTransactionModel = require('../models/cashTransactionModel.js'); import { type Request, type Response } from "express";
import cashTransactionModel from '../models/cashTransactionModel.js';
const types = ['ZAVRNITEV POS NAKUP', 'POS NAKUP', 'BA DVIG', 'priliv', 'SDD', 'SPLET/TEL NAKUP', 'PREDAVTORIZACIJE']; const types = ['ZAVRNITEV POS NAKUP', 'POS NAKUP', 'BA DVIG', 'priliv', 'SDD', 'SPLET/TEL NAKUP', 'PREDAVTORIZACIJE'];
module.exports = {
list: async function (req, res) { export default {
list: async function (req: Request, res: Response) {
try { try {
var transactions; var transactions;
const date = req.body.date; const date = req.body.date;
@ -30,7 +32,7 @@ module.exports = {
}; };
}, },
create: async function (req, res) { create: async function (req: Request, res: Response) {
const rawString = req.body.messageBody; const rawString = req.body.messageBody;
if (rawString == "") return res.status(400).json({ message: "empty string" }); if (rawString == "") return res.status(400).json({ message: "empty string" });
@ -83,7 +85,7 @@ module.exports = {
} }
}, },
delete: async function (req, res) { delete: async function (req: Request, res: Response) {
cashTransactionModel.deleteMany({ type: -1 }) cashTransactionModel.deleteMany({ type: -1 })
.then(data => { .then(data => {
res.status(201).json({ message: "OK" }); res.status(201).json({ message: "OK" });

View File

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

View File

@ -1,83 +1,84 @@
var { MovieModel, SeriesModel } = require('../models/mediaModel'); import { type Request, type Response } from "express";
import { MovieModel, SeriesModel } from '../models/mediaModel';
/**
* mediaController.js /**
* * mediaController.js
* @description :: Server-side logic for managing movies and series. *
*/ * @description :: Server-side logic for managing movies and series.
module.exports = { */
export default {
/**
* mediaController.list() /**
*/ * mediaController.list()
list: function (req, res) { */
const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel; list: function (req: Request, res: Response) {
const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel;
MediaModel.find({}, { __v: 0 })
.then(media => { MediaModel.find({}, { __v: 0 })
return res.json(media); .then(media => {
}) return res.json(media);
.catch(err => { })
return res.status(500).json({ .catch(err => {
message: 'Error when getting media.', return res.status(500).json({
error: err message: 'Error when getting media.',
}); error: err
}); });
}, });
},
/**
* mediaController.create() /**
*/ * mediaController.create()
create: async function (req, res) { */
const mediaCode = req.body.code; create: async function (req: Request, res: Response) {
const userFound = req.user; const mediaCode = req.body.code;
try { const userFound = req.user;
const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel; try {
const mediaFound = await MediaModel.findOne({ code: mediaCode }); const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel;
if (mediaFound) { const mediaFound = await MediaModel.findOne({ code: mediaCode });
return res.status(409).json({ message: 'Media already exists' }); if (mediaFound) {
} return res.status(409).json({ message: 'Media already exists' });
}
const uri = `http://www.omdbapi.com/?i=${mediaCode}&apikey=${userFound.omdb_key}`;
const mJson = await fetch(uri); const uri = `http://www.omdbapi.com/?i=${mediaCode}&apikey=${userFound.omdb_key}`;
const mData = await mJson.json(); const mJson = await fetch(uri);
const mData = await mJson.json();
if (mData.Response == 'False') {
return res.status(404).json({ message: 'wrong code' }); if (mData.Response == 'False') {
} return res.status(404).json({ message: 'wrong code' });
}
const media = new MediaModel({
code: mediaCode, const media = new MediaModel({
title: mData.Title, code: mediaCode,
released: mData.Released, title: mData.Title,
webImg: mData.Poster, released: mData.Released,
}); webImg: mData.Poster,
});
const savedMedia = await media.save();
return res.status(201).json(media); const savedMedia = await media.save();
} catch (err) { return res.status(201).json(media);
return res.status(500).json({ message: 'Error when creating media' }); } catch (err) {
} return res.status(500).json({ message: 'Error when creating media' });
}, }
},
/**
* mediaController.delete() /**
*/ * mediaController.delete()
remove: async function (req, res) { */
var id = req.body.code; remove: async function (req: Request, res: Response) {
var id = req.body.code;
try {
try {
const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel;
const media = await MediaModel.findOneAndDelete({ code: id }); const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel;
if (!media) { const media = await MediaModel.findOneAndDelete({ code: id });
return res.status(404).json({ message: 'No such media' }); if (!media) {
} return res.status(404).json({ message: 'No such media' });
}
return res.status(204).json();
} return res.status(204).json();
catch (err) { }
return res.status(500).json({ message: 'Error when deleting the media.' }); catch (err) {
} return res.status(500).json({ message: 'Error when deleting the media.' });
}, }
}; },
};

View File

@ -1,6 +1,6 @@
import express from "express"; import express from "express";
const router = express.Router(); const router = express.Router();
import cashTransaction from '../../controllers/cashTransactionController.js'; import cashTransaction from '../../controllers/cashTransactionController';
router.post('/list', cashTransaction.list); router.post('/list', cashTransaction.list);

View File

@ -1,6 +1,6 @@
import express from "express"; import express from "express";
var router = express.Router(); var router = express.Router();
import gameController from '../../controllers/gameController.js'; import gameController from '../../controllers/gameController';
import checkAuthenticated from '../../miscellaneous/checkAuthenticated'; import checkAuthenticated from '../../miscellaneous/checkAuthenticated';
router.get('/', gameController.list); router.get('/', gameController.list);