backend/app/controllers/api/v1/Page.controller.js

63 lines
2.0 KiB
JavaScript
Raw Normal View History

const Controller = require('libflitter/controller/Controller')
const PageModel = require('../../../models/api/Page.model')
/*
* Page Controller
* -------------------------------------------------------------
* Put some description here!
*/
class Page extends Controller {
async get_page(req, res) {
const PageId = req.params.PageId
const user = req.user
const page = await PageModel.findOne({UUID: PageId})
if ( !page ) return res.status(404).message('Page not found with that ID.').api({})
if ( !page.accessible_by(user) ) return req.security.deny()
return res.api(page)
}
async save_page(req, res) {
const PageId = req.params.PageId
let page;
if ( PageId ) {
page = await PageModel.findOne({UUID: PageId})
if ( !page ) return res.status(404).message('Page not found with that ID.').api({})
if ( !page.accessible_by(req.user, 'update') ) return req.security.deny()
} else {
page = new PageModel
page.CreatedUserId = req.user.id
page.OrgUserId = req.user.id
}
if ( !req.body.Name ) return res.status(400).message('Missing required: Name').api({})
page.Name = req.body.Name
if ( 'IsPublic' in req.body ) {
page.IsPublic = !!req.body.IsPublic
}
if ( 'IsVisibleInMenu' in req.body ) {
page.IsVisibleInMenu = !!req.body.IsVisibleInMenu
}
let parent;
if ( !req.body.ParentId ) return res.status(400).message('Missing required: ParentId').api({})
else {
parent = await PageModel.findOne({UUID: req.body.ParentId})
if ( !parent ) return res.status(404).message('Parent page not found with that ID.').api({})
if ( !parent.accessible_by(req.user, 'update') ) return req.security.kickout()
}
page.UpdatedAt = new Date
page.UpdateUserId = req.user.id
await page.save()
return req.api(page)
}
}
module.exports = exports = Page