Selaa lähdekoodia

Create the page to display all lectures from a course.

Lee Morgan 5 vuotta sitten
vanhempi
sitoutus
65863070d4

+ 24 - 6
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({
@@ -61,16 +61,16 @@ module.exports = {
     createLecture: function(req, res){
         Course.findOne({_id: req.body.course})
             .populate("owner")
-            .then((response)=>{
-                if(response[0] === null) throw "noCourse";
-                if(req.body.uploader !== response[0].owner?._id.toString()) throw "badOwner";
-                if(req.body.password !== response[0].owner.password) throw "badPass";
+            .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: response[0]._id,
+                    course: course._id,
                     title: req.body.title,
                     video: req.body.video,
                     description: req.body.description,
@@ -106,5 +106,23 @@ module.exports = {
             .catch((err)=>{
                 return res.json("Could not retrieve courses");
             });
+    },
+
+    /*
+    GET: get a list of lectures from a course
+    req.params.id = String (course id)
+    response = [Lecture]
+    */
+    getLectures: function(req, res){
+        let course = Course.findOne({_id: req.params.id});
+        let lectures = Lecture.find({course: req.params.id});
+
+        Promise.all([course, lectures])
+            .then((response)=>{
+                return res.json({course: response[0], lectures: response[1]});
+            })
+            .catch((err)=>{
+                return res.json("Could not retrieve lectures");
+            });
     }
 }

+ 9 - 5
routes.js

@@ -43,13 +43,17 @@ module.exports = function(app){
 
     //LEARN
     app.get("/learn/style", (req, res)=>{res.sendFile(`${views}/learn/index.css`)});
-    app.get("/learn/course/new", (req, res)=>{res.sendFile(`${views}/learn/newCourse.html`)});
-    app.post("/learn/course/new", learn.createCourse);
-    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);
+    
+    app.get("/learn/courses/new", (req, res)=>{res.sendFile(`${views}/learn/newCourse.html`)});
+    app.post("/learn/courses/new", learn.createCourse);
+    app.get("/learn/courses/json", learn.getCourses);
+    app.get("/learn/courses/:id", (req, res)=>{res.sendFile(`${views}/learn/course.html`)});
     app.get("/learn", (req, res)=>{res.sendFile(`${views}/learn/courses.html`)});
 
+    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)
+
     //CONTENT
     app.get("/thumbNails/*", (req, res)=>{res.sendFile(`${__dirname}${req.url}`)});
     app.get("/documents/*", (req, res)=>{res.sendFile(`${__dirname}${req.url}`)});

+ 49 - 0
views/learn/course.html

@@ -0,0 +1,49 @@
+<!DOCTYPE html>
+<html lang="en">
+    <head>
+        <meta charset="utf-8">
+        <title>Lee Morgan -course-</title>
+        <link rel="stylesheet" href="/learn/style">
+    </head>
+    <body>
+        <h1 id="title"></h1>
+
+        <div id="lectures" class="cardContainer"></div>
+
+        <template id="template">
+            <a class="card">
+                <img>
+
+                <div>
+                    <h2></h2>
+
+                    <p></p>
+                </div>
+            </a>
+        </template>
+
+        <script>
+            let id = window.location.href.split("/");
+            id = id[id.length-1];
+
+            fetch(`/learn/lectures/json/${id}`)
+                .then(response => response.json())
+                .then((response)=>{
+                    document.getElementById("title").innerText = response.course.title;
+                    let container = document.getElementById("lectures");
+                    let template = document.getElementById("template").content.children[0];
+
+                    for(let i = 0; i < response.lectures.length; i++){
+                        let card = template.cloneNode(true);
+                        card.href = "/learn/lectures/: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;
+                        card.children[1].children[1].innerText = response.lectures[i].description;
+                        container.appendChild(card)
+                    }
+                })
+                .catch((err)=>{});
+        </script>
+    </body>
+</html>

+ 2 - 2
views/learn/courses.html

@@ -23,7 +23,7 @@
         </template>
 
         <script>
-            fetch("/learn/courses")
+            fetch("/learn/courses/json")
                 .then(response => response.json())
                 .then((response)=>{
                     let container = document.getElementById("container");
@@ -31,7 +31,7 @@
 
                     for(let i = 0; i < response.length; i++){
                         let course = template.cloneNode(true);
-                        course.href = `/learn/course/${response[i]._id}`;
+                        course.href = `/learn/courses/${response[i]._id}`;
                         course.children[0].src = response[i].thumbNail;
                         course.children[0].alt = `${response[i].title} thumbnail`;
                         course.children[1].children[0].innerText = response[i].title;

+ 1 - 1
views/learn/newCourse.html

@@ -21,7 +21,7 @@
     <body>
         <h1>Enter course details</h1>
 
-        <form action="/learn/course/new" method="post" encType="multipart/form-data">
+        <form action="/learn/courses/new" method="post" encType="multipart/form-data">
             <label>Uploader ID
                 <input name="uploaderId" type="text" required>
             </label>

+ 2 - 2
views/learn/newLecture.html

@@ -40,7 +40,7 @@
 
         <form
             id="form"
-            action="/learn/lecture/new"
+            action="/learn/lectures/new"
             method="post"
             encType="multipart/form-data"
         >
@@ -86,7 +86,7 @@
 
         <script>
             //Get Available Courses
-            fetch("/learn/courses")
+            fetch("/learn/courses/json")
                 .then(response => response.json())
                 .then((response)=>{
                     let select = document.getElementById("course");

+ 0 - 29
views/learn/web.html

@@ -1,29 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-    <head>
-        <meta charset="utf-8">
-        <title>Lee Morgan</title>
-        <link rel="stylesheet" href="/learn/style">
-    </head>
-    <body>
-        <h1>Introduction to Web Programming</h1>
-
-        <div class="cardContainer">
-            <a class="card" href="/learn/web/001">
-                <img src="/images/web" alt="World Wide Web">
-                <div>
-                    <h3>How the Web Works</h3>
-                    <p>A brief description of what the web is and what happens when you visit a web page.</p>
-                </div>
-            </a>
-
-            <a class="card" href="/learn/web/002">
-                <img src="/images/web" alt="World Wide Web">
-                <div>
-                    <h3>Workplace Setup</h3>
-                    <p>A few tools that are used for programming and some instructions to install and set them up.</p>
-                </div>
-            </a>
-        </div>
-    </body>
-</html>

+ 2 - 2
views/main/index.html

@@ -322,7 +322,7 @@
                 .catch((err)=>{});
 
                 //Get "learn" content
-                fetch("/learn/courses")
+                fetch("/learn/courses/json")
                     .then(response => response.json())
                     .then((response)=>{
                         let container = document.getElementById("learn");
@@ -330,7 +330,7 @@
                         for(let i = 0; i < response.length; i++){
                             let card = document.createElement("a");
                             card.classList.add("card");
-                            card.href = `/learn/course/${response[i]._id}`;
+                            card.href = `/learn/courses/${response[i]._id}`;
                             container.appendChild(card);
 
                             let h2 = document.createElement("h2");