Task #21 - add page controller and page/node api endpoints

This commit is contained in:
garrettmills
2020-02-08 11:16:04 -06:00
parent 7c8bcd928f
commit 617a77b809
5 changed files with 152 additions and 3 deletions

View File

@@ -10,6 +10,7 @@
*/
const Middleware = [
"auth:Utility",
"api:GuaranteeRootNode",
// 'MiddlewareName',

View File

@@ -0,0 +1,43 @@
const Middleware = require('libflitter/middleware/Middleware')
/*
* GuaranteeRootNode Middleware
* -------------------------------------------------------------
* Put some description here!
*/
class GuaranteeRootNode extends Middleware {
static get services() {
return [...super.services, 'models']
}
/*
* Run the middleware test.
* This method is required by all Flitter middleware.
* It should either call the next function in the stack,
* or it should handle the response accordingly.
*/
async test(req, res, next, args = {}){
const Page = this.models.get('api:Page')
if ( req.user ) {
const root_page = await Page.findOne({ OrgUserId: req.user._id, ParentId: "0" })
if ( !root_page ) {
const new_page = new Page({
Name: `${req.user.uid} virtual root`,
OrgUserId: req.user._id,
ParentId: "0",
CreatedUserId: req.user.id,
UpdateUserId: req.user.id,
})
await new_page.save()
}
}
/*
* Call the next function in the stack.
*/
next()
}
}
module.exports = exports = GuaranteeRootNode

View File

@@ -38,10 +38,12 @@ const index = {
get: {
'/hello_world': ['controller::api:v1:Misc.hello_world'],
'/page/:PageId': ['controller::api:v1:Page.get_page'],
'/page/:PageId/nodes': ['controller::api:v1:Page.get_nodes'],
},
post: {
'/page/:PageId/save': ['controller::api:v1:Page.save_page'],
'/page/:PageId/nodes/save': ['controller::api:v1:Page.save_nodes'],
},
}