Add delete for pages, active scope to model
This commit is contained in:
parent
29a873559b
commit
f720e334b3
@ -155,6 +155,112 @@ class Page extends Controller {
|
|||||||
// Make sure the new nodes are created
|
// Make sure the new nodes are created
|
||||||
// Make sure the order is correct
|
// 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
|
module.exports = exports = Page
|
||||||
|
@ -2,6 +2,8 @@ const Model = require("flitter-orm/src/model/Model");
|
|||||||
const { ObjectId } = require("mongodb");
|
const { ObjectId } = require("mongodb");
|
||||||
const uuid = require('uuid/v4');
|
const uuid = require('uuid/v4');
|
||||||
|
|
||||||
|
const ActiveScope = require('../scopes/Active.scope')
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Page Model
|
* Page Model
|
||||||
* -------------------------------------------------------------
|
* -------------------------------------------------------------
|
||||||
@ -23,12 +25,16 @@ class Page extends Model {
|
|||||||
NodeIds: [String],
|
NodeIds: [String],
|
||||||
CreatedAt: {type: Date, default: () => new Date},
|
CreatedAt: {type: Date, default: () => new Date},
|
||||||
UpdatedAt: {type: Date, default: () => new Date},
|
UpdatedAt: {type: Date, default: () => new Date},
|
||||||
|
DeletedAt: Date,
|
||||||
|
Active: {type: Boolean, default: true},
|
||||||
CreatedUserId: {type: String},
|
CreatedUserId: {type: String},
|
||||||
UpdateUserId: {type: String},
|
UpdateUserId: {type: String},
|
||||||
ChildPageIds: [String],
|
ChildPageIds: [String],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static scopes = [new ActiveScope]
|
||||||
|
|
||||||
// Static and instance methods can go here
|
// Static and instance methods can go here
|
||||||
get user() {
|
get user() {
|
||||||
const User = this.models.get("auth:User");
|
const User = this.models.get("auth:User");
|
||||||
|
9
app/models/scopes/Active.scope.js
Normal file
9
app/models/scopes/Active.scope.js
Normal 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
|
@ -46,6 +46,9 @@ const index = {
|
|||||||
post: {
|
post: {
|
||||||
'/page/:PageId/save': ['controller::api:v1:Page.save_page'],
|
'/page/:PageId/save': ['controller::api:v1:Page.save_page'],
|
||||||
'/page/:PageId/nodes/save': ['controller::api:v1:Page.save_nodes'],
|
'/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'],
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user