Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/third nav retrieval #71

Merged
merged 3 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}