From 6863081e9e3cda517d1bc6e7449545cc163ea66a Mon Sep 17 00:00:00 2001 From: Nikola Petrov Date: Thu, 10 Aug 2023 15:05:50 +0200 Subject: [PATCH] Podpora za srije modele in controllereje skupaj --- controllers/gameController.js | 8 +-- ...{movieController.js => mediaController.js} | 52 ++++++++++--------- models/gameModel.js | 11 ---- models/mediaModel.js | 15 ++++++ models/movieModel.js | 11 ---- routes/api/apiRouter.js | 10 ++-- routes/api/mediaRouter.js | 11 ++++ routes/api/movieRouter.js | 11 ---- views/list.hbs | 6 ++- 9 files changed, 66 insertions(+), 69 deletions(-) rename controllers/{movieController.js => mediaController.js} (51%) delete mode 100644 models/gameModel.js create mode 100644 models/mediaModel.js delete mode 100644 models/movieModel.js create mode 100644 routes/api/mediaRouter.js delete mode 100644 routes/api/movieRouter.js diff --git a/controllers/gameController.js b/controllers/gameController.js index c5bf3c6..c823830 100644 --- a/controllers/gameController.js +++ b/controllers/gameController.js @@ -1,5 +1,5 @@ var UserModel = require('../models/userModel'); -var GameModel = require('../models/gameModel'); +var { GameModel } = require('../models/mediaModel'); module.exports = { @@ -35,7 +35,7 @@ module.exports = { return res.status(409).json({ message: 'Game already exists' }); } - const uri = "https://id.twitch.tv/oauth2/token?client_id=" +userFound.client_id+ "&client_secret=" + userFound.client_secret+ "&grant_type=client_credentials"; + const uri = "https://id.twitch.tv/oauth2/token?client_id=" + userFound.client_id + "&client_secret=" + userFound.client_secret + "&grant_type=client_credentials"; var response = await fetch(uri, { method: 'POST' }); var mData = await response.json(); @@ -59,7 +59,7 @@ module.exports = { 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); @@ -74,7 +74,7 @@ module.exports = { } ) const coverData = await response.json() - + const game = new GameModel({ code: gameCode, title: gameData[0].name, diff --git a/controllers/movieController.js b/controllers/mediaController.js similarity index 51% rename from controllers/movieController.js rename to controllers/mediaController.js index 5fac838..a188e3f 100644 --- a/controllers/movieController.js +++ b/controllers/mediaController.js @@ -1,34 +1,36 @@ -var MovieModel = require('../models/movieModel'); var UserModel = require('../models/userModel'); +var { MovieModel, SeriesModel } = require('../models/mediaModel'); /** - * movieController.js + * mediaController.js * - * @description :: Server-side logic for managing movies. + * @description :: Server-side logic for managing movies and series. */ module.exports = { /** - * movieController.list() + * mediaController.list() */ list: function (req, res) { - MovieModel.find({}, { _id: 0, __v: 0 }) - .then(movies => { - return res.json(movies); + const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel; + + MediaModel.find({}, { _id: 0, __v: 0 }) + .then(media => { + return res.json(media); }) .catch(err => { return res.status(500).json({ - message: 'Error when getting movies.', + message: 'Error when getting media.', error: err }); }); }, /** - * movieController.create() + * mediaController.create() */ create: async function (req, res) { - const movieCode = req.body.code; + const mediaCode = req.body.code; const passp = req.body.pass; try { @@ -37,12 +39,13 @@ module.exports = { return res.status(404).json({ message: 'Wrong password' }); } - const movieFound = await MovieModel.findOne({ code: movieCode }); - if (movieFound) { - return res.status(409).json({ message: 'Movie already exists' }); + 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=${movieCode}&apikey=${userFound.key}`; + const uri = `http://www.omdbapi.com/?i=${mediaCode}&apikey=${userFound.key}`; const mJson = await fetch(uri); const mData = await mJson.json(); @@ -50,22 +53,22 @@ module.exports = { return res.status(404).json({ message: 'wrong code' }); } - const movie = new MovieModel({ - code: movieCode, + const media = new MediaModel({ + code: mediaCode, title: mData.Title, released: mData.Released, webImg: mData.Poster, }); - const savedMovie = await movie.save(); - return res.status(201).json(movie); + const savedMedia = await media.save(); + return res.status(201).json(media); } catch (err) { - return res.status(500).json({ message: 'Error when creating movie' }); + return res.status(500).json({ message: 'Error when creating media' }); } }, /** - * movieController.delete() + * mediaController.delete() */ remove: async function (req, res) { var id = req.body.code; @@ -77,15 +80,16 @@ module.exports = { return res.status(404).json({ message: 'Wrong password' }); } - const movie = await MovieModel.findOneAndDelete({ code: id }); - if (!movie) { - return res.status(404).json({ message: 'No such movie' }); + 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 movie.' }); + return res.status(500).json({ message: 'Error when deleting the media.' }); } }, }; diff --git a/models/gameModel.js b/models/gameModel.js deleted file mode 100644 index 5ebfac9..0000000 --- a/models/gameModel.js +++ /dev/null @@ -1,11 +0,0 @@ -var mongoose = require('mongoose'); -var Schema = mongoose.Schema; - -var gameSchema = new Schema({ - 'title': String, - 'released': String, - 'webImg': String, - 'code': String -}); - -module.exports = mongoose.model('game', gameSchema); \ No newline at end of file diff --git a/models/mediaModel.js b/models/mediaModel.js new file mode 100644 index 0000000..18121ce --- /dev/null +++ b/models/mediaModel.js @@ -0,0 +1,15 @@ +var mongoose = require('mongoose'); +var Schema = mongoose.Schema; + +var mediaSchema = new Schema({ + 'title': String, + 'released': String, + 'webImg': String, + 'code': String +}); + +module.exports = { + MovieModel: mongoose.model('movie', mediaSchema), + SeriesModel: mongoose.model('series', mediaSchema), + GameModel: mongoose.model('game', mediaSchema) +}; \ No newline at end of file diff --git a/models/movieModel.js b/models/movieModel.js deleted file mode 100644 index 3d4927a..0000000 --- a/models/movieModel.js +++ /dev/null @@ -1,11 +0,0 @@ -var mongoose = require('mongoose'); -var Schema = mongoose.Schema; - -var movieSchema = new Schema({ - 'title': String, - 'released': String, - 'webImg': String, - 'code': String -}); - -module.exports = mongoose.model('movie', movieSchema); \ No newline at end of file diff --git a/routes/api/apiRouter.js b/routes/api/apiRouter.js index 406fca3..95ae070 100644 --- a/routes/api/apiRouter.js +++ b/routes/api/apiRouter.js @@ -1,17 +1,13 @@ - - var express = require('express'); var router = express.Router(); - var photosRouter = require('./photoRouter'); -var movieRouter = require('./movieRouter'); +var mediaRouter = require('./mediaRouter'); var gameRouter = require('./gameRouter'); router.use('/photos', photosRouter); -router.use('/movies', movieRouter); router.use('/games', gameRouter); - - +router.use('/movies', mediaRouter); +router.use('/series', mediaRouter); module.exports = router; \ No newline at end of file diff --git a/routes/api/mediaRouter.js b/routes/api/mediaRouter.js new file mode 100644 index 0000000..6217e36 --- /dev/null +++ b/routes/api/mediaRouter.js @@ -0,0 +1,11 @@ +var express = require('express'); +var router = express.Router(); +var mediaController = require('../../controllers/mediaController.js'); + +router.get('/', mediaController.list); + +router.post('/', mediaController.create); + +router.delete('/', mediaController.remove); + +module.exports = router; \ No newline at end of file diff --git a/routes/api/movieRouter.js b/routes/api/movieRouter.js deleted file mode 100644 index 061b664..0000000 --- a/routes/api/movieRouter.js +++ /dev/null @@ -1,11 +0,0 @@ -var express = require('express'); -var router = express.Router(); -var movieController = require('../../controllers/movieController.js'); - -router.get('/', movieController.list); - -router.post('/', movieController.create); - -router.delete('/', movieController.remove); - -module.exports = router; \ No newline at end of file diff --git a/views/list.hbs b/views/list.hbs index a6e02b5..292fb28 100644 --- a/views/list.hbs +++ b/views/list.hbs @@ -7,7 +7,8 @@