Podpora za srije modele in controllereje skupaj
This commit is contained in:
parent
9992dda27a
commit
6863081e9e
@ -1,5 +1,5 @@
|
|||||||
var UserModel = require('../models/userModel');
|
var UserModel = require('../models/userModel');
|
||||||
var GameModel = require('../models/gameModel');
|
var { GameModel } = require('../models/mediaModel');
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
@ -35,7 +35,7 @@ module.exports = {
|
|||||||
return res.status(409).json({ message: 'Game already exists' });
|
return res.status(409).json({ message: 'Game already exists' });
|
||||||
}
|
}
|
||||||
|
|
||||||
const uri = "https://id.twitch.tv/oauth2/token?client_id=" +userFound.client_id+ "&client_secret=" + userFound.client_secret+ "&grant_type=client_credentials";
|
const uri = "https://id.twitch.tv/oauth2/token?client_id=" + userFound.client_id + "&client_secret=" + userFound.client_secret + "&grant_type=client_credentials";
|
||||||
var response = await fetch(uri, { method: 'POST' });
|
var response = await fetch(uri, { method: 'POST' });
|
||||||
var mData = await response.json();
|
var mData = await response.json();
|
||||||
|
|
||||||
@ -59,7 +59,7 @@ module.exports = {
|
|||||||
if (gameData.length == 0) {
|
if (gameData.length == 0) {
|
||||||
return res.status(404).json({ message: 'wrong code' });
|
return res.status(404).json({ message: 'wrong code' });
|
||||||
}
|
}
|
||||||
|
|
||||||
var date = new Date(gameData[0].first_release_date * 1000);
|
var date = new Date(gameData[0].first_release_date * 1000);
|
||||||
const options = { day: 'numeric', month: 'short', year: 'numeric' }
|
const options = { day: 'numeric', month: 'short', year: 'numeric' }
|
||||||
var dateStr = date.toLocaleDateString(undefined, options);
|
var dateStr = date.toLocaleDateString(undefined, options);
|
||||||
@ -74,7 +74,7 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
const coverData = await response.json()
|
const coverData = await response.json()
|
||||||
|
|
||||||
const game = new GameModel({
|
const game = new GameModel({
|
||||||
code: gameCode,
|
code: gameCode,
|
||||||
title: gameData[0].name,
|
title: gameData[0].name,
|
||||||
|
@ -1,34 +1,36 @@
|
|||||||
var MovieModel = require('../models/movieModel');
|
|
||||||
var UserModel = require('../models/userModel');
|
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 = {
|
module.exports = {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* movieController.list()
|
* mediaController.list()
|
||||||
*/
|
*/
|
||||||
list: function (req, res) {
|
list: function (req, res) {
|
||||||
MovieModel.find({}, { _id: 0, __v: 0 })
|
const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel;
|
||||||
.then(movies => {
|
|
||||||
return res.json(movies);
|
MediaModel.find({}, { _id: 0, __v: 0 })
|
||||||
|
.then(media => {
|
||||||
|
return res.json(media);
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
message: 'Error when getting movies.',
|
message: 'Error when getting media.',
|
||||||
error: err
|
error: err
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* movieController.create()
|
* mediaController.create()
|
||||||
*/
|
*/
|
||||||
create: async function (req, res) {
|
create: async function (req, res) {
|
||||||
const movieCode = req.body.code;
|
const mediaCode = req.body.code;
|
||||||
const passp = req.body.pass;
|
const passp = req.body.pass;
|
||||||
try {
|
try {
|
||||||
|
|
||||||
@ -37,12 +39,13 @@ module.exports = {
|
|||||||
return res.status(404).json({ message: 'Wrong password' });
|
return res.status(404).json({ message: 'Wrong password' });
|
||||||
}
|
}
|
||||||
|
|
||||||
const movieFound = await MovieModel.findOne({ code: movieCode });
|
const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel;
|
||||||
if (movieFound) {
|
const mediaFound = await MediaModel.findOne({ code: mediaCode });
|
||||||
return res.status(409).json({ message: 'Movie already exists' });
|
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 mJson = await fetch(uri);
|
||||||
const mData = await mJson.json();
|
const mData = await mJson.json();
|
||||||
|
|
||||||
@ -50,22 +53,22 @@ module.exports = {
|
|||||||
return res.status(404).json({ message: 'wrong code' });
|
return res.status(404).json({ message: 'wrong code' });
|
||||||
}
|
}
|
||||||
|
|
||||||
const movie = new MovieModel({
|
const media = new MediaModel({
|
||||||
code: movieCode,
|
code: mediaCode,
|
||||||
title: mData.Title,
|
title: mData.Title,
|
||||||
released: mData.Released,
|
released: mData.Released,
|
||||||
webImg: mData.Poster,
|
webImg: mData.Poster,
|
||||||
});
|
});
|
||||||
|
|
||||||
const savedMovie = await movie.save();
|
const savedMedia = await media.save();
|
||||||
return res.status(201).json(movie);
|
return res.status(201).json(media);
|
||||||
} catch (err) {
|
} 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) {
|
remove: async function (req, res) {
|
||||||
var id = req.body.code;
|
var id = req.body.code;
|
||||||
@ -77,15 +80,16 @@ module.exports = {
|
|||||||
return res.status(404).json({ message: 'Wrong password' });
|
return res.status(404).json({ message: 'Wrong password' });
|
||||||
}
|
}
|
||||||
|
|
||||||
const movie = await MovieModel.findOneAndDelete({ code: id });
|
const MediaModel = req.baseUrl.includes('movies') ? MovieModel : SeriesModel;
|
||||||
if (!movie) {
|
const media = await MediaModel.findOneAndDelete({ code: id });
|
||||||
return res.status(404).json({ message: 'No such movie' });
|
if (!media) {
|
||||||
|
return res.status(404).json({ message: 'No such media' });
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.status(204).json();
|
return res.status(204).json();
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
return res.status(500).json({ message: 'Error when deleting the movie.' });
|
return res.status(500).json({ message: 'Error when deleting the media.' });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
@ -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
15
models/mediaModel.js
Normal 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)
|
||||||
|
};
|
@ -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);
|
|
@ -1,17 +1,13 @@
|
|||||||
|
|
||||||
|
|
||||||
var express = require('express');
|
var express = require('express');
|
||||||
var router = express.Router();
|
var router = express.Router();
|
||||||
|
|
||||||
|
|
||||||
var photosRouter = require('./photoRouter');
|
var photosRouter = require('./photoRouter');
|
||||||
var movieRouter = require('./movieRouter');
|
var mediaRouter = require('./mediaRouter');
|
||||||
var gameRouter = require('./gameRouter');
|
var gameRouter = require('./gameRouter');
|
||||||
|
|
||||||
router.use('/photos', photosRouter);
|
router.use('/photos', photosRouter);
|
||||||
router.use('/movies', movieRouter);
|
|
||||||
router.use('/games', gameRouter);
|
router.use('/games', gameRouter);
|
||||||
|
router.use('/movies', mediaRouter);
|
||||||
|
router.use('/series', mediaRouter);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
11
routes/api/mediaRouter.js
Normal file
11
routes/api/mediaRouter.js
Normal 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;
|
@ -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;
|
|
@ -7,7 +7,8 @@
|
|||||||
<header>
|
<header>
|
||||||
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
|
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
|
||||||
<div class="container-fluid">
|
<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>
|
<span class="navbar-toggler-icon"></span>
|
||||||
</button>
|
</button>
|
||||||
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
<div class="collapse navbar-collapse" id="navbarSupportedContent">
|
||||||
@ -15,6 +16,9 @@
|
|||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<button class="nav-link active" id="movieButton">Movies</button>
|
<button class="nav-link active" id="movieButton">Movies</button>
|
||||||
</li>
|
</li>
|
||||||
|
<li class="nav-item">
|
||||||
|
<button class="nav-link" id="seriesButton">Series</button>
|
||||||
|
</li>
|
||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<button class="nav-link" id="gameButton">Games</button>
|
<button class="nav-link" id="gameButton">Games</button>
|
||||||
</li>
|
</li>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user