Update to movie controller and list

This commit is contained in:
Nikola Petrov 2023-07-06 23:50:39 +02:00
parent d46c4bba61
commit 2c14f5f45d
6 changed files with 117 additions and 128 deletions

View File

@ -1,4 +1,5 @@
var MovieModel = require('../models/movieModel'); var MovieModel = require('../models/movieModel');
var UserModel = require('../models/userModel');
/** /**
* movieController.js * movieController.js
@ -7,118 +8,87 @@ var MovieModel = require('../models/movieModel');
*/ */
module.exports = { module.exports = {
/** /**
* movieController.list() * movieController.list()
*/ */
list: function (req, res) { list: function (req, res) {
MovieModel.find() MovieModel.find()
.then(movies => { .then(movies => {
return res.json(movies); return res.json(movies);
}) })
.catch(err => { .catch(err => {
return res.status(500).json({ return res.status(500).json({
message: 'Error when getting movies.', message: 'Error when getting movies.',
error: err 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.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' });
}
/** const movieFound = await MovieModel.findOne({ code: movieCode });
* movieController.update() if (movieFound) {
*/ return res.status(409).json({ message: 'Movie already exists' });
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 uri = `http://www.omdbapi.com/?i=${movieCode}&apikey=${userFound.key}`;
* movieController.remove() const mJson = await fetch(uri);
*/ const mData = await mJson.json();
remove: function (req, res) {
var id = req.params.id; if(mData.Response == 'False'){
console.log(id); return res.status(404).json({message: 'wrong code'});
MovieModel.findOneAndDelete({ code: id }) }
.then(movie => {
if (!movie) { const movie = new MovieModel({
return res.status(404).json({ code: movieCode,
message: 'No such movie' title: mData.Title,
}); released: mData.Released,
} webImg: mData.Poster,
return res.status(204).json(); genre: mData.Genre
}) });
.catch(err => {
return res.status(500).json({ const savedMovie = await movie.save();
message: 'Error when deleting the movie.', return res.status(201).json(savedMovie);
error: err } 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.' });
}
},
}; };

9
models/userModel.js Normal file
View File

@ -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);

View File

@ -1,18 +1,11 @@
var express = require('express'); var express = require('express');
var router = express.Router(); var router = express.Router();
var movieController = require('../../controllers/movieController.js'); var movieController = require('../../controllers/movieController.js');
router.get('/', movieController.list); router.get('/', movieController.list);
//router.get('/publish', movieController.publish);
router.post('/', movieController.create); router.post('/', movieController.create);
//router.put('/:id', movieController.update); router.delete('/', movieController.remove);
router.delete('/:id', movieController.remove);
module.exports = router; module.exports = router;

22
routes/list.js Normal file
View File

@ -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;

View File

@ -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;

View File

@ -1,4 +1,8 @@
<script defer src="/global/movies/script.js"></script>
{{#each scripts}}
<script defer src="{{this}}"></script>
{{/each}}
<div class="album py-5 bg-body-tertiary"> <div class="album py-5 bg-body-tertiary">
<div class="container" id="movie_tbody"> <div class="container" id="movie_tbody">