Explorar el Código

Create models for the courses.

Lee Morgan hace 5 años
padre
commit
889c824c4c
Se han modificado 7 ficheros con 128 adiciones y 0 borrados
  1. 58 0
      controllers/learn.js
  2. 27 0
      models/course.js
  3. 29 0
      models/lecture.js
  4. 8 0
      models/uploader.js
  5. 3 0
      routes.js
  6. 0 0
      views/learn/newCourse.html
  7. 3 0
      views/main/index.html

+ 58 - 0
controllers/learn.js

@@ -0,0 +1,58 @@
+const Course = require("../models/course.js");
+
+module.exports = {
+    /*
+    POST: creates a new course
+    req.body = {
+        groupTitle: String,
+        title: String,
+        video: String,
+        description: String,
+        documents: [{
+            title: String,
+            link: String
+        }],
+        exercises: [String]
+    }
+    */
+    createLecture: function(req, res){
+        Course.find({groupTitle: req.body.groupTitle})
+            .then((courses)=>{
+                let max = 0;
+                for(let i = 0; i < courses.length; i++){
+                    let num = parseInt(courses[i].courseId.split("-")[1]);
+
+                    if(num > max) max = num;
+                }
+
+                //from StackOverflow user "RobG"
+                function pad(n, length) {
+                    var len = length - (''+n).length;
+                    return (len > 0 ? new Array(++len).join('0') : '') + n;
+                }
+
+                let newCourse = new Course({
+                    courseId: `${courses[0].courseId.split("-")[0]}-${pad(max++)}`,
+                    groupTitle: courses[0].groupTitle,
+                    title: req.body.title,
+                    video: req.body.video,
+                    thumbNail: courses[0].thumbNail,
+                    description: req.body.description,
+                    documents: req.body.documents,
+                    exercises: req.body.exercises
+                });
+
+                return newCourse.save();
+            })
+            .then((course)=>{
+                return res.redirect("/");
+            })
+            .catch((err)=>{
+                return res.json("Something went wrong");
+            });
+    },
+
+    getCourseGroups: function(req, res){
+        console.log("something");
+    }
+}

+ 27 - 0
models/course.js

@@ -0,0 +1,27 @@
+const mongoose = require("mongoose");
+
+const CourseSchema = new mongoose.Schema({
+    owner: {
+        type: mongoose.Schema.Types.ObjectId,
+        ref: "Uploader",
+        required: true
+    },
+    courseId: {
+        type: String,
+        required: true
+    },
+    title: {
+        type: String,
+        required: true
+    },
+    thumbNail: {
+        type: String,
+        required: true
+    },
+    description: {
+        type: String,
+        required: true
+    }
+});
+
+module.exports = mongoose.model("course", CourseSchema);

+ 29 - 0
models/lecture.js

@@ -0,0 +1,29 @@
+const mongoose = require("mongoose");
+
+const LectureSchema = new mongoose.Schema({
+    course: {
+        type: mongoose.Schema.Types.ObjectId,
+        ref: "Course",
+        required: true
+    },
+    groupTitle: {
+        type: String,
+        required: true,
+    },
+    title: {
+        type: String,
+        required: true
+    },
+    video: String,
+    description: {
+        type: String,
+        required: true,
+    },
+    documents: [{
+        title: String,
+        link: String
+    }],
+    exercises: [String]
+});
+
+module.exports = mongoose.model("lecture", LectureSchema);

+ 8 - 0
models/uploader.js

@@ -0,0 +1,8 @@
+const mongoose = require("mongoose");
+
+const UploaderSchema = new mongoose.Schema({
+    name: String,
+    password: String
+});
+
+module.exports = mongoose.model("uploader", UploaderSchema);

+ 3 - 0
routes.js

@@ -1,5 +1,6 @@
 const writing = require("./controllers/writing.js");
 const travel = require("./controllers/travel.js");
+const learn = require("./controllers/learn.js");
 
 module.exports = function(app){
     let views = `${__dirname}/views`;
@@ -41,6 +42,8 @@ module.exports = function(app){
     app.get("/travel/*", (req, res)=>res.sendFile(`${views}/travel/index.html`));
 
     //LEARN
+    app.get("/learn/new", (req, res)=>{res.sendFile(`${views}/learn/new.html`)});
     app.get("/learn/style", (req, res)=>res.sendFile(`${views}/learn/index.css`));
     app.get("/learn/web", (req, res)=>{res.sendFile(`${views}/learn/web.html`)});
+    app.get("/learn/courses", learn.getCourseGroups);
 }

+ 0 - 0
views/learn/newCourse.html


+ 3 - 0
views/main/index.html

@@ -330,6 +330,9 @@
                     recurse(response, headContainer);
                 })
                 .catch((err)=>{});
+
+                //Get "learn" content
+                
         </script>
     </body>
 </html>