From 0d6c2d0aa8f4c1b759935166cb776b29f4a137ae Mon Sep 17 00:00:00 2001 From: Nikola Petrov Date: Sun, 25 Jun 2023 01:48:32 +0200 Subject: [PATCH] Dodal movie data --- app.js | 6 +- controllers/movieController.js | 127 ++++++++++++++++++++++ controllers/photoController.js | 1 - models/movieModel.js | 12 ++ routes/movieRouter.js | 19 ++++ routes/{photoRoutes.js => photoRouter.js} | 0 views/error.hbs | 1 + views/main/1_0.hbs | 1 + 8 files changed, 164 insertions(+), 3 deletions(-) create mode 100644 controllers/movieController.js create mode 100644 models/movieModel.js create mode 100644 routes/movieRouter.js rename routes/{photoRoutes.js => photoRouter.js} (100%) diff --git a/app.js b/app.js index 28a4c17..3bfacf5 100644 --- a/app.js +++ b/app.js @@ -17,7 +17,8 @@ db.on('error', console.error.bind(console, 'MongoDB connection error:')); var oldRouter = require('./routes/main/1_0'); var newRouter = require('./routes/main/2_0'); -var photosRouter = require('./routes/photoRoutes'); +var photosRouter = require('./routes/photoRouter'); +var movieRouter = require('./routes/movieRouter'); var app = express(); @@ -56,7 +57,8 @@ app.use(function (req, res, next) { app.use('/', newRouter); app.use('/old', oldRouter); -app.use('/photos', photosRouter); +app.use('/api/photos', photosRouter); +app.use('/api/movies', movieRouter); // catch 404 and forward to error handler app.use(function(req, res, next) { diff --git a/controllers/movieController.js b/controllers/movieController.js new file mode 100644 index 0000000..145508f --- /dev/null +++ b/controllers/movieController.js @@ -0,0 +1,127 @@ +var MovieModel = require('../models/movieModel'); + +/** + * movieController.js + * + * @description :: Server-side logic for managing movies. + */ +module.exports = { + + /** + * movieController.list() + */ + list: function (req, res) { + MovieModel.find() + .then(movies => { + var data = { + movies: movies + }; + return res.json(data); + }) + .catch(err => { + return res.status(500).json({ + message: 'Error when getting movies.', + error: err + }); + }); + }, + + /** + * movieController.show() + */ + show: function (req, res) { + var id = req.params.id; + + MovieModel.findOne({_id: id}, function (err, movie) { + if (err) { + return res.status(500).json({ + message: 'Error when getting movie.', + error: err + }); + } + + if (!movie) { + return res.status(404).json({ + message: 'No such movie' + }); + } + + return res.json(movie); + }); + }, + + /** + * 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 + }); + + 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 + }); + }); + }, + + /** + * 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 + }); + }); + }, + + /** + * movieController.remove() + */ + remove: function (req, res) { + var id = req.params.id; + + MovieModel.findByIdAndRemove(id, function (err, movie) { + if (err) { + return res.status(500).json({ + message: 'Error when deleting the movie.', + error: err + }); + } + + return res.status(204).json(); + }); + }, +}; diff --git a/controllers/photoController.js b/controllers/photoController.js index ba66a29..46b5980 100644 --- a/controllers/photoController.js +++ b/controllers/photoController.js @@ -12,7 +12,6 @@ module.exports = { */ list: function (req, res) { PhotoModel.find() - .exec() .then(photos => { var data = { photos: photos diff --git a/models/movieModel.js b/models/movieModel.js new file mode 100644 index 0000000..5256a51 --- /dev/null +++ b/models/movieModel.js @@ -0,0 +1,12 @@ +var mongoose = require('mongoose'); +var Schema = mongoose.Schema; + +var movieSchema = new Schema({ + 'title' : String, + 'released' : String, + 'webImg' : String, + 'genre' : String, + 'code' : String +}); + +module.exports = mongoose.model('movie', movieSchema); \ No newline at end of file diff --git a/routes/movieRouter.js b/routes/movieRouter.js new file mode 100644 index 0000000..4ce828d --- /dev/null +++ b/routes/movieRouter.js @@ -0,0 +1,19 @@ +var express = require('express'); + + + +var router = express.Router(); +var movieController = require('../controllers/movieController.js'); + + +router.get('/', movieController.list); +//router.get('/publish', movieController.publish); +//router.get('/:id', movieController.show); + +router.post('/', movieController.create); + +//router.put('/:id', movieController.update); + +//router.delete('/:id', movieController.remove); + +module.exports = router; \ No newline at end of file diff --git a/routes/photoRoutes.js b/routes/photoRouter.js similarity index 100% rename from routes/photoRoutes.js rename to routes/photoRouter.js diff --git a/views/error.hbs b/views/error.hbs index b458c56..9fc4e88 100644 --- a/views/error.hbs +++ b/views/error.hbs @@ -3,6 +3,7 @@ + Document