Browse Source

Lectures can now be created.

Lee Morgan 5 years ago
parent
commit
a0d2b41548
8 changed files with 210 additions and 18 deletions
  1. 2 1
      .gitignore
  2. 1 1
      app.js
  3. 65 2
      controllers/learn.js
  4. 4 8
      models/lecture.js
  5. 1 1
      models/uploader.js
  6. 3 4
      routes.js
  7. 1 1
      views/learn/newCourse.html
  8. 133 0
      views/learn/newLecture.html

+ 2 - 1
.gitignore

@@ -1,4 +1,5 @@
 /node_modules
 /content/*
 *.swp
-/thumbNails/
+/thumbNails/
+/documents/

+ 1 - 1
app.js

@@ -44,7 +44,7 @@ app.use(express.static(__dirname + "/content"));
 app.use(compression());
 app.use(express.json());
 app.use(bodyParser.urlencoded({extended: false}))
-app.use(fileUpload({limits: { fileSize: 5 * 1024 * 1024 }}));
+app.use(fileUpload({limits: { fileSize: 1024 * 1024}}));
 require("./routes.js")(app);
 
 if(process.env.NODE_ENV === "production") httpsServer.listen(process.env.HTTPS_PORT);

+ 65 - 2
controllers/learn.js

@@ -43,7 +43,70 @@ module.exports = {
             });
     },
 
-    getCourseGroups: function(req, res){
-        console.log("something");
+    /*
+    POST: create a new lecture
+    req.body = {
+        uploader: String
+        password: String
+        course: String
+        title: String
+        video: String
+        description: String
+        exercises: [String]
+        documents: [{
+            title: String
+            link: String
+        }]
+    }
+    redirects to home
+    */
+    createLecture: function(req, res){
+        Course.findOne({_id: req.body.course})
+            .populate("owner")
+            .then((course)=>{
+                if(course === null) throw "noCourse";
+                if(req.body.uploader !== course.owner?._id.toString()) throw "badOwner";
+                if(req.body.password !== course.owner.password) throw "badPass";
+
+                let exercises = req.body.exercises.split("~");
+                exercises.splice(exercises.length - 1, 1);
+
+                let lecture = new Lecture({
+                    course: course._id,
+                    title: req.body.title,
+                    video: req.body.video,
+                    description: req.body.description,
+                    exercises: exercises,
+                    documents: []
+                });
+
+                let files = req.files.documents;
+                for(let i = 0; i < files.length; i++){
+                    let fileString = `./documents/${files[i].name}`;
+                    files[i].mv(fileString);
+                    lecture.documents.push(fileString);
+                }
+
+                return lecture.save();
+            })
+            .then((lecture)=>{
+                return res.redirect("/");
+            })
+            .catch((err)=>{
+                if(err === "noCourse") return res.json("Course does not exist");
+                if(err === "badOwner") return res.json("You do not own this course");
+                if(err === "badPass") return res.json("Incorrect password");
+                return res.redirect("/");
+            });
+    },
+
+    getCourses: function(req, res){
+        Course.find()
+            .then((courses)=>{
+                return res.json(courses);
+            })
+            .catch((err)=>{
+                return res.json("Could not retrieve courses");
+            });
     }
 }

+ 4 - 8
models/lecture.js

@@ -6,23 +6,19 @@ const LectureSchema = new mongoose.Schema({
         ref: "Course",
         required: true
     },
-    groupTitle: {
+    title: {
         type: String,
-        required: true,
+        required: true
     },
-    title: {
+    video: {
         type: String,
         required: true
     },
-    video: String,
     description: {
         type: String,
         required: true,
     },
-    documents: [{
-        title: String,
-        link: String
-    }],
+    documents: [String],
     exercises: [String]
 });
 

+ 1 - 1
models/uploader.js

@@ -5,4 +5,4 @@ const UploaderSchema = new mongoose.Schema({
     password: String
 });
 
-module.exports = mongoose.model("uploader", UploaderSchema);
+module.exports = mongoose.model("Uploader", UploaderSchema);

+ 3 - 4
routes.js

@@ -44,8 +44,7 @@ module.exports = function(app){
     //LEARN
     app.get("/learn/course/new", (req, res)=>{res.sendFile(`${views}/learn/newCourse.html`)});
     app.post("/learn/course/new", learn.createCourse);
-    // app.get("/learn/lecture", (req, res)=>{res.sendFile(`${views}/learn/newLecture.html`)});
-    // app.post("/learn/lecture", (learn.createLecture));
-    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/lecture/new", (req, res)=>{res.sendFile(`${views}/learn/newLecture.html`)});
+    app.post("/learn/lecture/new", (learn.createLecture));
+    app.get("/learn/courses", learn.getCourses);
 }

+ 1 - 1
views/learn/newCourse.html

@@ -38,7 +38,7 @@
                 <input name="courseId" type="text" required>
             </label>
 
-            <label>ThumbNail
+            <label>ThumbNail (max 1mb)
                 <input name="thumbNail" type="file" required>
             </label>
             

+ 133 - 0
views/learn/newLecture.html

@@ -0,0 +1,133 @@
+<!DOCTYPE html>
+<html lang="en">
+    <head>
+        <meta charset="utf-8">
+        <title>Lee Morgan -create lecture-</title>
+        <style>
+            form{
+                display: flex;
+                flex-direction: column;
+            }
+
+            form > *{
+                margin: 10px 0;
+            }
+
+            input[type=submit]{
+                max-width: 250px;
+            }
+
+            button{
+                max-width: 250px;
+            }
+
+            #exercises{
+                display: flex;
+                flex-direction: column;
+            }
+
+            #exercises > *{
+                margin: 10px 0;
+            }
+
+            #exercises input{
+                margin-left: 5px;
+            }
+        </style>
+    </head>
+    <body>
+        <h1>Create New Lecture</h1>
+
+        <form
+            id="form"
+            action="/learn/lecture/new"
+            method="post"
+            encType="multipart/form-data"
+        >
+            <label>Uploader ID
+                <input name="uploader" type="text" required>
+            </label>
+
+            <label>Uploader Password
+                <input name="password" type="password" required>
+            </label>
+
+            <label>Course
+                <select id="course" name="course" required></select>
+            </label>
+
+            <label>Title
+                <input name="title" type="text" required>
+            </label>
+
+            <label>Video (Rumble link)
+                <input name="video" type="text" required>
+            </label>
+
+            <label>Description
+                <input name="description" type="text" required>
+            </label>
+            
+            <label>Documents
+                <input name="documents" type="file" multiple>
+            </label>
+
+            <h2>Exercises</h2>
+            <button id="exerciseButton" type="button">Add Exercise</button>
+            
+            <div id="exercises">
+                <label>Exercise 1<input type="text"></label>
+            </div>
+
+            <input id="hidden" name="exercises" type="hidden">
+
+            <input type="submit" value="Submit">
+        </form>
+
+        <script>
+            //Get Available Courses
+            fetch("/learn/courses")
+                .then(response => response.json())
+                .then((response)=>{
+                    let select = document.getElementById("course");
+
+                    for(let i = 0; i < response.length; i++){
+                        let option = document.createElement("option");
+                        option.innerText = response[i].title;
+                        option.value = response[i]._id;
+                        select.appendChild(option);
+                    }
+                })
+                .catch((err)=>{});
+
+            //Add exercises
+            let count = 1;
+            let div = document.getElementById("exercises");
+
+            document.getElementById("exerciseButton").onclick = ()=>{
+                count++;
+                let label = document.createElement("label");
+                label.innerText = `Exercise ${count}`;
+                div.appendChild(label);
+
+                let input = document.createElement("input");
+                input.type = "text";
+                label.appendChild(input);
+            }
+
+            //Submit
+            let form = document.getElementById("form");
+            form.onsubmit = ()=>{
+                event.preventDefault();
+                let exercises = "";
+                for(let i = 0; i < div.children.length; i++){
+                    exercises += `${div.children[i].children[0].value}~`;
+                }
+
+                document.getElementById("hidden").value = exercises;
+
+                form.submit();
+            }
+        </script>
+    </body>
+</html>