Update to movie controller and list
This commit is contained in:
parent
d46c4bba61
commit
2c14f5f45d
@ -1,4 +1,5 @@
|
||||
var MovieModel = require('../models/movieModel');
|
||||
var UserModel = require('../models/userModel');
|
||||
|
||||
/**
|
||||
* movieController.js
|
||||
@ -26,99 +27,68 @@ module.exports = {
|
||||
/**
|
||||
* 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
|
||||
});
|
||||
create: async function (req, res) {
|
||||
const movieCode = req.body.code;
|
||||
const passp = req.body.pass;
|
||||
try {
|
||||
|
||||
MovieModel.findOne({ code: movie.code })
|
||||
.then(movieFound => {
|
||||
const userFound = await UserModel.findOne({ pass: passp });
|
||||
if (!userFound) {
|
||||
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'
|
||||
});
|
||||
return res.status(409).json({ message: 'Movie already exists' });
|
||||
}
|
||||
else
|
||||
|
||||
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
|
||||
{
|
||||
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
|
||||
});
|
||||
});
|
||||
const userFound = await UserModel.findOne({pass: passp});
|
||||
if (!userFound) {
|
||||
return res.status(404).json({ message: 'Wrong password' });
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
return res.status(500).json({
|
||||
message: 'Error when getting movie',
|
||||
error: err
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* movieController.update()
|
||||
*/
|
||||
update: function (req, res) {
|
||||
var id = req.params.id;
|
||||
|
||||
MovieModel.findOne({ _id: id })
|
||||
.then(movie => {
|
||||
const movie = await MovieModel.findOneAndDelete({ code: id });
|
||||
if (!movie) {
|
||||
return res.status(404).json({
|
||||
message: 'No such 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;
|
||||
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
|
||||
});
|
||||
});
|
||||
}
|
||||
catch(err)
|
||||
{
|
||||
return res.status(500).json({ message: 'Error when deleting the movie.' });
|
||||
}
|
||||
},
|
||||
};
|
||||
|
9
models/userModel.js
Normal file
9
models/userModel.js
Normal 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);
|
@ -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;
|
22
routes/list.js
Normal file
22
routes/list.js
Normal 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;
|
@ -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;
|
@ -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="container" id="movie_tbody">
|
Loading…
x
Reference in New Issue
Block a user