|
|
@@ -1,5 +1,6 @@
|
|
|
const Uploader = require("../models/uploader.js");
|
|
|
const Gallery = require("../models/gallery.js");
|
|
|
+
|
|
|
const createId = require("./createId.js");
|
|
|
|
|
|
module.exports = {
|
|
|
@@ -84,17 +85,52 @@ module.exports = {
|
|
|
/*
|
|
|
POST: updates a gallery
|
|
|
req.body = {
|
|
|
+ uploader: String
|
|
|
+ password: String
|
|
|
title: String
|
|
|
tags: String
|
|
|
location: String
|
|
|
}
|
|
|
req.files = {
|
|
|
- images: [Image]
|
|
|
+ images: undefined || Image || [Image]
|
|
|
}
|
|
|
req.params.id = String (id of gallery)
|
|
|
*/
|
|
|
updateGallery: function(req, res){
|
|
|
- console.log(req.body);
|
|
|
- console.log(req.files.images);
|
|
|
+ Promise.all([
|
|
|
+ Uploader.findOne({_id: req.body.uploader}),
|
|
|
+ Gallery.findOne({_id: req.params.id})
|
|
|
+ ])
|
|
|
+ .then((response)=>{
|
|
|
+ if(response[0] === null || response[1].owner.toString() !== req.body.uploader) throw "uploader";
|
|
|
+ if(response[0].password !== req.body.password) throw "password";
|
|
|
+
|
|
|
+ response[1].title = req.body.title;
|
|
|
+ response[1].tags = req.body.tags.split(",");
|
|
|
+ response[1].location = req.body.location.split(", ");
|
|
|
+
|
|
|
+ let handleImage = (fileData)=>{
|
|
|
+ let fileString = `/galleryImages/${createId(25)}.jpg`;
|
|
|
+ fileData.mv(`${__dirname}/..${fileString}`);
|
|
|
+ response[1].images.push(fileString);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(req.files !== null){
|
|
|
+ if(req.files.images.length === undefined) handleImage(req.files.images);
|
|
|
+ for(let i = 0; i < req.files.images.length; i++){
|
|
|
+ handleImage(req.files.images[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return response[1].save()
|
|
|
+ })
|
|
|
+ .then((gallery)=>{
|
|
|
+ return res.redirect(`/gallery/${gallery._id}`);
|
|
|
+ })
|
|
|
+ .catch((err)=>{
|
|
|
+ if(err === "uploader") return res.json("Incorrect uploader");
|
|
|
+ if(err === "password") return res.json("Incorrect password");
|
|
|
+ return res.json("ERROR: a whoopsie happened");
|
|
|
+ })
|
|
|
}
|
|
|
}
|