Эх сурвалжийг харах

Add ability to upload a new currency.

Lee Morgan 4 жил өмнө
parent
commit
a6d16d510a

+ 58 - 1
controllers/currency.js

@@ -1,10 +1,67 @@
 const Currency = require("../models/currency.js");
+const Uploader = require("../models/uploader.js");
+
+const createId = require("./createId.js");
 
 module.exports = {
+    /*
+    POST: create a new currency
+    req.body = {
+        uploader: String (id of owner)
+        password: String
+        location: String
+        type: String (paper/coin)
+        year: Number
+        comment: String
+    }
+    req.files = {
+        frontImage: Image
+        backImage: Image
+    }
+    redirect: /currency
+    */
+    create: function(req, res){
+        Uploader.findOne({_id: req.body.uploader})
+            .then((uploader)=>{
+                if(!uploader) throw "uploader";
+                if(req.body.password !== uploader.password) throw "pass";
+
+                let createImage = (image)=>{
+                    console.log(image);
+                    let fileString = `/currency/${createId(25)}.jpg`;
+                    image.mv(`${__dirname}/..${fileString}`);
+                    return fileString;
+                }
+
+                let currency = new Currency({
+                    location: req.body.location,
+                    type: req.body.type,
+                    year: req.body.year,
+                    comment: req.body.comment,
+                    frontImage: createImage(req.files.frontImage),
+                    backImage: createImage(req.files.backImage)
+                });
+
+                return currency.save();
+            })
+            .then((currency)=>{
+                return res.redirect("/currency");
+            })
+            .catch((err)=>{
+                switch(err){
+                    case "uploader": return res.redirect("/");
+                    case "pass": return res.redirect("/");
+                    default:
+                        console.error(err);
+                        return res.redirect("/");
+                }
+            });
+    },
+
     currency: function(req, res){
         Currency.find()
             .then((currencies)=>{
-                return res.render("currency/display.ejs");
+                return res.render("currency/display.html");
             })
             .catch((err)=>{
                 console.error(err);

+ 1 - 0
controllers/gallery.js

@@ -51,6 +51,7 @@ module.exports = {
                 return res.redirect(`/gallery/${gallery._id}`);
             })
             .catch((err)=>{
+                console.error(err);
                 if(err === "uploader") return res.json("You do not have permission to upload");
                 if(err === "pass") return res.json("Incorrect password");
                 return res.json("ERROR: something went wrong");

BIN
currency/L70jYgr0XXZaExIpXFHmDjvqU.jpg


BIN
currency/O0pQrNmuvG1hjiv8xTLWvK1I4.jpg


BIN
currency/afoiRqbCBBpBP1CO4krjSxpft.jpg


BIN
currency/fSkmmsgI0FxF1bAAx4e8S5gwE.jpg


+ 2 - 1
models/currency.js

@@ -5,7 +5,8 @@ const CurrencySchema = new mongoose.Schema({
     frontImage: String,
     backImage: String,
     type: String,
-    year: Number
+    year: Number,
+    comment: String
 });
 
 module.exports = mongoose.model("currency", CurrencySchema);

+ 3 - 1
routes.js

@@ -90,8 +90,10 @@ module.exports = function(app){
 
     //CURRENCY
     app.get("/currency/style", (req, res)=>res.sendFile(`${views}/currency/currency.css`));
-    app.get("/currency", visit, currency.currency);
     app.get("/currency/new", (req, res)=>{res.sendFile(`${views}/currency/new.html`)});
+    
+    app.get("/currency", visit, currency.currency);
+    app.post("/currency", currency.create);
 
     //CONTENT
     app.get("/thumbNails/*", (req, res)=>{res.sendFile(`${__dirname}${req.url}`)});

+ 0 - 0
views/currency/display.ejs → views/currency/display.html


+ 14 - 2
views/currency/new.html

@@ -11,9 +11,17 @@
         </style>
     </head>
     <body>
-        <form action="/currency" method="post" ecntype="multipart/form-data">
+        <form action="/currency" 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>Location
-                <input name="location" type="text" placeholder="Location" required>
+                <input name="location" type="text" required>
             </label>
 
             <label>Paper
@@ -28,6 +36,10 @@
                 <input name="year" type="number" required>
             </label>
 
+            <label>Comments
+                <textarea name="comment"></textarea>
+            </label>
+
             <label>Front Image
                 <input name="frontImage" type="file" required>
             </label>