remove photo

This commit is contained in:
Nikola Petrov 2024-07-15 19:09:28 +02:00
parent 1f7eb09651
commit 2f68098f62
7 changed files with 3 additions and 173 deletions

View File

@ -1,125 +0,0 @@
var PhotoModel = require('../models/photoModel.js');
/**
* photoController.js
*
* @description :: Server-side logic for managing photos.
*/
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.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);
});
},
/**
* photoController.create()
*/
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
});
});
},
/**
* 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,9 +0,0 @@
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var photoSchema = new Schema({
'name': String,
'path': String
});
module.exports = mongoose.model('photo', photoSchema);

View File

@ -9,10 +9,11 @@
"dependencies": {
"@mi-sec/wol": "^1.0.13",
"express": "^4.18.2",
"@types/express": "^4.17.21",
"hbs": "^4.2.0",
"http": "^0.0.1-security",
"mongoose": "^7.5.2",
"morgan": "~1.9.1",
"multer": "^1.4.5-lts.1"
"@types/morgan": "^1.9.9"
}
}
}

View File

@ -2,12 +2,10 @@ var express = require('express');
var router = express.Router();
const checkAuthenticated = require('../../middleware/checkAuthenticated.js');
var photosRouter = require('./photoRouter');
var mediaRouter = require('./mediaRouter');
var gameRouter = require('./gameRouter');
var cashTransactionRouter = require('./cashTransactionRouter');
router.use('/photos', photosRouter);
router.use('/games', gameRouter);
router.use('/movies', mediaRouter);
router.use('/series', mediaRouter);

View File

@ -1,20 +0,0 @@
var express = require('express');
// Vključimo multer za file upload
var multer = require('multer');
var upload = multer({ dest: 'public/UpImage/' });
var router = express.Router();
var photoController = require('../../controllers/photoController.js');
router.get('/', photoController.list);
router.get('/publish', photoController.publish);
router.get('/:id', photoController.show);
router.post('/', upload.single('image'), photoController.create);
router.put('/:id', photoController.update);
router.delete('/:id', photoController.remove);
module.exports = router;

View File

@ -1,7 +0,0 @@
<h1>Slike:</h1>
{{#each photos}}
<h2>slika:</h2>
<img src="{{path}}" title="{{name}}" width="400"/><br/>
Id: <span>{{id}}</span>
<hr/>
{{/each}}

View File

@ -1,8 +0,0 @@
<h1>Dodaj sliko</h1>
<form action="/photos" method="post" enctype="multipart/form-data">
<input type="text" name="name" placeholder="ime slike" required>
<input type="file" name="image">
<div class="tp">
<input type="submit" value="Dodaj">
</div>
</form>