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:'));
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);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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/');
});

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;