فهرست منبع

Can now update galleries.

Lee Morgan 4 سال پیش
والد
کامیت
9c3f439274
2فایلهای تغییر یافته به همراه49 افزوده شده و 4 حذف شده
  1. 39 3
      controllers/gallery.js
  2. 10 1
      views/gallery/editGallery.html

+ 39 - 3
controllers/gallery.js

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

+ 10 - 1
views/gallery/editGallery.html

@@ -16,6 +16,14 @@
             method="post"
             enctype="multipart/form-data"
         >
+            <label>Uploader
+                <input name="uploader" type="text" required>
+            </label>
+
+            <label>Password
+                <input name="password" type="password" required>
+            </label>
+
             <label>Title
                 <input id="titleInput" name="title" type="text" required>
             </label>
@@ -46,9 +54,10 @@
                     document.getElementById("tagInput").value = gallery.tags.toString();
                     document.getElementById("locationInput").value = gallery.location.coordinates.toString().replaceAll(",", ", ");
                 })
-                .catch((err)=>{console.log(err)});
+                .catch((err)=>{});
 
             let form = document.getElementById("form");
+            form.action = `/gallery/update/${id}`;
         </script>
     </body>
 </html>