Add delete for pages, active scope to model

This commit is contained in:
garrettmills 2020-02-08 15:27:53 -06:00
parent 29a873559b
commit f720e334b3
4 changed files with 124 additions and 0 deletions

View File

@ -155,6 +155,112 @@ class Page extends Controller {
// Make sure the new nodes are created
// Make sure the order is correct
}
async create_top_level(req, res) {
if ( !req.body.name ) {
return res.status(400).message('Missing required field: name').api({})
}
const root_page = await req.user.get_root_page()
const new_page = new PageModel({
Name: req.body.name,
OrgUserId: req.user._id,
ParentId: root_page.UUID,
CreatedUserId: req.user.id,
UpdateUserId: req.user.id
})
await new_page.save()
root_page.ChildPageIds.push(new_page.UUID)
await root_page.save()
req.user.allow(`page:${new_page.UUID}`)
await req.user.save()
const starter_node = new Node({
Type: 'paragraph',
Value: {
Value: 'Click to edit...',
},
PageId: new_page.UUID,
CreatedUserId: req.user.id,
UpdateUserId: req.user.id
})
await starter_node.save()
new_page.NodeIds.push(starter_node.UUID)
await new_page.save()
return res.api(new_page)
}
async create_child(req, res) {
if ( !req.body.name ) {
return res.status(400).message('Missing required field: name').api({})
}
if ( !req.body.parentId ) {
return res.status(400).message('Missing required field: parentId').api({})
}
// Get the parent
const parent = await PageModel.findOne({UUID: req.body.parentId})
if ( !parent ) {
return res.status(404).message('Unable to find parent with that ID.').api({})
} else if ( !parent.accessible_by(req.user, 'update') ) {
return res.security.deny()
}
const new_page = new PageModel({
Name: req.body.name,
OrgUserId: req.user._id,
ParentId: parent.UUID,
CreatedUserId: req.user.id,
UpdateUserId: req.user.id,
})
await new_page.save()
parent.ChildPageIds.push(new_page.UUID)
await parent.save()
req.user.allow(`page:${new_page.UUID}`)
await req.user.save()
const starter_node = new Node({
Type: 'paragraph',
Value: {
Value: 'Click to edit...',
},
PageId: new_page.UUID,
CreatedUserId: req.user.id,
UpdateUserId: req.user.id
})
await starter_node.save()
new_page.NodeIds.push(starter_node.UUID)
await new_page.save()
return res.api(new_page)
}
async delete_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()
}
page.Active = false
page.DeletedAt = new Date
await page.save()
return res.api({})
}
}
module.exports = exports = Page

View File

@ -2,6 +2,8 @@ const Model = require("flitter-orm/src/model/Model");
const { ObjectId } = require("mongodb");
const uuid = require('uuid/v4');
const ActiveScope = require('../scopes/Active.scope')
/*
* Page Model
* -------------------------------------------------------------
@ -23,12 +25,16 @@ class Page extends Model {
NodeIds: [String],
CreatedAt: {type: Date, default: () => new Date},
UpdatedAt: {type: Date, default: () => new Date},
DeletedAt: Date,
Active: {type: Boolean, default: true},
CreatedUserId: {type: String},
UpdateUserId: {type: String},
ChildPageIds: [String],
};
}
static scopes = [new ActiveScope]
// Static and instance methods can go here
get user() {
const User = this.models.get("auth:User");

View File

@ -0,0 +1,9 @@
const Scope = require('flitter-orm/src/model/Scope')
class ActiveScope extends Scope {
async filter(to_filter) {
return to_filter.equal('Active', true)
}
}
module.exports = exports = ActiveScope

View File

@ -46,6 +46,9 @@ const index = {
post: {
'/page/:PageId/save': ['controller::api:v1:Page.save_page'],
'/page/:PageId/nodes/save': ['controller::api:v1:Page.save_nodes'],
'/page/create': ['controller::api:v1:Page.create_top_level'],
'/page/create-child': ['controller::api:v1:Page.create_child'],
'/page/delete/:PageId': ['controller::api:v1:Page.delete_page'],
},
}