Skip to content

Commit

Permalink
Feat/new-add-collection-page-endpoint (#122)
Browse files Browse the repository at this point in the history
* Feat/get all collections directories (#117)

* feat: add RootType Directory

This commit adds a RootType to the Directory class so that we can
retrieve directories from the root of a repo.

* feat: new endpoint for retrieving information on all folders

This commit introduces a new GET `/:siteName/folders/all` endpoint
which:
1) gets the names of all collections/folders within the repo
2) gets the collection.yml directory config file for each collection

* fix: remove _ before sending folder name

Co-authored-by: jiehao <[email protected]>

* feat: add function for creating new collection page

* feat: add route for creating new collection page

* fix: updates createCollectionPage endpoint to use /new in endpoint url

* feat: creates equivalent createPage endpoint for creating new unlinked pages

* fix: updates authenticated endpoints on backend

* fix: updates unlinkedPages to use Directory for .list()

* fix: removes unused listUnlinkedPages route

Co-authored-by: kwajiehao <[email protected]>
Co-authored-by: jiehao <[email protected]>
  • Loading branch information
3 people authored Mar 10, 2021
1 parent a52ee50 commit a2732a8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
7 changes: 4 additions & 3 deletions middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ auth.get('/v1/sites/:siteName/folders/all', verifyJwt)
// Collection pages
auth.get('/v1/sites/:siteName/collections/:collectionName', verifyJwt)
auth.get('/v1/sites/:siteName/collections/:collectionName/pages', verifyJwt)
auth.post('/v1/sites/:siteName/collections/:collectionName/pages', verifyJwt)
auth.post('/v1/sites/:siteName/collections/:collectionName/pages', verifyJwt) // to remove
auth.post('/v1/sites/:siteName/collections/:collectionName/pages/new/:pageName', verifyJwt)
auth.get('/v1/sites/:siteName/collections/:collectionName/pages/:pageName', verifyJwt)
auth.post('/v1/sites/:siteName/collections/:collectionName/pages/:pageName', verifyJwt)
auth.delete('/v1/sites/:siteName/collections/:collectionName/pages/:pageName', verifyJwt)
Expand Down Expand Up @@ -94,8 +95,8 @@ auth.post('/v1/sites/:siteName/menus/:menuName', verifyJwt)

// Pages
auth.get('/v1/sites/:siteName/pages', verifyJwt)
auth.get('/v1/sites/:siteName/unlinkedPages', verifyJwt)
auth.post('/v1/sites/:siteName/pages', verifyJwt)
auth.post('/v1/sites/:siteName/pages', verifyJwt) // to remove
auth.post('/v1/sites/:siteName/pages/new/:pageName', verifyJwt)
auth.get('/v1/sites/:siteName/pages/:pageName', verifyJwt)
auth.post('/v1/sites/:siteName/pages/:pageName', verifyJwt)
auth.delete('/v1/sites/:siteName/pages/:pageName', verifyJwt)
Expand Down
27 changes: 24 additions & 3 deletions routes/collectionPages.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ const {
} = require('../middleware/routeHandler')

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

// Import utils
Expand Down Expand Up @@ -123,6 +124,25 @@ async function createNewcollectionPage (req, res, next) {
res.status(200).json({ collectionName, pageName, content, sha })
}

// // Create new page in collection
async function createCollectionPage (req, res, next) {
const { accessToken } = req

const { siteName, collectionName, pageName: encodedPageName } = req.params
const { content: pageContent } = req.body
const pageName = decodeURIComponent(encodedPageName)

const IsomerFile = new File(accessToken, siteName)
const collectionPageType = new CollectionPageType(collectionName)
IsomerFile.setFileType(collectionPageType)
await IsomerFile.create(pageName, Base64.encode(pageContent))

const config = new CollectionConfig(accessToken, siteName, collectionName)
await config.addItemToOrder(pageName)

res.status(200).json({collectionName, pageName, pageContent })
}

// Read page in collection
async function readCollectionPage(req, res, next) {
const { accessToken } = req
Expand Down Expand Up @@ -207,7 +227,8 @@ async function renameCollectionPage (req, res, next) {

router.get('/:siteName/collections/:collectionName', attachReadRouteHandlerWrapper(listCollectionPages))
router.get('/:siteName/collections/:collectionName/pages', attachReadRouteHandlerWrapper(listCollectionPagesDetails))
router.post('/:siteName/collections/:collectionName/pages', attachRollbackRouteHandlerWrapper(createNewcollectionPage))
router.post('/:siteName/collections/:collectionName/pages', attachRollbackRouteHandlerWrapper(createNewcollectionPage)) // to remove
router.post('/:siteName/collections/:collectionName/pages/new/:pageName', attachRollbackRouteHandlerWrapper(createCollectionPage))
router.get('/:siteName/collections/:collectionName/pages/:pageName', attachReadRouteHandlerWrapper(readCollectionPage))
router.post('/:siteName/collections/:collectionName/pages/:pageName', attachWriteRouteHandlerWrapper(updateCollectionPage))
router.delete('/:siteName/collections/:collectionName/pages/:pageName', attachRollbackRouteHandlerWrapper(deleteCollectionPage))
Expand Down
36 changes: 18 additions & 18 deletions routes/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,9 @@ const {
// Import classes
const { File, PageType, CollectionPageType } = require('../classes/File.js')
const { Collection } = require('../classes/Collection.js');
const { Directory, FolderType } = require('../classes/Directory');
const { create } = require('lodash');

const getUnlinkedPages = async (accessToken, siteName) => {
const IsomerFile = new File(accessToken, siteName)
const pageType = new PageType()
IsomerFile.setFileType(pageType)
const simplePages = await IsomerFile.list()
return simplePages
}

async function listUnlinkedPages (req, res, next) {
const { accessToken } = req
const { siteName } = req.params

const unlinkedPages = await getUnlinkedPages(accessToken, siteName)
res.status(200).json({ pages: unlinkedPages })
}

// List both simple pages and collection pages
async function listPages (req, res, next) {
const { accessToken } = req
Expand Down Expand Up @@ -91,6 +76,21 @@ async function createNewPage (req, res, next) {
res.status(200).json({ pageName, content, sha })
}

async function createPage (req, res, next) {
const { accessToken } = req

const { siteName, pageName: encodedPageName } = req.params
const { content: pageContent } = req.body
const pageName = decodeURIComponent(encodedPageName)

const IsomerFile = new File(accessToken, siteName)
const pageType = new PageType()
IsomerFile.setFileType(pageType)
await IsomerFile.create(pageName, Base64.encode(pageContent))

res.status(200).json({ pageName, pageContent })
}

// Read page
async function readPage(req, res, next) {
const { accessToken } = req
Expand Down Expand Up @@ -162,8 +162,8 @@ async function renamePage(req, res, next) {


router.get('/:siteName/pages', attachReadRouteHandlerWrapper(listPages))
router.get('/:siteName/unlinkedPages', attachReadRouteHandlerWrapper(listUnlinkedPages))
router.post('/:siteName/pages', attachWriteRouteHandlerWrapper(createNewPage))
router.post('/:siteName/pages', attachWriteRouteHandlerWrapper(createNewPage)) // to remove
router.post('/:siteName/pages/new/:pageName', attachWriteRouteHandlerWrapper(createPage))
router.get('/:siteName/pages/:pageName', attachReadRouteHandlerWrapper(readPage))
router.post('/:siteName/pages/:pageName', attachWriteRouteHandlerWrapper(updatePage))
router.delete('/:siteName/pages/:pageName', attachWriteRouteHandlerWrapper(deletePage))
Expand Down

0 comments on commit a2732a8

Please sign in to comment.