feat(courses): Add lessons
This commit is contained in:
committed by
Benedikt Rötsch
parent
6c7ab31305
commit
e2ceb39e83
@@ -1,5 +1,5 @@
|
|||||||
const express = require('express')
|
const express = require('express')
|
||||||
const {getCourses} = require('./../services/contentful')
|
const {getCourses, getCourse} = require('./../services/contentful')
|
||||||
|
|
||||||
const router = express.Router()
|
const router = express.Router()
|
||||||
|
|
||||||
@@ -16,13 +16,18 @@ router.get('/', async function (req, res, next) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
/* GET course detail. */
|
/* GET course detail. */
|
||||||
router.get('/:slug', function (req, res, next) {
|
router.get('/:slug', async function (req, res, next) {
|
||||||
res.render('courses', { title: `Course with slug ${req.params.slug}` })
|
let course = {}
|
||||||
|
course = await getCourse(req.params.slug)
|
||||||
|
res.render('course', {course})
|
||||||
})
|
})
|
||||||
|
|
||||||
/* GET course lesson detail. */
|
/* GET course lesson detail. */
|
||||||
router.get('/:cslug/lessons/:lslug', function (req, res, next) {
|
router.get('/:cslug/lessons/:lslug', async function (req, res, next) {
|
||||||
res.render('courses', { title: `Course with slug ${req.params.cslug}` })
|
let course = await getCourse(req.params.cslug)
|
||||||
|
let lesson = course.fields.lessons.find((lesson) => lesson.fields.slug === req.params.lslug)
|
||||||
|
console.log(lesson)
|
||||||
|
res.render('course', {course, lesson})
|
||||||
})
|
})
|
||||||
|
|
||||||
module.exports = router
|
module.exports = router
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
const { createClient } = require('contentful')
|
const { createClient } = require('contentful')
|
||||||
|
|
||||||
let client = null
|
let client = null
|
||||||
|
let previewClient = null
|
||||||
|
|
||||||
exports.initClient = () => {
|
exports.initClient = (config = {space: process.env.CF_SPACE, accessToken: process.env.CF_ACCESS_TOKEN}) => {
|
||||||
client = createClient({space: process.env.CF_SPACE, accessToken: process.env.CF_ACCESS_TOKEN})
|
client = createClient(config)
|
||||||
|
previewClient = createClient({...config, host: 'preview.contentful.com'})
|
||||||
}
|
}
|
||||||
exports.getCourses = () => {
|
exports.getCourses = () => {
|
||||||
// to get all the courses we simply request from Contentful all the entries
|
// to get all the courses we simply request from Contentful all the entries
|
||||||
@@ -11,6 +13,14 @@ exports.getCourses = () => {
|
|||||||
return client.getEntries({content_type: 'course'})
|
return client.getEntries({content_type: 'course'})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exports.getCourse = (slug) => {
|
||||||
|
// the SDK support link resolution only when you request the collection endpoint
|
||||||
|
// That's why we are using getEntries with a query instead of getEntry(entryId)
|
||||||
|
// make sure to specify the content_type whenever you want to perform a query
|
||||||
|
return client.getEntries({content_type: 'course', 'fields.slug': slug})
|
||||||
|
.then((response) => response.items[0])
|
||||||
|
}
|
||||||
|
|
||||||
exports.getLessons = (courseId) => {
|
exports.getLessons = (courseId) => {
|
||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,12 @@
|
|||||||
extends layout
|
extends layout
|
||||||
|
|
||||||
block content
|
block content
|
||||||
h1= title
|
h1= course.fields.title
|
||||||
p Welcome to #{title}
|
if lesson
|
||||||
|
p= lesson.fields.description
|
||||||
|
else
|
||||||
|
p= course.fields.description
|
||||||
|
ul
|
||||||
|
each lesson in course.fields.lessons
|
||||||
|
li
|
||||||
|
a(href=`/courses/${course.fields.slug}/lessons/${lesson.fields.slug}`) #{lesson.fields.title}
|
||||||
|
|||||||
@@ -6,5 +6,5 @@ mixin courseCard(course = {fields: {title: '', description: '', categories: [],
|
|||||||
a.course__categories--category(href=`categories/${category.fields.slug}`) #{category.title}
|
a.course__categories--category(href=`categories/${category.fields.slug}`) #{category.title}
|
||||||
h1.course__title #{course.fields.title}
|
h1.course__title #{course.fields.title}
|
||||||
.course__description #{course.fields.shortDescription}
|
.course__description #{course.fields.shortDescription}
|
||||||
a.course__link(href=`/courses/${course.fields}`) view more
|
a.course__link(href=`/courses/${course.fields.slug}`) view more
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user