Add endpoint for saving a single node to a page
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing

This commit is contained in:
Garrett Mills 2020-10-14 09:23:44 -05:00
parent 7652bef29f
commit 9beb0d2c3a
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246
2 changed files with 42 additions and 0 deletions

View File

@ -84,6 +84,46 @@ class Page extends Controller {
return res.api(return_nodes);
}
async save_node_to_page(req, res) {
const PageId = req.params.PageId
const page = await PageModel.findOne({UUID: PageId})
if ( !page ) return res.status(404).message('Page not found with that ID.').api({})
if ( !(await page.is_accessible_by(req.user, 'update')) ) return req.security.deny()
const nodes = await Node.find({PageId: page.UUID})
const assoc_nodes = {}
nodes.forEach(node => {
assoc_nodes[node.UUID] = node
})
if ( !req.body.nodeData ) {
return res.status(400).message('Missing nodeData body').api()
}
const node = req.body.nodeData;
if ( node.UUID && assoc_nodes[node.UUID] ) {
assoc_nodes[node.UUID].update_from_raw(node)
assoc_nodes[node.UUID].UpdatedAt = new Date
assoc_nodes[node.UUID].UpdateUserId = req.user._id
assoc_nodes[node.UUID].save()
return res.api(assoc_nodes[node.UUID])
} else {
const node_obj = new Node({
Type: node.Type,
Value: node.Value,
PageId,
CreatedUserId: req.user._id,
UpdateUserId: req.user._id,
})
await node_obj.save()
page.NodeIds.push(node_obj.UUID);
await page.save();
return res.api(node_obj)
}
}
async save_nodes(req, res) {
const PageId = req.params.PageId

View File

@ -65,6 +65,8 @@ const index = {
// Save the node data for the specified page
'/page/:PageId/nodes/save': ['controller::api:v1:Page.save_nodes'],
'/page/:PageId/nodes/save_one': ['controller::api:v1:Page.save_node_to_page'],
// Create a new page in the personal root
'/page/create': ['controller::api:v1:Page.create_top_level'],