Add endpoint for saving a single node to a page
This commit is contained in:
parent
7652bef29f
commit
9beb0d2c3a
@ -84,6 +84,46 @@ class Page extends Controller {
|
|||||||
return res.api(return_nodes);
|
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) {
|
async save_nodes(req, res) {
|
||||||
const PageId = req.params.PageId
|
const PageId = req.params.PageId
|
||||||
|
|
||||||
|
@ -65,6 +65,8 @@ const index = {
|
|||||||
// Save the node data for the specified page
|
// Save the node data for the specified page
|
||||||
'/page/:PageId/nodes/save': ['controller::api:v1:Page.save_nodes'],
|
'/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
|
// Create a new page in the personal root
|
||||||
'/page/create': ['controller::api:v1:Page.create_top_level'],
|
'/page/create': ['controller::api:v1:Page.create_top_level'],
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user