Procházet zdrojové kódy

Display a basic form of a lecture on the page.

Lee Morgan před 5 roky
rodič
revize
285d2dd5b3
6 změnil soubory, kde provedl 99 přidání a 10 odebrání
  1. 30 5
      controllers/learn.js
  2. 1 1
      models/course.js
  3. 6 2
      models/lecture.js
  4. 3 1
      routes.js
  5. 1 1
      views/learn/course.html
  6. 58 0
      views/learn/lecture.html

+ 30 - 5
controllers/learn.js

@@ -19,7 +19,7 @@ module.exports = {
             .then((uploader)=>{
                 if(uploader === null) throw "uploader";
                 if(req.body.password !== uploader.password) throw "pass";
-                let imgString = `thumbNails/${req.files.thumbNail.name}`;
+                let imgString = `/thumbNails/${req.files.thumbNail.name}`;
                 req.files.thumbNail.mv(imgString);
 
                 let course = new Course({
@@ -79,10 +79,16 @@ module.exports = {
                 });
 
                 let files = req.files.documents;
-                for(let i = 0; i < files.length; i++){
-                    let fileString = `./documents/${files[i].name}`;
-                    files[i].mv(fileString);
+                if(files.length === undefined){
+                    let fileString = `/documents/${files.name}`;
+                    files.mv(fileString)
                     lecture.documents.push(fileString);
+                }else{
+                    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();
@@ -111,7 +117,10 @@ module.exports = {
     /*
     GET: get a list of lectures from a course
     req.params.id = String (course id)
-    response = [Lecture]
+    response = {
+        course: Course,
+        lectures: [Lecture]
+    }
     */
     getLectures: function(req, res){
         let course = Course.findOne({_id: req.params.id});
@@ -124,5 +133,21 @@ module.exports = {
             .catch((err)=>{
                 return res.json("Could not retrieve lectures");
             });
+    },
+
+    /*
+    GET: get data for a single lecture
+    req.params.id = String (lecture id)
+    response = Lecture
+    */
+    getLecture: function(req, res){
+        Lecture.findOne({_id: req.params.id})
+            .populate("course")
+            .then((lecture)=>{
+                return res.json(lecture);
+            })
+            .catch((err)=>{
+                return res.json("Could not retrieve lecture");
+            });
     }
 }

+ 1 - 1
models/course.js

@@ -20,4 +20,4 @@ const CourseSchema = new mongoose.Schema({
     }
 });
 
-module.exports = mongoose.model("course", CourseSchema);
+module.exports = mongoose.model("Course", CourseSchema);

+ 6 - 2
models/lecture.js

@@ -20,7 +20,11 @@ const LectureSchema = new mongoose.Schema({
     },
     documents: [String],
     exercises: [String],
-    url: String
+    url: String,
+    date: {
+        type: Date,
+        default: new Date()
+    }
 });
 
-module.exports = mongoose.model("lecture", LectureSchema);
+module.exports = mongoose.model("Lecture", LectureSchema);

+ 3 - 1
routes.js

@@ -52,7 +52,9 @@ module.exports = function(app){
 
     app.get("/learn/lectures/new", (req, res)=>{res.sendFile(`${views}/learn/newLecture.html`)});
     app.post("/learn/lectures/new", learn.createLecture);
-    app.get("/learn/lectures/json/:id", learn.getLectures)
+    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);
 
     //CONTENT
     app.get("/thumbNails/*", (req, res)=>{res.sendFile(`${__dirname}${req.url}`)});

+ 1 - 1
views/learn/course.html

@@ -35,7 +35,7 @@
 
                     for(let i = 0; i < response.lectures.length; i++){
                         let card = template.cloneNode(true);
-                        card.href = "/learn/lectures/:id";
+                        card.href = `/learn/lectures/${response.lectures[i]._id}`;
                         card.children[0].src = response.course.thumbNail;
                         card.children[0].alt = `${response.course.thumbNail} thumbnail`;
                         card.children[1].children[0].innerText = response.lectures[i].title;

+ 58 - 0
views/learn/lecture.html

@@ -0,0 +1,58 @@
+<!DOCTYPE html>
+<html lang="en">
+    <head>
+        <meta charset="utf-8">
+        <title>Lee Morgan -lecture-</title>
+        <link rel="stylesheet" href="/learn/style">
+    </head>
+    <body>
+        <h1 id="title"></h1>
+
+        <h3 id="course"></h3>
+
+        <h6 id="date"></h6>
+
+        <iframe id="video"></iframe>
+
+        <p id="description"></p>
+
+        <div id="documents">
+            <h2>Resources</h2>
+        </div>
+
+        <ol id="exercises">
+            <h2>Exercises</h2>
+        </ol>
+
+        <script>
+            let id = window.location.href.split("/");
+            id = id[id.length-1];
+
+            fetch(`/learn/lectures/json/one/${id}`)
+                .then(response => response.json())
+                .then((response)=>{
+                    document.getElementById("title").innerText = response.title;
+                    document.getElementById("course").innerText = response.course.title;
+                    document.getElementById("date").innerText = response.date;
+                    document.getElementById("video").src = response.video;
+                    document.getElementById("description").innerText = response.description;
+                    
+                    let documents = document.getElementById("documents");
+                    for(let i = 0; i < response.documents.length; i++){
+                        let a = document.createElement("a");
+                        a.href = response.documents[i];
+                        a.innerText = response.documents[i];
+                        documents.appendChild(a);
+                    }
+
+                    let exercises = document.getElementById("exercises");
+                    for(let i = 0; i < response.exercises.length; i++){
+                        let li = document.createElement("li");
+                        li.innerText = response.exercises[i];
+                        exercises.appendChild(li);
+                    }
+                })
+                .catch((err)=>{});
+        </script>
+    </body>
+</html>