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() * movieController.create()
*/ */
create: function (req, res) { create: async function (req, res) {
var movie = new MovieModel({ const movieCode = req.body.code;
title: req.body.title ? req.body.title : req.body.Title, const passp = req.body.pass;
released: req.body.released ? req.body.released : req.body.Released, try {
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 }) const userFound = await UserModel.findOne({ pass: passp });
.then(movieFound => { if (!userFound) {
if (movieFound) { return res.status(404).json({ message: 'Wrong password' });
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
})
});
const movieFound = await MovieModel.findOne({ code: movieCode });
if (movieFound) {
return res.status(409).json({ message: 'Movie already exists' });
}
}, 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'){
* movieController.update() return res.status(404).json({message: 'wrong code'});
*/ }
update: function (req, res) {
var id = req.params.id;
MovieModel.findOne({ _id: id }) const movie = new MovieModel({
.then(movie => { code: movieCode,
if (!movie) { title: mData.Title,
return res.status(404).json({ released: mData.Released,
message: 'No such movie' webImg: mData.Poster,
}); genre: mData.Genre
} });
movie.title = req.body.title ? req.body.title : movie.title; const savedMovie = await movie.save();
movie.released = req.body.released ? req.body.released : movie.released; return res.status(201).json(savedMovie);
movie.webImg = req.body.webImg ? req.body.webImg : movie.webImg; } catch (err) {
movie.genre = req.body.genre ? req.body.genre : movie.genre; return res.status(500).json({ message: 'Error when creating movie' });
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() * movieController.delete()
*/ */
remove: function (req, res) { remove: async function (req, res) {
var id = req.params.id; var id = req.body.code;
console.log(id); const passp = req.body.pass;
MovieModel.findOneAndDelete({ code: id })
.then(movie => { try
if (!movie) { {
return res.status(404).json({ const userFound = await UserModel.findOne({pass: passp});
message: 'No such movie' if (!userFound) {
}); return res.status(404).json({ message: 'Wrong password' });
} }
return res.status(204).json();
}) const movie = await MovieModel.findOneAndDelete({ code: id });
.catch(err => { if (!movie) {
return res.status(500).json({ return res.status(404).json({ message: 'No such movie' });
message: 'Error when deleting the movie.', }
error: err
}); 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">