diff --git a/app.js b/app.js index 5489400..0a8e184 100644 --- a/app.js +++ b/app.js @@ -27,13 +27,6 @@ var db = mongoose.connection; 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 listRouter = require('./routes/list'); -var photosRouter = require('./routes/api/photoRouter'); -var movieRouter = require('./routes/api/movieRouter'); -var mailRouter = require('./routes/mail'); - var app = express(); const httpServer = http.createServer(app); @@ -49,20 +42,6 @@ app.use(express.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); - -/** - * Vključimo session in connect-mongo. - * Connect-mongo skrbi, da se session hrani v bazi. - * Posledično ostanemo prijavljeni, tudi ko spremenimo kodo (restartamo strežnik) - */ -var session = require('express-session'); -var MongoStore = require('connect-mongo'); -app.use(session({ - secret: 'work hard', - resave: true, - saveUninitialized: false, - store: MongoStore.create({ mongoUrl: mongoDB }) -})); //Shranimo sejne spremenljivke v locals //Tako lahko do njih dostopamo v vseh view-ih (glej layout.hbs) app.use(function (req, res, next) { @@ -70,10 +49,13 @@ app.use(function (req, res, next) { next(); }); +var mainRouter = require('./routes/main'); +var listRouter = require('./routes/list'); +var photosRouter = require('./routes/api/photoRouter'); +var movieRouter = require('./routes/api/movieRouter'); +var mailRouter = require('./routes/mail'); - -app.use('/', newRouter); -app.use('/old', oldRouter); +app.use('/', mainRouter); app.use('/list', listRouter); app.use('/api/photos', photosRouter); app.use('/api/movies', movieRouter); diff --git a/controllers/movieController.js b/controllers/movieController.js index 2687c27..5fac838 100644 --- a/controllers/movieController.js +++ b/controllers/movieController.js @@ -12,15 +12,15 @@ module.exports = { * movieController.list() */ list: function (req, res) { - MovieModel.find({}, {_id: 0, __v: 0}) + MovieModel.find({}, { _id: 0, __v: 0 }) .then(movies => { - return res.json(movies); + return res.json(movies); }) .catch(err => { - return res.status(500).json({ - message: 'Error when getting movies.', - error: err - }); + return res.status(500).json({ + message: 'Error when getting movies.', + error: err + }); }); }, @@ -31,7 +31,7 @@ module.exports = { const movieCode = req.body.code; const passp = req.body.pass; try { - + const userFound = await UserModel.findOne({ pass: passp }); if (!userFound) { return res.status(404).json({ message: 'Wrong password' }); @@ -46,8 +46,8 @@ module.exports = { const mJson = await fetch(uri); const mData = await mJson.json(); - if(mData.Response == 'False'){ - return res.status(404).json({message: 'wrong code'}); + if (mData.Response == 'False') { + return res.status(404).json({ message: 'wrong code' }); } const movie = new MovieModel({ @@ -55,7 +55,6 @@ module.exports = { title: mData.Title, released: mData.Released, webImg: mData.Poster, - genre: mData.Genre }); const savedMovie = await movie.save(); @@ -69,26 +68,24 @@ module.exports = { * movieController.delete() */ remove: async function (req, res) { - var id = req.body.code; - const passp = req.body.pass; + var id = req.body.code; + const passp = req.body.pass; - try - { - const userFound = await UserModel.findOne({pass: passp}); - if (!userFound) { - 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' }); - } - - return res.status(204).json(); + try { + 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 deleting the movie.' }); + + const movie = await MovieModel.findOneAndDelete({ code: id }); + 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.' }); + } }, }; diff --git a/controllers/photoController.js b/controllers/photoController.js index 46b5980..2d3ecd0 100644 --- a/controllers/photoController.js +++ b/controllers/photoController.js @@ -7,119 +7,119 @@ var PhotoModel = require('../models/photoModel.js'); */ module.exports = { - /** - * photoController.list() - */ - list: function (req, res) { - PhotoModel.find() - .then(photos => { - var data = { - photos: photos - }; - return res.render('photo/list', data); - }) - .catch(err => { - return res.status(500).json({ - message: 'Error when getting photos.', - error: err - }); + /** + * photoController.list() + */ + list: function (req, res) { + PhotoModel.find() + .then(photos => { + var data = { + photos: photos + }; + return res.render('photo/list', data); + }) + .catch(err => { + return res.status(500).json({ + message: 'Error when getting photos.', + error: err }); - }, + }); + }, - /** - * photoController.show() - */ - show: function (req, res) { - var id = req.params.id; + /** + * photoController.show() + */ + show: function (req, res) { + var id = req.params.id; - PhotoModel.findOne({_id: id}, function (err, photo) { - if (err) { - return res.status(500).json({ - message: 'Error when getting photo.', - error: err - }); - } - - if (!photo) { - return res.status(404).json({ - message: 'No such photo' - }); - } - - return res.json(photo); + PhotoModel.findOne({ _id: id }, function (err, photo) { + if (err) { + return res.status(500).json({ + message: 'Error when getting photo.', + error: err }); - }, + } - /** - * photoController.create() - */ - create: function (req, res) { - var photo = new PhotoModel({ - name: req.body.name, - path: "/UpImage/" + req.file.filename, + if (!photo) { + return res.status(404).json({ + message: 'No such photo' }); - - photo.save() - .then(savedPhoto => { - // Handle successful save operation - return res.redirect('/photos'); - }) - .catch(err => { - return res.status(500).json({ - message: 'Error when creating photo', - error: err - }); - }); - }, + } - /** - * photoController.update() - */ - update: function (req, res) { - var id = req.params.id; - - PhotoModel.findOne({ _id: id }) - .then(photo => { - if (!photo) { - return res.status(404).json({ - message: 'No such photo' - }); - } - - photo.name = req.body.name ? req.body.name : photo.name; - photo.path = req.body.path ? req.body.path : photo.path; - return photo.save(); - }) - .then(updatedPhoto => { - return res.json(updatedPhoto); - }) - .catch(err => { - return res.status(500).json({ - message: 'Error when updating photo.', - error: err - }); - }); - }, + return res.json(photo); + }); + }, - /** - * photoController.remove() - */ - remove: function (req, res) { - var id = req.params.id; + /** + * photoController.create() + */ + create: function (req, res) { + var photo = new PhotoModel({ + name: req.body.name, + path: "/UpImage/" + req.file.filename, + }); - PhotoModel.findByIdAndRemove(id, function (err, photo) { - if (err) { - return res.status(500).json({ - message: 'Error when deleting the photo.', - error: err - }); - } - - return res.status(204).json(); + photo.save() + .then(savedPhoto => { + // Handle successful save operation + return res.redirect('/photos'); + }) + .catch(err => { + return res.status(500).json({ + message: 'Error when creating photo', + error: err }); - }, + }); + }, - publish: function(req, res){ - return res.render('photo/publish'); - } + /** + * photoController.update() + */ + update: function (req, res) { + var id = req.params.id; + + PhotoModel.findOne({ _id: id }) + .then(photo => { + if (!photo) { + return res.status(404).json({ + message: 'No such photo' + }); + } + + photo.name = req.body.name ? req.body.name : photo.name; + photo.path = req.body.path ? req.body.path : photo.path; + return photo.save(); + }) + .then(updatedPhoto => { + return res.json(updatedPhoto); + }) + .catch(err => { + return res.status(500).json({ + message: 'Error when updating photo.', + error: err + }); + }); + }, + + /** + * photoController.remove() + */ + remove: function (req, res) { + var id = req.params.id; + + PhotoModel.findByIdAndRemove(id, function (err, photo) { + if (err) { + return res.status(500).json({ + message: 'Error when deleting the photo.', + error: err + }); + } + + return res.status(204).json(); + }); + }, + + publish: function (req, res) { + return res.render('photo/publish'); + } }; diff --git a/models/movieModel.js b/models/movieModel.js index 5256a51..3d4927a 100644 --- a/models/movieModel.js +++ b/models/movieModel.js @@ -1,12 +1,11 @@ var mongoose = require('mongoose'); -var Schema = mongoose.Schema; +var Schema = mongoose.Schema; var movieSchema = new Schema({ - 'title' : String, - 'released' : String, - 'webImg' : String, - 'genre' : String, - 'code' : String + 'title': String, + 'released': String, + 'webImg': String, + 'code': String }); module.exports = mongoose.model('movie', movieSchema); \ No newline at end of file diff --git a/models/photoModel.js b/models/photoModel.js index 85a1fc8..3610e01 100644 --- a/models/photoModel.js +++ b/models/photoModel.js @@ -1,9 +1,9 @@ var mongoose = require('mongoose'); -var Schema = mongoose.Schema; +var Schema = mongoose.Schema; var photoSchema = new Schema({ - 'name' : String, - 'path' : String + 'name': String, + 'path': String }); module.exports = mongoose.model('photo', photoSchema); diff --git a/models/userModel.js b/models/userModel.js index 7388421..af1c088 100644 --- a/models/userModel.js +++ b/models/userModel.js @@ -1,9 +1,9 @@ var mongoose = require('mongoose'); -var Schema = mongoose.Schema; +var Schema = mongoose.Schema; var userSchema = new Schema({ - 'pass' : String, - 'key' : String, + 'pass': String, + 'key': String, }); module.exports = mongoose.model('user', userSchema); \ No newline at end of file diff --git a/routes/api/photoRouter.js b/routes/api/photoRouter.js index c80a3e6..22fc3ee 100644 --- a/routes/api/photoRouter.js +++ b/routes/api/photoRouter.js @@ -1,7 +1,7 @@ var express = require('express'); // Vključimo multer za file upload var multer = require('multer'); -var upload = multer({dest: 'public/UpImage/'}); +var upload = multer({ dest: 'public/UpImage/' }); var router = express.Router(); var photoController = require('../../controllers/photoController.js'); diff --git a/routes/list.js b/routes/list.js index 07e5386..c899cfe 100644 --- a/routes/list.js +++ b/routes/list.js @@ -3,13 +3,13 @@ const fs = require('fs'); var router = express.Router(); /* GET home page. */ -router.get('/', function(req, res, next) { +router.get('/', function (req, res, next) { - data = { + data = { title: 'Movies', scripts: [] }; - + const f = fs.readdirSync('./public/assets/list/'); f.forEach(file => { data.scripts.push("/assets/list/" + file); diff --git a/routes/mail.js b/routes/mail.js index 52352a2..9507979 100644 --- a/routes/mail.js +++ b/routes/mail.js @@ -3,7 +3,7 @@ var express = require('express'); var router = express.Router(); /* GET home page. */ -router.get('/', function(req, res, next) { +router.get('/', function (req, res, next) { res.redirect('https://privateemail.com/'); }); diff --git a/routes/main.js b/routes/main.js new file mode 100644 index 0000000..c6835a3 --- /dev/null +++ b/routes/main.js @@ -0,0 +1,13 @@ +var express = require('express'); +var router = express.Router(); + +/* GET home page. */ +router.get('/', function (req, res, next) { + res.render('main/2_0', { title: 'Nikola Petrov', disableBootStrap: true }); +}); + +router.get('/old', function (req, res, next) { + res.render('main/1_0', { title: 'Nikola Petrov' }); +}); + +module.exports = router; \ No newline at end of file diff --git a/routes/main/1_0.js b/routes/main/1_0.js deleted file mode 100644 index 4b0cfdb..0000000 --- a/routes/main/1_0.js +++ /dev/null @@ -1,9 +0,0 @@ -var express = require('express'); -var router = express.Router(); - -/* GET home page. */ -router.get('/', function(req, res, next) { - res.render('main/1_0', {title:'Nikola Petrov'}); -}); - -module.exports = router; diff --git a/routes/main/2_0.js b/routes/main/2_0.js deleted file mode 100644 index d0e50c3..0000000 --- a/routes/main/2_0.js +++ /dev/null @@ -1,9 +0,0 @@ -var express = require('express'); -var router = express.Router(); - -/* GET home page. */ -router.get('/', function(req, res, next) { - res.render('main/2_0', {title:'Nikola Petrov', disableBootStrap: true}); -}); - -module.exports = router; \ No newline at end of file