From a2732a882f771c3cc9f772422a8e1be88609616e Mon Sep 17 00:00:00 2001 From: gweiying <39231249+gweiying@users.noreply.github.com> Date: Wed, 10 Mar 2021 12:50:25 +0800 Subject: [PATCH] Feat/new-add-collection-page-endpoint (#122) * 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 * 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 <31984694+kwajiehao@users.noreply.github.com> Co-authored-by: jiehao --- middleware/auth.js | 7 ++++--- routes/collectionPages.js | 27 ++++++++++++++++++++++++--- routes/pages.js | 36 ++++++++++++++++++------------------ 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/middleware/auth.js b/middleware/auth.js index 003eaced9..9c278cd4f 100644 --- a/middleware/auth.js +++ b/middleware/auth.js @@ -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) @@ -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) diff --git a/routes/collectionPages.js b/routes/collectionPages.js index d35926ed0..57aa5075b 100644 --- a/routes/collectionPages.js +++ b/routes/collectionPages.js @@ -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 @@ -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 @@ -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)) diff --git a/routes/pages.js b/routes/pages.js index f8e7433ba..7543ea971 100644 --- a/routes/pages.js +++ b/routes/pages.js @@ -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 @@ -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 @@ -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))