Podpora za srije modele in controllereje skupaj

This commit is contained in:
Nikola Petrov 2023-08-10 15:05:50 +02:00
parent 9992dda27a
commit 6863081e9e
9 changed files with 66 additions and 69 deletions

View File

@ -1,5 +1,5 @@
var UserModel = require('../models/userModel');
var GameModel = require('../models/gameModel');
var { GameModel } = require('../models/mediaModel');
module.exports = {

View File

@ -1,34 +1,36 @@
var MovieModel = require('../models/movieModel');
var UserModel = require('../models/userModel');
var { MovieModel, SeriesModel } = require('../models/mediaModel');
/**
* movieController.js
* mediaController.js
*
* @description :: Server-side logic for managing movies.
* @description :: Server-side logic for managing movies and series.
*/
module.exports = {
/**
* movieController.list()
* mediaController.list()
*/
list: function (req, res) {
MovieModel.find({}, { _id: 0, __v: 0 })
.then(movies => {
return res.json(movies);
const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel;
MediaModel.find({}, { _id: 0, __v: 0 })
.then(media => {
return res.json(media);
})
.catch(err => {
return res.status(500).json({
message: 'Error when getting movies.',
message: 'Error when getting media.',
error: err
});
});
},
/**
* movieController.create()
* mediaController.create()
*/
create: async function (req, res) {
const movieCode = req.body.code;
const mediaCode = req.body.code;
const passp = req.body.pass;
try {
@ -37,12 +39,13 @@ module.exports = {
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' });
const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel;
const mediaFound = await MediaModel.findOne({ code: mediaCode });
if (mediaFound) {
return res.status(409).json({ message: 'Media already exists' });
}
const uri = `http://www.omdbapi.com/?i=${movieCode}&apikey=${userFound.key}`;
const uri = `http://www.omdbapi.com/?i=${mediaCode}&apikey=${userFound.key}`;
const mJson = await fetch(uri);
const mData = await mJson.json();
@ -50,22 +53,22 @@ module.exports = {
return res.status(404).json({ message: 'wrong code' });
}
const movie = new MovieModel({
code: movieCode,
const media = new MediaModel({
code: mediaCode,
title: mData.Title,
released: mData.Released,
webImg: mData.Poster,
});
const savedMovie = await movie.save();
return res.status(201).json(movie);
const savedMedia = await media.save();
return res.status(201).json(media);
} catch (err) {
return res.status(500).json({ message: 'Error when creating movie' });
return res.status(500).json({ message: 'Error when creating media' });
}
},
/**
* movieController.delete()
* mediaController.delete()
*/
remove: async function (req, res) {
var id = req.body.code;
@ -77,15 +80,16 @@ module.exports = {
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' });
const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel;
const media = await MediaModel.findOneAndDelete({ code: id });
if (!media) {
return res.status(404).json({ message: 'No such media' });
}
return res.status(204).json();
}
catch (err) {
return res.status(500).json({ message: 'Error when deleting the movie.' });
return res.status(500).json({ message: 'Error when deleting the media.' });
}
},
};

View File

@ -1,11 +0,0 @@
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var gameSchema = new Schema({
'title': String,
'released': String,
'webImg': String,
'code': String
});
module.exports = mongoose.model('game', gameSchema);

15
models/mediaModel.js Normal file
View File

@ -0,0 +1,15 @@
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var mediaSchema = new Schema({
'title': String,
'released': String,
'webImg': String,
'code': String
});
module.exports = {
MovieModel: mongoose.model('movie', mediaSchema),
SeriesModel: mongoose.model('series', mediaSchema),
GameModel: mongoose.model('game', mediaSchema)
};

View File

@ -1,11 +0,0 @@
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var movieSchema = new Schema({
'title': String,
'released': String,
'webImg': String,
'code': String
});
module.exports = mongoose.model('movie', movieSchema);

View File

@ -1,17 +1,13 @@
var express = require('express');
var router = express.Router();
var photosRouter = require('./photoRouter');
var movieRouter = require('./movieRouter');
var mediaRouter = require('./mediaRouter');
var gameRouter = require('./gameRouter');
router.use('/photos', photosRouter);
router.use('/movies', movieRouter);
router.use('/games', gameRouter);
router.use('/movies', mediaRouter);
router.use('/series', mediaRouter);
module.exports = router;

11
routes/api/mediaRouter.js Normal file
View File

@ -0,0 +1,11 @@
var express = require('express');
var router = express.Router();
var mediaController = require('../../controllers/mediaController.js');
router.get('/', mediaController.list);
router.post('/', mediaController.create);
router.delete('/', mediaController.remove);
module.exports = router;

View File

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

View File

@ -7,7 +7,8 @@
<header>
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<div class="container-fluid">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
@ -15,6 +16,9 @@
<li class="nav-item">
<button class="nav-link active" id="movieButton">Movies</button>
</li>
<li class="nav-item">
<button class="nav-link" id="seriesButton">Series</button>
</li>
<li class="nav-item">
<button class="nav-link" id="gameButton">Games</button>
</li>