Skip to content

Commit

Permalink
Feat/third nav retrieval (#71)
Browse files Browse the repository at this point in the history
* feat: add endpoint to retrieve details of all pages in collection

This commit adds a new endpoint, GET /sites/:siteName/collections/:collectionName/pages,
which retrieves the page details (including what type of page it is,
third nav or not) of all pages in a collection. This is primarily
helpful for retrieving information about third nav sections in
any given collection.

* feat: add util functions for route functions

This commit adds a route-utils.js file, which contains useful
util functions for common CMS operations, such as reading from
a collection page, or simple page, or creating a file in the _data
folder.

* fix: import required dependencies

Co-authored-by: Jie Hao Kwa <[email protected]>
  • Loading branch information
kwajiehao and Jie Hao Kwa authored Nov 13, 2020
1 parent aa7f7da commit ab17e5b
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ auth.post('/sites/:siteName/homepage', verifyJwt)

// Collection pages
auth.get('/sites/:siteName/collections/:collectionName', verifyJwt)
auth.get('/sites/:siteName/collections/:collectionName/pages', verifyJwt)
auth.post('/sites/:siteName/collections/:collectionName/pages', verifyJwt)
auth.get('/sites/:siteName/collections/:collectionName/pages/:pageName', verifyJwt)
auth.post('/sites/:siteName/collections/:collectionName/pages/:pageName', verifyJwt)
Expand Down
73 changes: 73 additions & 0 deletions routes/collectionPages.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
const express = require('express');
const router = express.Router();
const Bluebird = require('bluebird');
const yaml = require('js-yaml');
const base64 = require('base-64');
const _ = require('lodash');

// Import middleware
const { attachRouteHandlerWrapper } = require('../middleware/routeHandler')

// Import classes
const { Collection } = require('../classes/Collection.js')
const { File, CollectionPageType } = require('../classes/File.js');
const { update } = require('lodash');

// Import utils
const { readCollectionPageUtilFunc } = require('../utils/route-utils')

// List pages in collection
async function listCollectionPages(req, res, next) {
const { accessToken } = req
Expand All @@ -23,6 +31,70 @@ async function listCollectionPages(req, res, next) {
res.status(200).json({ collectionPages })
}

// Get details on all pages in a collection
async function listCollectionPagesDetails(req, res, next) {
const { accessToken } = req
const { siteName, collectionName } = req.params

// Verify that collection exists
const IsomerCollection = new Collection(accessToken, siteName)
const collections = await IsomerCollection.list()
if (!(collections.includes(collectionName))) throw new NotFoundError('Collection provided was not a valid collection')

// Retrieve metadata of files in collection
const CollectionPage = new File(accessToken, siteName)
const collectionPageType = new CollectionPageType(collectionName)
CollectionPage.setFileType(collectionPageType)
const collectionPages = await CollectionPage.list()
const collectionPagesMetadata = await Bluebird.map(collectionPages, async (page) => {
const { content } = await readCollectionPageUtilFunc(accessToken, siteName, collectionName, page.fileName)
const frontMatter = yaml.safeLoad(base64.decode(content).split('---')[1])
return {
fileName: page.fileName,
title: frontMatter.title,
thirdNavTitle: frontMatter.third_nav_title
}
})

const collectionHierarchy = collectionPagesMetadata.reduce((acc, file) => {
if (file.thirdNavTitle) {
// Check whether third nav section already exists
const thirdNavIteratee = { type: 'third-nav', 'title': file.thirdNavTitle }
if (_.some(acc, thirdNavIteratee)) {
const thirdNavIdx = _.findIndex(acc, thirdNavIteratee)
acc[thirdNavIdx].contents.push({
type: 'third-nav-page',
title: file.title,
fileName: file.fileName,
})
return acc
}

// Create new third nav section
acc.push({
type: 'third-nav',
title: file.thirdNavTitle,
contents: [{
type: 'third-nav-page',
title: file.title,
fileName: file.fileName,
}]
})
return acc
}

// If no third nav title, just push into array
acc.push({
type: 'page',
title: file.title,
fileName: file.fileName,
})
return acc
}, [])

res.status(200).json({ collectionPages: collectionHierarchy })
}

// Create new page in collection
async function createNewcollectionPage (req, res, next) {
const { accessToken } = req
Expand Down Expand Up @@ -118,6 +190,7 @@ async function renameCollectionPage (req, res, next) {
}

router.get('/:siteName/collections/:collectionName', attachRouteHandlerWrapper(listCollectionPages))
router.get('/:siteName/collections/:collectionName/pages', attachRouteHandlerWrapper(listCollectionPagesDetails))
router.post('/:siteName/collections/:collectionName/pages', attachRouteHandlerWrapper(createNewcollectionPage))
router.get('/:siteName/collections/:collectionName/pages/:pageName', attachRouteHandlerWrapper(readCollectionPage))
router.post('/:siteName/collections/:collectionName/pages/:pageName', attachRouteHandlerWrapper(updateCollectionPage))
Expand Down
37 changes: 37 additions & 0 deletions utils/route-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Import classes
const {
File,
PageType,
CollectionPageType,
DataType,
} = require('../classes/File')

const readPageUtilFunc = async (accessToken, siteName, pageName) => {
const IsomerFile = new File(accessToken, siteName)
const pageType = new PageType()
IsomerFile.setFileType(pageType)
const fileContents = await IsomerFile.read(pageName)
return fileContents
}

const readCollectionPageUtilFunc = async (accessToken, siteName, collectionName, pageName) => {
const IsomerFile = new File(accessToken, siteName)
const collectionPageType = new CollectionPageType(collectionName)
IsomerFile.setFileType(collectionPageType)
const fileContents = await IsomerFile.read(pageName)
return fileContents
}

const createDataFileUtilFunc = async (accessToken, siteName, filePath, content) => {
const IsomerFile = new File(accessToken, siteName)
const dataType = new DataType()
IsomerFile.setFileType(dataType)
await IsomerFile.create(filePath, content)
return
}

module.exports = {
readPageUtilFunc,
readCollectionPageUtilFunc,
createDataFileUtilFunc,
}

0 comments on commit ab17e5b

Please sign in to comment.