From 2c14f5f45df1b893378d90d8e2362b2ff1d10573 Mon Sep 17 00:00:00 2001 From: Nikola Petrov Date: Thu, 6 Jul 2023 23:50:39 +0200 Subject: [PATCH] Update to movie controller and list --- controllers/movieController.js | 190 +++++++++++--------------- models/userModel.js | 9 ++ routes/{movies => api}/movieRouter.js | 9 +- routes/list.js | 22 +++ routes/movies/movieShow.js | 9 -- views/{movies/show.hbs => list.hbs} | 6 +- 6 files changed, 117 insertions(+), 128 deletions(-) create mode 100644 models/userModel.js rename routes/{movies => api}/movieRouter.js (53%) create mode 100644 routes/list.js delete mode 100644 routes/movies/movieShow.js rename views/{movies/show.hbs => list.hbs} (51%) diff --git a/controllers/movieController.js b/controllers/movieController.js index 33fa7c9..716c271 100644 --- a/controllers/movieController.js +++ b/controllers/movieController.js @@ -1,4 +1,5 @@ var MovieModel = require('../models/movieModel'); +var UserModel = require('../models/userModel'); /** * movieController.js @@ -7,118 +8,87 @@ var MovieModel = require('../models/movieModel'); */ module.exports = { - /** - * movieController.list() - */ - list: function (req, res) { - MovieModel.find() - .then(movies => { - return res.json(movies); - }) - .catch(err => { - return res.status(500).json({ - message: 'Error when getting movies.', - error: err - }); - }); - }, - - /** - * movieController.create() - */ - create: function (req, res) { - var movie = new MovieModel({ - title: req.body.title ? req.body.title : req.body.Title, - released: req.body.released ? req.body.released : req.body.Released, - webImg: req.body.webImg ? req.body.webImg : req.body.WebImg, - genre: req.body.genre ? req.body.genre : req.body.Genre, - code: req.body.code ? req.body.code : req.body.Code - }); - - MovieModel.findOne({ code: movie.code }) - .then(movieFound => { - if (movieFound) { - return res.status(409).json({ - message: 'Movie already exists' - }); - } - else - { - movie.save() - .then(savedMovie => { - // Handle successful save operation - return res.json(savedMovie); - }) - .catch(err => { - return res.status(500).json({ - message: 'Error when creating movie', - error: err - }); - }); - } - }) - .catch(err => { - return res.status(500).json({ - message: 'Error when getting movie', - error: err - }) - }); + /** + * movieController.list() + */ + list: function (req, res) { + MovieModel.find() + .then(movies => { + return res.json(movies); + }) + .catch(err => { + return res.status(500).json({ + message: 'Error when getting movies.', + error: err + }); + }); + }, + /** + * movieController.create() + */ + create: async function (req, res) { + const movieCode = req.body.code; + const passp = req.body.pass; + try { - }, + const userFound = await UserModel.findOne({ pass: passp }); + if (!userFound) { + return res.status(404).json({ message: 'Wrong password' }); + } - /** - * movieController.update() - */ - update: function (req, res) { - var id = req.params.id; - - MovieModel.findOne({ _id: id }) - .then(movie => { - if (!movie) { - return res.status(404).json({ - message: 'No such movie' - }); - } - - movie.title = req.body.title ? req.body.title : movie.title; - movie.released = req.body.released ? req.body.released : movie.released; - movie.webImg = req.body.webImg ? req.body.webImg : movie.webImg; - movie.genre = req.body.genre ? req.body.genre : movie.genre; - movie.code = req.body.code ? req.body.code : movie.code; - return movie.save(); - }) - .then(updatedMovie => { - return res.json(updatedMovie); - }) - .catch(err => { - return res.status(500).json({ - message: 'Error when updating movie.', - error: err - }); - }); - }, + const movieFound = await MovieModel.findOne({ code: movieCode }); + if (movieFound) { + return res.status(409).json({ message: 'Movie already exists' }); + } - /** - * movieController.remove() - */ - remove: function (req, res) { - var id = req.params.id; - console.log(id); - MovieModel.findOneAndDelete({ code: id }) - .then(movie => { - if (!movie) { - return res.status(404).json({ - message: 'No such movie' - }); - } - return res.status(204).json(); - }) - .catch(err => { - return res.status(500).json({ - message: 'Error when deleting the movie.', - error: err - }); - }); - }, + const uri = `http://www.omdbapi.com/?i=${movieCode}&apikey=${userFound.key}`; + const mJson = await fetch(uri); + const mData = await mJson.json(); + + if(mData.Response == 'False'){ + return res.status(404).json({message: 'wrong code'}); + } + + const movie = new MovieModel({ + code: movieCode, + title: mData.Title, + released: mData.Released, + webImg: mData.Poster, + genre: mData.Genre + }); + + const savedMovie = await movie.save(); + return res.status(201).json(savedMovie); + } catch (err) { + return res.status(500).json({ message: 'Error when creating movie' }); + } + }, + + /** + * movieController.delete() + */ + remove: async function (req, res) { + var id = req.body.code; + const passp = req.body.pass; + + try + { + const userFound = await UserModel.findOne({pass: passp}); + if (!userFound) { + 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' }); + } + + return res.status(204).json(); + } + catch(err) + { + return res.status(500).json({ message: 'Error when deleting the movie.' }); + } + }, }; diff --git a/models/userModel.js b/models/userModel.js new file mode 100644 index 0000000..7388421 --- /dev/null +++ b/models/userModel.js @@ -0,0 +1,9 @@ +var mongoose = require('mongoose'); +var Schema = mongoose.Schema; + +var userSchema = new Schema({ + 'pass' : String, + 'key' : String, +}); + +module.exports = mongoose.model('user', userSchema); \ No newline at end of file diff --git a/routes/movies/movieRouter.js b/routes/api/movieRouter.js similarity index 53% rename from routes/movies/movieRouter.js rename to routes/api/movieRouter.js index 49c0bf7..061b664 100644 --- a/routes/movies/movieRouter.js +++ b/routes/api/movieRouter.js @@ -1,18 +1,11 @@ var express = require('express'); - - - var router = express.Router(); var movieController = require('../../controllers/movieController.js'); - router.get('/', movieController.list); -//router.get('/publish', movieController.publish); router.post('/', movieController.create); -//router.put('/:id', movieController.update); - -router.delete('/:id', movieController.remove); +router.delete('/', movieController.remove); module.exports = router; \ No newline at end of file diff --git a/routes/list.js b/routes/list.js new file mode 100644 index 0000000..c36ddef --- /dev/null +++ b/routes/list.js @@ -0,0 +1,22 @@ +var express = require('express'); +const fs = require('fs'); +var router = express.Router(); + +/* GET home page. */ +router.get('/', function(req, res, next) { + + data = { + title: 'Movies', + scripts: [] + }; + + const f = fs.readdirSync('./public/assets/list/'); + f.forEach(file => { + + data.scripts.push("/assets/list/"+file); + }); + + res.render('list', data); +}); + +module.exports = router; \ No newline at end of file diff --git a/routes/movies/movieShow.js b/routes/movies/movieShow.js deleted file mode 100644 index a21926c..0000000 --- a/routes/movies/movieShow.js +++ /dev/null @@ -1,9 +0,0 @@ -var express = require('express'); -var router = express.Router(); - -/* GET home page. */ -router.get('/', function(req, res, next) { - res.render('movies/show', { title: 'Movies' }); -}); - -module.exports = router; \ No newline at end of file diff --git a/views/movies/show.hbs b/views/list.hbs similarity index 51% rename from views/movies/show.hbs rename to views/list.hbs index 7109632..b05d80d 100644 --- a/views/movies/show.hbs +++ b/views/list.hbs @@ -1,4 +1,8 @@ - + +{{#each scripts}} + +{{/each}} +