Dodal movie data

This commit is contained in:
Nikola Petrov 2023-06-25 01:48:32 +02:00
parent 845a74dd49
commit 0d6c2d0aa8
8 changed files with 164 additions and 3 deletions

6
app.js
View File

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

View File

@ -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();
});
},
};

View File

@ -12,7 +12,6 @@ module.exports = {
*/
list: function (req, res) {
PhotoModel.find()
.exec()
.then(photos => {
var data = {
photos: photos

12
models/movieModel.js Normal file
View File

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

19
routes/movieRouter.js Normal file
View File

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

View File

@ -3,6 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<title>Document</title>
<style>
body{

View File

@ -15,6 +15,7 @@
<script defer src="/1_0/assets/WinBox/winbox.bundle.js"></script>
<script defer src="/1_0/js/main.js"></script>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
</head>
<body>