Procházet zdrojové kódy

Add backend for updating lectures.

Lee Morgan před 5 roky
rodič
revize
316839f72a
2 změnil soubory, kde provedl 50 přidání a 2 odebrání
  1. 50 1
      controllers/learn.js
  2. 0 1
      models/lecture.js

+ 50 - 1
controllers/learn.js

@@ -124,7 +124,7 @@ module.exports = {
                 return lecture.save();
             })
             .then((lecture)=>{
-                return res.redirect("/");
+                return res.redirect(`/learn/lectures/${lecture._id}`);
             })
             .catch((err)=>{
                 if(err === "noCourse") return res.json("Course does not exist");
@@ -134,6 +134,55 @@ module.exports = {
             });
     },
 
+    /*
+    POST: updates a lecture
+    req.body = {
+        course: String (id)
+        title: String
+        video: String
+        description: String
+        furtherReading: String (\r\n delimited)
+        exercises; String (\r\n delimited)
+        date: Date
+    }
+    req.params.id = String (lecture id)
+    */
+    updateLecture: function(req, res){
+        Lecture.findOne({_id: req.params.id})
+            .then((lecture)=>{
+                if(lecture === null) throw "notFound";
+
+                let furtherReading = req.body.furtherReading.split("\r\n");
+                furtherReading.splice(furtherReading.length - 1, 1);
+                let readings = [];
+                for(let i = 0; i < furtherReading.length; i+=2){
+                    readings.push({
+                        text: furtherReading[i],
+                        link: furtherReading[i+1]
+                    });
+                }
+
+                let exercises = req.body.exercises.split("\r\n");
+                exercises.splice(exercises.length - 1, 1);
+
+                lecture.course = req.body.course;
+                lecture.title = req.body.title;
+                lecture.video = req.body.video;
+                lecture.description = req.body.description;
+                lecture.furtherReading = readings;
+                lecture.exercises = exercises;
+                lecture.date = new Date(req.body.date);
+
+                return lecture.save();
+            })
+            .then((lecture)=>{
+                return res.redirect(`/learn/lectures/${lecture._id}`);
+            })
+            .catch((err)=>{
+                return res.redirect(`/learn/lectures/edit/${lecture._id}`);
+            });
+    },
+
     getCourses: function(req, res){
         Course.find()
             .then((courses)=>{

+ 0 - 1
models/lecture.js

@@ -27,7 +27,6 @@ const LectureSchema = new mongoose.Schema({
         link: String
     }],
     exercises: [String],
-    url: String,
     date: {
         type: Date,
         default: new Date()