This commit is contained in:
Nikola Petrov 2023-08-02 14:27:35 +02:00
parent 96099f751b
commit f240d4e7c5
12 changed files with 164 additions and 191 deletions

30
app.js
View File

@ -27,13 +27,6 @@ var db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:')); 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(); var app = express();
const httpServer = http.createServer(app); const httpServer = http.createServer(app);
@ -49,20 +42,6 @@ app.use(express.urlencoded({ extended: false }));
app.use(cookieParser()); app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public'))); 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 //Shranimo sejne spremenljivke v locals
//Tako lahko do njih dostopamo v vseh view-ih (glej layout.hbs) //Tako lahko do njih dostopamo v vseh view-ih (glej layout.hbs)
app.use(function (req, res, next) { app.use(function (req, res, next) {
@ -70,10 +49,13 @@ app.use(function (req, res, next) {
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('/', mainRouter);
app.use('/', newRouter);
app.use('/old', oldRouter);
app.use('/list', listRouter); app.use('/list', listRouter);
app.use('/api/photos', photosRouter); app.use('/api/photos', photosRouter);
app.use('/api/movies', movieRouter); app.use('/api/movies', movieRouter);

View File

@ -12,15 +12,15 @@ module.exports = {
* movieController.list() * movieController.list()
*/ */
list: function (req, res) { list: function (req, res) {
MovieModel.find({}, {_id: 0, __v: 0}) MovieModel.find({}, { _id: 0, __v: 0 })
.then(movies => { .then(movies => {
return res.json(movies); return res.json(movies);
}) })
.catch(err => { .catch(err => {
return res.status(500).json({ return res.status(500).json({
message: 'Error when getting movies.', message: 'Error when getting movies.',
error: err error: err
}); });
}); });
}, },
@ -31,7 +31,7 @@ module.exports = {
const movieCode = req.body.code; const movieCode = req.body.code;
const passp = req.body.pass; const passp = req.body.pass;
try { try {
const userFound = await UserModel.findOne({ pass: passp }); const userFound = await UserModel.findOne({ pass: passp });
if (!userFound) { if (!userFound) {
return res.status(404).json({ message: 'Wrong password' }); return res.status(404).json({ message: 'Wrong password' });
@ -46,8 +46,8 @@ module.exports = {
const mJson = await fetch(uri); const mJson = await fetch(uri);
const mData = await mJson.json(); const mData = await mJson.json();
if(mData.Response == 'False'){ if (mData.Response == 'False') {
return res.status(404).json({message: 'wrong code'}); return res.status(404).json({ message: 'wrong code' });
} }
const movie = new MovieModel({ const movie = new MovieModel({
@ -55,7 +55,6 @@ module.exports = {
title: mData.Title, title: mData.Title,
released: mData.Released, released: mData.Released,
webImg: mData.Poster, webImg: mData.Poster,
genre: mData.Genre
}); });
const savedMovie = await movie.save(); const savedMovie = await movie.save();
@ -69,26 +68,24 @@ module.exports = {
* movieController.delete() * movieController.delete()
*/ */
remove: async function (req, res) { remove: async function (req, res) {
var id = req.body.code; var id = req.body.code;
const passp = req.body.pass; const passp = req.body.pass;
try try {
{ const userFound = await UserModel.findOne({ pass: passp });
const userFound = await UserModel.findOne({pass: passp}); if (!userFound) {
if (!userFound) { return res.status(404).json({ message: 'Wrong password' });
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();
} }
catch(err)
{ const movie = await MovieModel.findOneAndDelete({ code: id });
return res.status(500).json({ message: 'Error when deleting the 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.' });
}
}, },
}; };

View File

@ -7,119 +7,119 @@ var PhotoModel = require('../models/photoModel.js');
*/ */
module.exports = { module.exports = {
/** /**
* photoController.list() * photoController.list()
*/ */
list: function (req, res) { list: function (req, res) {
PhotoModel.find() PhotoModel.find()
.then(photos => { .then(photos => {
var data = { var data = {
photos: photos photos: photos
}; };
return res.render('photo/list', data); return res.render('photo/list', data);
}) })
.catch(err => { .catch(err => {
return res.status(500).json({ return res.status(500).json({
message: 'Error when getting photos.', message: 'Error when getting photos.',
error: err error: err
});
}); });
}, });
},
/** /**
* photoController.show() * photoController.show()
*/ */
show: function (req, res) { show: function (req, res) {
var id = req.params.id; var id = req.params.id;
PhotoModel.findOne({_id: id}, function (err, photo) { PhotoModel.findOne({ _id: id }, function (err, photo) {
if (err) { if (err) {
return res.status(500).json({ return res.status(500).json({
message: 'Error when getting photo.', message: 'Error when getting photo.',
error: err error: err
});
}
if (!photo) {
return res.status(404).json({
message: 'No such photo'
});
}
return res.json(photo);
}); });
}, }
/** if (!photo) {
* photoController.create() return res.status(404).json({
*/ message: 'No such photo'
create: function (req, res) {
var photo = new PhotoModel({
name: req.body.name,
path: "/UpImage/" + req.file.filename,
}); });
}
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
});
});
},
/** return res.json(photo);
* 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() * photoController.create()
*/ */
remove: function (req, res) { create: function (req, res) {
var id = req.params.id; var photo = new PhotoModel({
name: req.body.name,
path: "/UpImage/" + req.file.filename,
});
PhotoModel.findByIdAndRemove(id, function (err, photo) { photo.save()
if (err) { .then(savedPhoto => {
return res.status(500).json({ // Handle successful save operation
message: 'Error when deleting the photo.', return res.redirect('/photos');
error: err })
}); .catch(err => {
} return res.status(500).json({
message: 'Error when creating photo',
return res.status(204).json(); 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');
}
}; };

View File

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

View File

@ -1,9 +1,9 @@
var mongoose = require('mongoose'); var mongoose = require('mongoose');
var Schema = mongoose.Schema; var Schema = mongoose.Schema;
var photoSchema = new Schema({ var photoSchema = new Schema({
'name' : String, 'name': String,
'path' : String 'path': String
}); });
module.exports = mongoose.model('photo', photoSchema); module.exports = mongoose.model('photo', photoSchema);

View File

@ -1,9 +1,9 @@
var mongoose = require('mongoose'); var mongoose = require('mongoose');
var Schema = mongoose.Schema; var Schema = mongoose.Schema;
var userSchema = new Schema({ var userSchema = new Schema({
'pass' : String, 'pass': String,
'key' : String, 'key': String,
}); });
module.exports = mongoose.model('user', userSchema); module.exports = mongoose.model('user', userSchema);

View File

@ -1,7 +1,7 @@
var express = require('express'); var express = require('express');
// Vključimo multer za file upload // Vključimo multer za file upload
var multer = require('multer'); var multer = require('multer');
var upload = multer({dest: 'public/UpImage/'}); var upload = multer({ dest: 'public/UpImage/' });
var router = express.Router(); var router = express.Router();
var photoController = require('../../controllers/photoController.js'); var photoController = require('../../controllers/photoController.js');

View File

@ -3,13 +3,13 @@ const fs = require('fs');
var router = express.Router(); var router = express.Router();
/* GET home page. */ /* GET home page. */
router.get('/', function(req, res, next) { router.get('/', function (req, res, next) {
data = { data = {
title: 'Movies', title: 'Movies',
scripts: [] scripts: []
}; };
const f = fs.readdirSync('./public/assets/list/'); const f = fs.readdirSync('./public/assets/list/');
f.forEach(file => { f.forEach(file => {
data.scripts.push("/assets/list/" + file); data.scripts.push("/assets/list/" + file);

View File

@ -3,7 +3,7 @@ var express = require('express');
var router = express.Router(); var router = express.Router();
/* GET home page. */ /* GET home page. */
router.get('/', function(req, res, next) { router.get('/', function (req, res, next) {
res.redirect('https://privateemail.com/'); res.redirect('https://privateemail.com/');
}); });

13
routes/main.js Normal file
View File

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

View File

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

View File

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