From 905ab263de2e9ca5dd98b96e6ae258734add8bb6 Mon Sep 17 00:00:00 2001 From: Nikola Petrov Date: Tue, 16 Jul 2024 15:13:10 +0200 Subject: [PATCH] UP controller -> ts --- ...roller.js => cashTransactionController.ts} | 12 +- .../{gameController.js => gameController.ts} | 208 +++++++++--------- ...{mediaController.js => mediaController.ts} | 167 +++++++------- routes/api/cashTransactionRouter.ts | 2 +- routes/api/gameRouter.ts | 2 +- 5 files changed, 197 insertions(+), 194 deletions(-) rename controllers/{cashTransactionController.js => cashTransactionController.ts} (88%) rename controllers/{gameController.js => gameController.ts} (86%) rename controllers/{mediaController.js => mediaController.ts} (86%) diff --git a/controllers/cashTransactionController.js b/controllers/cashTransactionController.ts similarity index 88% rename from controllers/cashTransactionController.js rename to controllers/cashTransactionController.ts index dbaad55..d6d21b4 100644 --- a/controllers/cashTransactionController.js +++ b/controllers/cashTransactionController.ts @@ -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']; -module.exports = { - list: async function (req, res) { +export default { + + list: async function (req: Request, res: Response) { try { var transactions; 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; 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 }) .then(data => { res.status(201).json({ message: "OK" }); diff --git a/controllers/gameController.js b/controllers/gameController.ts similarity index 86% rename from controllers/gameController.js rename to controllers/gameController.ts index ffbce92..d996ed2 100644 --- a/controllers/gameController.js +++ b/controllers/gameController.ts @@ -1,104 +1,104 @@ -var { GameModel } = require('../models/mediaModel'); - - -module.exports = { - - list: function (req, res) { - GameModel.find({}, { __v: 0 }) - .then(games => { - return res.json(games); - }) - .catch(err => { - return res.status(500).json({ - message: 'Error when getting games.', - error: err - }); - }); - }, - - create: async function (req, res) { - var gameCode = req.body.code; - const userFound = req.user; - - try { - const gameFound = await GameModel.findOne({ code: gameCode }); - if (gameFound) { - 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"; - var response = await fetch(uri, { method: 'POST' }); - var mData = await response.json(); - - var mheaders = { - 'Accept': 'application/json', - 'Client-ID': userFound.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' }); - } - - var date = new Date(gameData[0].first_release_date * 1000); - const options = { day: 'numeric', month: 'short', year: 'numeric' } - var 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 = new GameModel({ - 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(); - return res.status(201).json(game); - - } catch (error) { - - return res.status(500).json({ message: 'Error when creating game', error: error }); - } - }, - - remove: async function (req, res) { - var id = req.body.code; - - try { - const movie = await GameModel.findOneAndDelete({ code: id }); - 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.' }); - } - } - -}; - - - +import { type Request, type Response } from "express"; +import { GameModel } from '../models/mediaModel'; + +export default { + + list: function (req: Request, res: Response) { + GameModel.find({}, { __v: 0 }) + .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 userFound = req.user; + + try { + const gameFound = await GameModel.findOne({ code: gameCode }); + if (gameFound) { + 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"; + var response = await fetch(uri, { method: 'POST' }); + var mData = await response.json(); + + var mheaders = { + 'Accept': 'application/json', + 'Client-ID': userFound.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' }); + } + + var date = new Date(gameData[0].first_release_date * 1000); + const options: Intl.DateTimeFormatOptions = { day: 'numeric', month: 'short', year: 'numeric' } + var 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 = new GameModel({ + 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(); + 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) { + var id = req.body.code; + + try { + const movie = await GameModel.findOneAndDelete({ code: id }); + 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.' }); + } + } + +}; + + + diff --git a/controllers/mediaController.js b/controllers/mediaController.ts similarity index 86% rename from controllers/mediaController.js rename to controllers/mediaController.ts index df901da..299125a 100644 --- a/controllers/mediaController.js +++ b/controllers/mediaController.ts @@ -1,83 +1,84 @@ -var { MovieModel, SeriesModel } = require('../models/mediaModel'); - -/** - * mediaController.js - * - * @description :: Server-side logic for managing movies and series. - */ -module.exports = { - - /** - * mediaController.list() - */ - list: function (req, res) { - const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel; - - MediaModel.find({}, { __v: 0 }) - .then(media => { - return res.json(media); - }) - .catch(err => { - return res.status(500).json({ - message: 'Error when getting media.', - error: err - }); - }); - }, - - /** - * mediaController.create() - */ - create: async function (req, res) { - const mediaCode = req.body.code; - const userFound = req.user; - try { - const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel; - const mediaFound = await MediaModel.findOne({ code: mediaCode }); - 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 mData = await mJson.json(); - - if (mData.Response == 'False') { - return res.status(404).json({ message: 'wrong code' }); - } - - const media = new MediaModel({ - code: mediaCode, - title: mData.Title, - released: mData.Released, - webImg: mData.Poster, - }); - - const savedMedia = await media.save(); - return res.status(201).json(media); - } catch (err) { - return res.status(500).json({ message: 'Error when creating media' }); - } - }, - - /** - * mediaController.delete() - */ - remove: async function (req, res) { - var id = req.body.code; - - try { - - const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel; - const media = await MediaModel.findOneAndDelete({ code: id }); - if (!media) { - return res.status(404).json({ message: 'No such media' }); - } - - return res.status(204).json(); - } - catch (err) { - return res.status(500).json({ message: 'Error when deleting the media.' }); - } - }, -}; +import { type Request, type Response } from "express"; +import { MovieModel, SeriesModel } from '../models/mediaModel'; + +/** + * mediaController.js + * + * @description :: Server-side logic for managing movies and series. + */ +export default { + + /** + * mediaController.list() + */ + list: function (req: Request, res: Response) { + const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel; + + MediaModel.find({}, { __v: 0 }) + .then(media => { + return res.json(media); + }) + .catch(err => { + return res.status(500).json({ + message: 'Error when getting media.', + error: err + }); + }); + }, + + /** + * mediaController.create() + */ + create: async function (req: Request, res: Response) { + const mediaCode = req.body.code; + const userFound = req.user; + try { + const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel; + const mediaFound = await MediaModel.findOne({ code: mediaCode }); + 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 mData = await mJson.json(); + + if (mData.Response == 'False') { + return res.status(404).json({ message: 'wrong code' }); + } + + const media = new MediaModel({ + code: mediaCode, + title: mData.Title, + released: mData.Released, + webImg: mData.Poster, + }); + + const savedMedia = await media.save(); + return res.status(201).json(media); + } catch (err) { + return res.status(500).json({ message: 'Error when creating media' }); + } + }, + + /** + * mediaController.delete() + */ + remove: async function (req: Request, res: Response) { + var id = req.body.code; + + try { + + const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel; + const media = await MediaModel.findOneAndDelete({ code: id }); + if (!media) { + return res.status(404).json({ message: 'No such media' }); + } + + return res.status(204).json(); + } + catch (err) { + return res.status(500).json({ message: 'Error when deleting the media.' }); + } + }, +}; diff --git a/routes/api/cashTransactionRouter.ts b/routes/api/cashTransactionRouter.ts index dd8ac25..7e25c48 100644 --- a/routes/api/cashTransactionRouter.ts +++ b/routes/api/cashTransactionRouter.ts @@ -1,6 +1,6 @@ import express from "express"; const router = express.Router(); -import cashTransaction from '../../controllers/cashTransactionController.js'; +import cashTransaction from '../../controllers/cashTransactionController'; router.post('/list', cashTransaction.list); diff --git a/routes/api/gameRouter.ts b/routes/api/gameRouter.ts index 36feaab..d242f2c 100644 --- a/routes/api/gameRouter.ts +++ b/routes/api/gameRouter.ts @@ -1,6 +1,6 @@ import express from "express"; var router = express.Router(); -import gameController from '../../controllers/gameController.js'; +import gameController from '../../controllers/gameController'; import checkAuthenticated from '../../miscellaneous/checkAuthenticated'; router.get('/', gameController.list);