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/get all collections directories #117

Merged
merged 3 commits into from
Mar 3, 2021
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
17 changes: 16 additions & 1 deletion classes/Directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ class Directory {
}
}

class RootType {
constructor() {
this.folderName = ''
}

getFolderName() {
return this.folderName
}
}

class FolderType {
constructor(folderPath) {
this.folderName = folderPath
Expand All @@ -118,4 +128,9 @@ class ResourceRoomType {
}
}

module.exports = { Directory, FolderType, ResourceRoomType }
module.exports = {
Directory,
RootType,
FolderType,
ResourceRoomType,
}
1 change: 1 addition & 0 deletions middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ auth.post('/v1/sites/:siteName/homepage', verifyJwt)

// Folder pages
auth.get('/v1/sites/:siteName/folders', verifyJwt)
auth.get('/v1/sites/:siteName/folders/all', verifyJwt)

// Collection pages
auth.get('/v1/sites/:siteName/collections/:collectionName', verifyJwt)
Expand Down
41 changes: 38 additions & 3 deletions routes/folders.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
const express = require('express');
const router = express.Router();
const Bluebird = require('bluebird')

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

// Import classes
const { Directory, FolderType } = require('../classes/Directory.js');
// Import classes
const { File, CollectionPageType } = require('../classes/File.js');
const { Directory, RootType, FolderType } = require('../classes/Directory.js');

const ISOMER_TEMPLATE_DIRS = ['_data', '_includes', '_site', '_layouts']

// List pages and directories in folder
async function listFolderContent (req, res, next) {
Expand All @@ -20,6 +24,37 @@ async function listFolderContent (req, res, next) {
res.status(200).json({ folderPages })
}

router.get('/:siteName/folders', attachReadRouteHandlerWrapper(listFolderContent))
// List pages and directories from all folders
async function listAllFolderContent (req, res, next) {
const { accessToken } = req
const { siteName } = req.params

const IsomerDirectory = new Directory(accessToken, siteName)
const folderType = new RootType()
IsomerDirectory.setDirType(folderType)
const repoRootContent = await IsomerDirectory.list()

const allFolders = repoRootContent.reduce((acc, curr) => {
if (
curr.type === 'dir'
&& !ISOMER_TEMPLATE_DIRS.includes(curr.name)
&& curr.name.slice(0, 1) === '_'
) acc.push(curr.path)
Comment on lines +38 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could remove the underscore on the backend instead of the frontend, but I'm open to either option

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved with 7f8b635. For consistency, we will abstract the _ character away from the frontend entirely, and the backend will be responsible for removing and appending the _ character

return acc
}, [])

const allFolderContent = await Bluebird.map(allFolders, async (folder) => {
const IsomerFile = new File(accessToken, siteName)
const collectionPageType = new CollectionPageType(folder.slice(1))
IsomerFile.setFileType(collectionPageType)
const { sha, content } = await IsomerFile.read('collection.yml')
return { name: folder.slice(1), sha, content }
})

res.status(200).json({ allFolderContent })
}

router.get('/:siteName/folders', attachRouteHandlerWrapper(listFolderContent))
router.get('/:siteName/folders/all', attachRouteHandlerWrapper(listAllFolderContent))

module.exports = router;