feat(courses): Add lessons

This commit is contained in:
Khaled Garbaya
2017-09-26 11:19:28 +02:00
committed by Benedikt Rötsch
parent 6c7ab31305
commit e2ceb39e83
4 changed files with 32 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
const express = require('express')
const {getCourses} = require('./../services/contentful')
const {getCourses, getCourse} = require('./../services/contentful')
const router = express.Router()
@@ -16,13 +16,18 @@ router.get('/', async function (req, res, next) {
})
/* GET course detail. */
router.get('/:slug', function (req, res, next) {
res.render('courses', { title: `Course with slug ${req.params.slug}` })
router.get('/:slug', async function (req, res, next) {
let course = {}
course = await getCourse(req.params.slug)
res.render('course', {course})
})
/* GET course lesson detail. */
router.get('/:cslug/lessons/:lslug', function (req, res, next) {
res.render('courses', { title: `Course with slug ${req.params.cslug}` })
router.get('/:cslug/lessons/:lslug', async function (req, res, next) {
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

View File

@@ -1,9 +1,11 @@
const { createClient } = require('contentful')
let client = null
let previewClient = null
exports.initClient = () => {
client = createClient({space: process.env.CF_SPACE, accessToken: process.env.CF_ACCESS_TOKEN})
exports.initClient = (config = {space: process.env.CF_SPACE, accessToken: process.env.CF_ACCESS_TOKEN}) => {
client = createClient(config)
previewClient = createClient({...config, host: 'preview.contentful.com'})
}
exports.getCourses = () => {
// 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'})
}
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) => {
// TODO
}

View File

@@ -1,5 +1,12 @@
extends layout
block content
h1= title
p Welcome to #{title}
h1= course.fields.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}

View File

@@ -6,5 +6,5 @@ mixin courseCard(course = {fields: {title: '', description: '', categories: [],
a.course__categories--category(href=`categories/${category.fields.slug}`) #{category.title}
h1.course__title #{course.fields.title}
.course__description #{course.fields.shortDescription}
a.course__link(href=`/courses/${course.fields}`) view more
a.course__link(href=`/courses/${course.fields.slug}`) view more