Просмотр исходного кода

Add ability to remove lectures.

Lee Morgan 5 лет назад
Родитель
Сommit
4c4c83660d
3 измененных файлов с 35 добавлено и 0 удалено
  1. 16 0
      controllers/learn.js
  2. 1 0
      routes.js
  3. 18 0
      views/learn/editLecture.html

+ 16 - 0
controllers/learn.js

@@ -186,6 +186,22 @@ module.exports = {
             });
     },
 
+    /*
+    DELETE: remove the lecture
+    req.params.id = lecture to remove
+    redirects to home
+    */
+    removeLecture: function(req, res){
+        console.log("something");
+        Lecture.deleteOne({_id: req.params.id})
+            .then((lecture)=>{
+                return res.redirect("/");
+            })
+            .catch((err)=>{
+                return res.redirect(`/learn/lectures/edit/${req.params.id}`);
+            })
+    },
+
     getCourses: function(req, res){
         Course.find()
             .then((courses)=>{

+ 1 - 0
routes.js

@@ -67,6 +67,7 @@ module.exports = function(app){
     app.post("/learn/lectures/new", learn.createLecture);
     app.get("/learn/lectures/edit/:id", (req, res)=>{res.sendFile(`${views}/learn/editLecture.html`)});
     app.post("/learn/lectures/edit/:id", learn.updateLecture);
+    app.delete("/learn/lectures/:id", learn.removeLecture);
     app.get("/learn/lectures/json/:id", learn.getLectures);
     app.get("/learn/lectures/:id", (req, res)=>{res.sendFile(`${views}/learn/lecture.html`)});
     app.get("/learn/lectures/json/one/:id", learn.getLecture);

+ 18 - 0
views/learn/editLecture.html

@@ -8,6 +8,12 @@
                 display: flex;
                 flex-direction: column;
             }
+
+            #deleteButton{
+                background: red;
+                color: white;
+                margin-top: 100px;
+            }
         </style>
     </head>
     <body>
@@ -62,6 +68,8 @@
             <input type="submit" value="Submit">
         </form>
 
+        <button id="deleteButton">DELETE</button>
+
         <script>
             let id = window.location.href.split("/");
             id = id[id.length-1];
@@ -128,6 +136,16 @@
 
                     form.submit();
                 }
+
+                document.getElementById("deleteButton").onclick = ()=>{
+                    fetch(`/learn/lectures/${id}`,{
+                        method: "delete"
+                    })
+                        .then((response)=>{
+                            window.location.href;
+                        })
+                        .catch((err)=>{});
+                }
         </script>
     </body>
 </html>