update ORM, refactor models, add Page API endpoints
This commit is contained in:
@@ -14,39 +14,6 @@ class Misc extends Controller {
|
||||
hello: 'world',
|
||||
})
|
||||
}
|
||||
|
||||
async save_page(req, res) {
|
||||
// return res.status(400).message('Missing required field: some_field').api({})
|
||||
|
||||
// Name, Parent, originalID
|
||||
requried_fields = {
|
||||
Name: String,
|
||||
Parent: ObjectId,
|
||||
OriginalId: ObjectId
|
||||
}
|
||||
|
||||
requried_fields.name = req.name
|
||||
requried_fields.Parrent = req.Parrent
|
||||
requried_fields.OriginalId = req.OriginalId
|
||||
|
||||
if (!requried_fields.Name) {
|
||||
return res.status(400).message('Missing required field: Name').api({})
|
||||
} else if (!requried_fields.Parrent) {
|
||||
return res.status(400).message('Missing required field: Parent').api({})
|
||||
|
||||
} else if (!requried_fields.ObjectId) {
|
||||
return res.status(400).message('Missing required field: ObjectId').api({})
|
||||
}
|
||||
|
||||
if (req.body.PageId) {
|
||||
//use the page model to find by id and
|
||||
const page = await Page.findById(req.body.PageId)
|
||||
if (!page) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = Misc
|
||||
|
||||
62
app/controllers/api/v1/Page.controller.js
Normal file
62
app/controllers/api/v1/Page.controller.js
Normal file
@@ -0,0 +1,62 @@
|
||||
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
|
||||
@@ -1,8 +1,5 @@
|
||||
const Model = require("flitter-orm/src/model/Model");
|
||||
const { ObjectId } = require("mongodb");
|
||||
|
||||
//custom
|
||||
const NodeData = require("./NodeData.model");
|
||||
const uuid = require('uuid/v4');
|
||||
|
||||
/*
|
||||
* Node Model
|
||||
@@ -13,13 +10,17 @@ class Node extends Model {
|
||||
static get schema() {
|
||||
// Return a flitter-orm schema here.
|
||||
return {
|
||||
UUID: { type: String, default: () => uuid() },
|
||||
Type: String,
|
||||
Value: NodeData,
|
||||
PageId: ObjectId,
|
||||
CreatedAt: { type: Date, default: () => new Date() },
|
||||
UpdatedAt: { type: Date, default: () => new Date() },
|
||||
CreatedUserId: { type: ObjectId },
|
||||
UpdateUserId: { type: ObjectId }
|
||||
Value: {
|
||||
Mode: {type: String, default: 'norm'},
|
||||
Value: String,
|
||||
},
|
||||
PageId: String,
|
||||
CreatedAt: { type: Date, default: () => new Date },
|
||||
UpdatedAt: { type: Date, default: () => new Date },
|
||||
CreatedUserId: String,
|
||||
UpdateUserId: String
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
const Model = require("flitter-orm/src/model/Model");
|
||||
|
||||
/*
|
||||
* NodeData Model
|
||||
* -------------------------------------------------------------
|
||||
* Put some description here!
|
||||
*/
|
||||
class NodeData extends Model {
|
||||
static get schema() {
|
||||
// Return a flitter-orm schema here.
|
||||
return {
|
||||
// Will add
|
||||
Mode: { type: String, default: "norm" }, //Select the value (norm = string)
|
||||
Value: String
|
||||
};
|
||||
}
|
||||
|
||||
// Static and instance methods can go here
|
||||
}
|
||||
|
||||
module.exports = exports = NodeData;
|
||||
@@ -1,5 +1,6 @@
|
||||
const Model = require("flitter-orm/src/model/Model");
|
||||
const { ObjectId } = require("mongodb");
|
||||
const uuid = require('uuid/v4');
|
||||
|
||||
/*
|
||||
* Page Model
|
||||
@@ -13,17 +14,18 @@ class Page extends Model {
|
||||
static get schema() {
|
||||
// Return a flitter-orm schema here.
|
||||
return {
|
||||
UUID: {type: String, default: () => uuid()},
|
||||
Name: String,
|
||||
OrgUserId: ObjectId,
|
||||
IsPublic: { type: Boolean, default: true },
|
||||
IsVisibleInMenu: { type: Boolean, default: true },
|
||||
ParentId: ObjectId,
|
||||
NodeIds: [ObjectId],
|
||||
CreatedAt: { type: Date, default: () => new Date() },
|
||||
UpdatedAt: { type: Date, default: () => new Date() },
|
||||
CreatedUserId: { type: ObjectId },
|
||||
UpdateUserId: { type: ObjectId },
|
||||
ChildPageIds: { type: ObjectId }
|
||||
OrgUserId: String,
|
||||
IsPublic: {type: Boolean, default: true},
|
||||
IsVisibleInMenu: {type: Boolean, default: true},
|
||||
ParentId: String,
|
||||
NodeIds: [String],
|
||||
CreatedAt: {type: Date, default: () => new Date},
|
||||
UpdatedAt: {type: Date, default: () => new Date},
|
||||
CreatedUserId: {type: String},
|
||||
UpdateUserId: {type: String},
|
||||
ChildPageIds: [String],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -47,6 +49,10 @@ class Page extends Model {
|
||||
return this.belongs_to_one(Parent, "ParentId", "_id")
|
||||
}
|
||||
|
||||
accessible_by(user, mode = 'view') {
|
||||
return user.can(`page:${this.UUID}:${mode}`)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = exports = Page;
|
||||
|
||||
@@ -25,7 +25,7 @@ const index = {
|
||||
* handler's exec() method.
|
||||
*/
|
||||
middleware: [
|
||||
|
||||
'auth:UserOnly',
|
||||
],
|
||||
|
||||
/*
|
||||
@@ -37,11 +37,11 @@ const index = {
|
||||
*/
|
||||
get: {
|
||||
'/hello_world': ['controller::api:v1:Misc.hello_world'],
|
||||
'/page/:PageId': ['controller::api:v1:Page.get_page'],
|
||||
},
|
||||
|
||||
post: {
|
||||
'/page/save': ['middleware::auth:UserOnly', 'controller::api:v1:Misc.save_page']
|
||||
|
||||
'/page/:PageId/save': ['controller::api:v1:Page.save_page'],
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user