ソースを参照

Create display courses page.

Lee Morgan 5 年 前
コミット
685a126d85
6 ファイル変更66 行追加11 行削除
  1. 5 5
      controllers/learn.js
  2. 2 1
      models/lecture.js
  3. 2 0
      routes.js
  4. 45 0
      views/learn/courses.html
  5. 11 4
      views/learn/index.css
  6. 1 1
      views/main/index.html

+ 5 - 5
controllers/learn.js

@@ -63,16 +63,16 @@ module.exports = {
     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";
+            .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";
 
                 let exercises = req.body.exercises.split("~");
                 exercises.splice(exercises.length - 1, 1);
 
                 let lecture = new Lecture({
-                    course: course._id,
+                    course: response[0]._id,
                     title: req.body.title,
                     video: req.body.video,
                     description: req.body.description,

+ 2 - 1
models/lecture.js

@@ -19,7 +19,8 @@ const LectureSchema = new mongoose.Schema({
         required: true,
     },
     documents: [String],
-    exercises: [String]
+    exercises: [String],
+    url: String
 });
 
 module.exports = mongoose.model("lecture", LectureSchema);

+ 2 - 0
routes.js

@@ -42,11 +42,13 @@ module.exports = function(app){
     app.get("/travel/*", (req, res)=>res.sendFile(`${views}/travel/index.html`));
 
     //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", (req, res)=>{res.sendFile(`${views}/learn/courses.html`)});
 
     //CONTENT
     app.get("/thumbNails/*", (req, res)=>{res.sendFile(`${__dirname}${req.url}`)});

+ 45 - 0
views/learn/courses.html

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

+ 11 - 4
views/learn/index.css

@@ -18,7 +18,6 @@ h1{
     justify-content: space-between;
     align-items: center;
     margin: 15px auto;
-    width: 90%;
     height: 150px;
     background: white;
     padding-right:15px;
@@ -30,9 +29,6 @@ h1{
     }
 
     .card div{
-        display: flex;
-        flex-direction: column;
-        justify-content: space-around;
         align-items: center;
         color: black;
         height: 100%;
@@ -41,4 +37,15 @@ h1{
 
     .card img{
         height: 100%;
+    }
+
+    .card h2{
+        text-align: center;
+        margin: 10px 0;
+    }
+
+    .card p{
+        text-align:center;
+        width: 75%;
+        margin: auto;
     }

+ 1 - 1
views/main/index.html

@@ -330,7 +330,7 @@
                         for(let i = 0; i < response.length; i++){
                             let card = document.createElement("a");
                             card.classList.add("card");
-                            card.href = "#";
+                            card.href = `/learn/course/${response[i]._id}`;
                             container.appendChild(card);
 
                             let h2 = document.createElement("h2");