From f61c13d02e830d8377e47d63ff8a2184c1ad1b72 Mon Sep 17 00:00:00 2001 From: Khaled Garbaya Date: Mon, 16 Oct 2017 15:52:01 +0200 Subject: [PATCH] fix: Fix previw/delivery switch --- app.js | 3 +++ handlers/errorHandlers.js | 1 + public | 2 +- routes/about.js | 2 +- routes/courses.js | 10 +++++----- routes/index.js | 2 +- services/contentful.js | 19 +++++++++++++------ 7 files changed, 25 insertions(+), 14 deletions(-) diff --git a/app.js b/app.js index 842f21c..9e045f9 100644 --- a/app.js +++ b/app.js @@ -14,6 +14,8 @@ const categories = require('./routes/categories') const about = require('./routes/about') const settings = require('./routes/settings') const sitemap = require('./routes/sitemap') +const lessons = require('./routes/lessons') + const { initClient, getSpace } = require('./services/contentful') const breadcrumb = require('./lib/breadcrumb') const app = express() @@ -90,6 +92,7 @@ app.use(async function (req, res, next) { app.use('/', index) app.use('/courses', courses) +app.use('/lessons', lessons) app.use('/categories', categories) app.use('/about', about) app.use('/settings', settings) diff --git a/handlers/errorHandlers.js b/handlers/errorHandlers.js index 9fc3ce0..7238d6d 100644 --- a/handlers/errorHandlers.js +++ b/handlers/errorHandlers.js @@ -8,6 +8,7 @@ exports.catchErrors = (fn) => { return function (req, res, next) { return fn(req, res, next).catch((e) => { + console.log(e) next(e) }) } diff --git a/public b/public index 632b2e2..941a9d8 160000 --- a/public +++ b/public @@ -1 +1 @@ -Subproject commit 632b2e22e9f589f0986416eea6c199762e5c141c +Subproject commit 941a9d81cee1f5c100eee21c31f61204d1b296eb diff --git a/routes/about.js b/routes/about.js index 5c70513..371d243 100644 --- a/routes/about.js +++ b/routes/about.js @@ -5,7 +5,7 @@ const router = express.Router() /* GET the about landing page. */ router.get('/', catchErrors(async function (req, res, next) { - const landingPage = await getLandingPage('about', res.locals.currentLocale.code, res.locals.currentLocale.id) + const landingPage = await getLandingPage('about', res.locals.currentLocale.code, res.locals.currentApi.id) res.render('landingPage', { title: 'About', landingPage }) })) diff --git a/routes/courses.js b/routes/courses.js index 85f951e..39132ca 100644 --- a/routes/courses.js +++ b/routes/courses.js @@ -8,8 +8,8 @@ router.get('/', catchErrors(async function (req, res, next) { // we get all the entries with the content type `course` let courses = [] let categories = [] - courses = await getCourses(res.locals.currentLocale.code, res.locals.currentLocale.id) - categories = await getCategories(res.locals.currentLocale.code, res.locals.currentLocale.id) + courses = await getCourses(res.locals.currentLocale.code, res.locals.currentApi.id) + categories = await getCategories(res.locals.currentLocale.code, res.locals.currentApi.id) res.render('courses', { title: `All Courses (${courses.length})`, categories, courses }) })) @@ -22,7 +22,7 @@ router.get('/categories/:category', catchErrors(async function (req, res, next) try { categories = await getCategories() activeCategory = categories.find((category) => category.fields.slug === req.params.category) - courses = await getCoursesByCategory(activeCategory.sys.id, res.locals.currentLocale.code, res.locals.currentLocale.id) + courses = await getCoursesByCategory(activeCategory.sys.id, res.locals.currentLocale.code, res.locals.currentApi.id) } catch (e) { console.log('Error ', e) } @@ -31,7 +31,7 @@ router.get('/categories/:category', catchErrors(async function (req, res, next) /* GET course detail. */ const courseRoute = catchErrors(async function (req, res, next) { - let course = await getCourse(req.params.slug, res.locals.currentLocale.code, res.locals.currentLocale.id) + let course = await getCourse(req.params.slug, res.locals.currentLocale.code, res.locals.currentApi.id) const lessons = course.fields.lessons const lessonIndex = lessons.findIndex((lesson) => lesson.fields.slug === req.params.lslug) const lesson = lessons[lessonIndex] @@ -47,7 +47,7 @@ router.get('/:slug/lessons', courseRoute) /* GET course lesson detail. */ router.get('/:cslug/lessons/:lslug', catchErrors(async function (req, res, next) { - let course = await getCourse(req.params.cslug, res.locals.currentLocale.code, res.locals.currentLocale.id) + let course = await getCourse(req.params.cslug, res.locals.currentLocale.code, res.locals.currentApi.id) const lessons = course.fields.lessons const lessonIndex = lessons.findIndex((lesson) => lesson.fields.slug === req.params.lslug) const lesson = lessons[lessonIndex] diff --git a/routes/index.js b/routes/index.js index 5738d1f..5755ea5 100644 --- a/routes/index.js +++ b/routes/index.js @@ -5,7 +5,7 @@ const router = express.Router() /* GET the home landing page. */ router.get('/', catchErrors(async function (req, res, next) { - const landingPage = await getLandingPage('home', res.locals.currentLocale.code, res.locals.currentLocale.id) + const landingPage = await getLandingPage('home', res.locals.currentLocale.code, res.locals.currentApi.id) let title = landingPage.fields.title if (!title || landingPage.fields.slug === 'home') { title = 'The Example App' diff --git a/services/contentful.js b/services/contentful.js index 8032cb1..b470adc 100644 --- a/services/contentful.js +++ b/services/contentful.js @@ -3,8 +3,11 @@ const { createClient } = require('contentful') let cdaClient = null let cpaClient = null +// Initialize our client exports.initClient = (options) => { + // Getting the version the app version const { version } = require('../package.json') + const config = options || { space: process.env.CF_SPACE, cda: process.env.CF_ACCESS_TOKEN, @@ -28,9 +31,9 @@ exports.getSpace = assert((api = `cda`) => { return client.getSpace() }, 'Space') +// to get all the courses we request all the entries +// with the content_type `course` from Contentful exports.getCourses = assert((locale = 'en-US', api = `cda`) => { - // to get all the courses we request all the entries - // with the content_type `course` from Contentful const client = api === 'cda' ? cdaClient : cpaClient return client.getEntries({ content_type: 'course', @@ -41,8 +44,8 @@ exports.getCourses = assert((locale = 'en-US', api = `cda`) => { .then((response) => response.items) }, 'Course') +// Landing pages like the home or about page are fully controlable via Contentful. exports.getLandingPage = (slug, locale = 'en-US', api = `cda`) => { - // Landing pages like the home or about page are fully controlable via Contentful. const client = api === 'cda' ? cdaClient : cpaClient return client.getEntries({ content_type: 'landingPage', @@ -53,10 +56,10 @@ exports.getLandingPage = (slug, locale = 'en-US', api = `cda`) => { .then((response) => response.items[0]) } +// the SDK supports link resolution only when you call the collection endpoints +// 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 exports.getCourse = assert((slug, locale = 'en-US', api = `cda`) => { - // the SDK supports link resolution only when you call the collection endpoints - // 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 const client = api === 'cda' ? cdaClient : cpaClient return client.getEntries({ content_type: 'course', @@ -73,6 +76,9 @@ exports.getCategories = assert((locale = 'en-US', api = `cda`) => { .then((response) => response.items) }, 'Course') +// Getting a course by Category is simply querying all entries +// with a query params `fields.categories.sys.id` equal to the desired category id +// Note that you need to send the `content_type` param to be able to query the entry exports.getCoursesByCategory = assert((category, locale = 'en-US', api = `cda`) => { const client = api === 'cda' ? cdaClient : cpaClient return client.getEntries({ @@ -85,6 +91,7 @@ exports.getCoursesByCategory = assert((category, locale = 'en-US', api = `cda`) .then((response) => response.items) }, 'Category') +// Utitlities functions function assert (fn, context) { return function (req, res, next) { return fn(req, res, next)