瀏覽代碼

Add route to create answers for questions.

Lee Morgan 4 年之前
父節點
當前提交
f2ea0e84a0
共有 2 個文件被更改,包括 33 次插入0 次删除
  1. 32 0
      controllers/learn.js
  2. 1 0
      routes.js

+ 32 - 0
controllers/learn.js

@@ -305,5 +305,37 @@ module.exports = {
                 console.error(err);
                 return res.json("ERROR: unable to save the question");
             });
+    },
+
+    /*
+    POST: creates a new answer to a question
+    req.body = {
+        lecture: String (Lecture id)
+        question: String (Question id)
+        name: String
+        content: String
+    }
+    */
+    createAnswer: function(req, res){
+        let answer = new Answer({
+            name: req.body.name,
+            content: req.body.content
+        });
+
+        Lecture.findOne({_id: req.body.lecture})
+            .then((lecture)=>{
+                let question = lecture.questions.id(req.body.question);
+
+                question.answers.push(answer);
+                
+                lecture.save();
+            })
+            .then((lecture)=>{
+                return res.json(answer);
+            })
+            .catch((err)=>{
+                console.error(err);
+                return res.json("ERROR: unable to create new question");
+            });
     }
 }

+ 1 - 0
routes.js

@@ -75,6 +75,7 @@ module.exports = function(app){
     app.get("/learn/lectures/json/one/:id", learn.getLecture);
 
     app.post("/learn/questions/create", learn.createQuestion);
+    app.post("/learn/answers/create", learn.createAnswer);
 
     app.post("/htmltest", visit, (req, res)=>{res.send(`Hi ${req.body.name} from ${req.body.place}`)});