Skip to content

Commit

Permalink
Feat/move resources (#139)
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 moveResources API

* fix: adds backend check for if resource categories exist prior to move

* fix: update resourcePages routes to handle Base64 encoding on backend

* fix: update moveResource route to work on single file only

* Revert "fix: update moveResource route to work on single file only"

This reverts commit 81b4786.

* fix: typo in auth for resources route

Co-authored-by: kwajiehao <[email protected]>
Co-authored-by: jiehao <[email protected]>
  • Loading branch information
3 people authored Mar 31, 2021
1 parent af16016 commit b382d2d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
2 changes: 2 additions & 0 deletions middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ auth.post('/v1/sites/:siteName/pages/move/:newPagePath', verifyJwt)
auth.get('/v1/sites/:siteName/resources/:resourceName', verifyJwt)
auth.post('/v1/sites/:siteName/resources/:resourceName/pages', verifyJwt)
auth.get('/v1/sites/:siteName/resources/:resourceName/pages/:pageName', verifyJwt)
auth.post('/v1/sites/:siteName/resources/:resourceName/pages/new/:pageName', verifyJwt)
auth.post('/v1/sites/:siteName/resources/:resourceName/pages/:pageName', verifyJwt)
auth.delete('/v1/sites/:siteName/resources/:resourceName/pages/:pageName', verifyJwt)
auth.post('/v1/sites/:siteName/resources/:resourceName/pages/:pageName/rename/:newPageName', verifyJwt)
Expand All @@ -125,6 +126,7 @@ auth.get('/v1/sites/:siteName/resources', verifyJwt)
auth.post('/v1/sites/:siteName/resources', verifyJwt)
auth.delete('/v1/sites/:siteName/resources/:resourceName', verifyJwt)
auth.post('/v1/sites/:siteName/resources/:resourceName/rename/:newResourceName', verifyJwt)
auth.post('/v1/sites/:siteName/resources/:resourceName/move/:newResourceName', verifyJwt)

// Settings
auth.get('/v1/sites/:siteName/settings', verifyJwt)
Expand Down
27 changes: 15 additions & 12 deletions routes/resourcePages.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const express = require('express');
const router = express.Router();
const base64 = require('base-64');

// Import middleware
const {
Expand Down Expand Up @@ -40,8 +41,8 @@ async function listResourcePages (req, res, next) {
async function createNewResourcePage(req, res, next) {
const { accessToken } = req

const { siteName, resourceName } = req.params
const { pageName, content } = req.body
const { siteName, resourceName, pageName } = req.params
const { content: pageContent } = req.body

// TO-DO:
// Validate pageName and content
Expand All @@ -59,9 +60,10 @@ async function createNewResourcePage(req, res, next) {
const IsomerFile = new File(accessToken, siteName)
const resourcePageType = new ResourcePageType(resourceRoomName, resourceName)
IsomerFile.setFileType(resourcePageType)
const { sha } = await IsomerFile.create(pageName, content)

res.status(200).json({ resourceName, pageName, content, sha })
const { sha } = await IsomerFile.create(pageName, Base64.encode(pageContent))

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

// Read page in resource
Expand All @@ -75,7 +77,8 @@ async function readResourcePage (req, res, next) {
const IsomerFile = new File(accessToken, siteName)
const resourcePageType = new ResourcePageType(resourceRoomName, resourceName)
IsomerFile.setFileType(resourcePageType)
const { sha, content } = await IsomerFile.read(pageName)
const { sha, content: encodedContent } = await IsomerFile.read(pageName)
const content = base64.decode(encodedContent)

// TO-DO:
// Validate content
Expand All @@ -88,7 +91,7 @@ async function updateResourcePage (req, res, next) {
const { accessToken } = req

const { siteName, pageName, resourceName } = req.params
const { content, sha } = req.body
const { content: pageContent, sha } = req.body

// TO-DO:
// Validate pageName and content
Expand All @@ -98,9 +101,9 @@ async function updateResourcePage (req, res, next) {
const IsomerFile = new File(accessToken, siteName)
const resourcePageType = new ResourcePageType(resourceRoomName, resourceName)
IsomerFile.setFileType(resourcePageType)
const { newSha } = await IsomerFile.update(pageName, content, sha)
const { newSha } = await IsomerFile.update(pageName, base64.encode(pageContent), sha)

res.status(200).json({ resourceName, pageName, content, sha: newSha })
res.status(200).json({ resourceName, pageName, pageContent, sha: newSha })
}

// Delete page in resource
Expand All @@ -125,7 +128,7 @@ async function renameResourcePage (req, res, next) {
const { accessToken } = req

const { siteName, pageName, resourceName, newPageName } = req.params
const { sha, content } = req.body
const { sha, content: pageContent } = req.body

// TO-DO:
// Validate that resource exists
Expand All @@ -138,13 +141,13 @@ async function renameResourcePage (req, res, next) {
const IsomerFile = new File(accessToken, siteName)
const resourcePageType = new ResourcePageType(resourceRoomName, resourceName)
IsomerFile.setFileType(resourcePageType)
const { sha: newSha } = await IsomerFile.create(newPageName, content)
const { sha: newSha } = await IsomerFile.create(newPageName, base64.encode(pageContent))
await IsomerFile.delete(pageName, sha)

res.status(200).json({ resourceName, pageName: newPageName, content, sha: newSha })
res.status(200).json({ resourceName, pageName: newPageName, pageContent, sha: newSha })
}
router.get('/:siteName/resources/:resourceName', attachReadRouteHandlerWrapper(listResourcePages))
router.post('/:siteName/resources/:resourceName/pages', attachRollbackRouteHandlerWrapper(createNewResourcePage))
router.post('/:siteName/resources/:resourceName/pages/new/:pageName', attachRollbackRouteHandlerWrapper(createNewResourcePage))
router.get('/:siteName/resources/:resourceName/pages/:pageName', attachReadRouteHandlerWrapper(readResourcePage))
router.post('/:siteName/resources/:resourceName/pages/:pageName', attachWriteRouteHandlerWrapper(updateResourcePage))
router.delete('/:siteName/resources/:resourceName/pages/:pageName', attachRollbackRouteHandlerWrapper(deleteResourcePage))
Expand Down
32 changes: 32 additions & 0 deletions routes/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { attachReadRouteHandlerWrapper, attachRollbackRouteHandlerWrapper } = req
// Import classes
const { ResourceRoom } = require('../classes/ResourceRoom.js')
const { Resource } = require('../classes/Resource.js')
const { File, ResourcePageType } = require('../classes/File');

// List resources
async function listResources (req, res, next) {
Expand Down Expand Up @@ -65,9 +66,40 @@ async function renameResource (req, res, next) {
res.status(200).json({ resourceName, newResourceName })
}

// Move resource
async function moveResources (req, res, next) {
const { accessToken } = req
const { siteName, resourceName, newResourceName } = req.params
const { files } = req.body

const ResourceRoomInstance = new ResourceRoom(accessToken, siteName)
const resourceRoomName = await ResourceRoomInstance.get()

const IsomerResource = new Resource(accessToken, siteName)
const resources = await IsomerResource.list(resourceRoomName)
const resourceCategories = resources.map(resource => resource.dirName)
if (!resourceCategories.includes(resourceName)) throw new NotFoundError(`Resource category ${resourceName} was not found!`)
if (!resourceCategories.includes(newResourceName)) throw new NotFoundError(`Resource category ${newResourceName} was not found!`)

const oldIsomerFile = new File(accessToken, siteName)
const newIsomerFile = new File(accessToken, siteName)
const oldResourcePageType = new ResourcePageType(resourceRoomName, resourceName)
const newResourcePageType = new ResourcePageType(resourceRoomName, newResourceName)
oldIsomerFile.setFileType(oldResourcePageType)
newIsomerFile.setFileType(newResourcePageType)

for (const fileName of files) {
const { content, sha } = await oldIsomerFile.read(fileName)
await oldIsomerFile.delete(fileName, sha)
await newIsomerFile.create(fileName, content)
}
res.status(200).send('OK')
}

router.get('/:siteName/resources', attachReadRouteHandlerWrapper(listResources))
router.post('/:siteName/resources', attachRollbackRouteHandlerWrapper(createNewResource))
router.delete('/:siteName/resources/:resourceName', attachRollbackRouteHandlerWrapper(deleteResource))
router.post('/:siteName/resources/:resourceName/rename/:newResourceName', attachRollbackRouteHandlerWrapper(renameResource))
router.post('/:siteName/resources/:resourceName/move/:newResourceName', attachRollbackRouteHandlerWrapper(moveResources))

module.exports = router;

0 comments on commit b382d2d

Please sign in to comment.