-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create menus routes for navigation + footer (#11)
- Loading branch information
1 parent
429a351
commit 8605eea
Showing
2 changed files
with
75 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const jwtUtils = require('../utils/jwt-utils') | ||
|
||
// Import classes | ||
const { File, DataType } = require('../classes/File.js') | ||
|
||
// List menus | ||
router.get('/:siteName/menus', async function(req, res, next) { | ||
try { | ||
const { oauthtoken } = req.cookies | ||
const { access_token } = jwtUtils.verifyToken(oauthtoken) | ||
|
||
const { siteName } = req.params | ||
|
||
const GitHubFile = new File(access_token, siteName) | ||
const dataType = new DataType() | ||
GitHubFile.setFileType(dataType) | ||
const menus = await GitHubFile.list() | ||
|
||
// TO-DO: | ||
// Validate content | ||
|
||
res.status(200).json({ menus }) | ||
} catch (err) { | ||
console.log(err) | ||
} | ||
}) | ||
|
||
// Read menu | ||
router.get('/:siteName/menus/:menuName', async function(req, res, next) { | ||
try { | ||
const { oauthtoken } = req.cookies | ||
const { access_token } = jwtUtils.verifyToken(oauthtoken) | ||
|
||
const { siteName, menuName } = req.params | ||
|
||
const GitHubFile = new File(access_token, siteName) | ||
const dataType = new DataType() | ||
GitHubFile.setFileType(dataType) | ||
const { sha, content } = await GitHubFile.read(menuName) | ||
|
||
// TO-DO: | ||
// Validate content | ||
|
||
res.status(200).json({ menuName, sha, content }) | ||
} catch (err) { | ||
console.log(err) | ||
} | ||
}) | ||
|
||
// Update menu | ||
router.post('/:siteName/menus/:menuName', async function(req, res, next) { | ||
try { | ||
const { oauthtoken } = req.cookies | ||
const { access_token } = jwtUtils.verifyToken(oauthtoken) | ||
|
||
const { siteName, menuName } = req.params | ||
const { content, sha } = req.body | ||
|
||
// TO-DO: | ||
// Validate imageName and content | ||
|
||
const GitHubFile = new File(access_token, siteName) | ||
const dataType = new DataType() | ||
GitHubFile.setFileType(dataType) | ||
const { newSha } = await GitHubFile.update(menuName, content, sha) | ||
|
||
res.status(200).json({ menuName, content, sha: newSha }) | ||
} catch (err) { | ||
console.log(err) | ||
} | ||
}) | ||
|
||
module.exports = router; |
This file was deleted.
Oops, something went wrong.