update ORM, refactor models, add Page API endpoints
This commit is contained in:
parent
7f832c0d92
commit
26d6bf5721
@ -14,39 +14,6 @@ class Misc extends Controller {
|
|||||||
hello: 'world',
|
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
|
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 Model = require("flitter-orm/src/model/Model");
|
||||||
const { ObjectId } = require("mongodb");
|
const uuid = require('uuid/v4');
|
||||||
|
|
||||||
//custom
|
|
||||||
const NodeData = require("./NodeData.model");
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Node Model
|
* Node Model
|
||||||
@ -13,13 +10,17 @@ class Node extends Model {
|
|||||||
static get schema() {
|
static get schema() {
|
||||||
// Return a flitter-orm schema here.
|
// Return a flitter-orm schema here.
|
||||||
return {
|
return {
|
||||||
|
UUID: { type: String, default: () => uuid() },
|
||||||
Type: String,
|
Type: String,
|
||||||
Value: NodeData,
|
Value: {
|
||||||
PageId: ObjectId,
|
Mode: {type: String, default: 'norm'},
|
||||||
CreatedAt: { type: Date, default: () => new Date() },
|
Value: String,
|
||||||
UpdatedAt: { type: Date, default: () => new Date() },
|
},
|
||||||
CreatedUserId: { type: ObjectId },
|
PageId: String,
|
||||||
UpdateUserId: { type: ObjectId }
|
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 Model = require("flitter-orm/src/model/Model");
|
||||||
const { ObjectId } = require("mongodb");
|
const { ObjectId } = require("mongodb");
|
||||||
|
const uuid = require('uuid/v4');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Page Model
|
* Page Model
|
||||||
@ -13,17 +14,18 @@ class Page extends Model {
|
|||||||
static get schema() {
|
static get schema() {
|
||||||
// Return a flitter-orm schema here.
|
// Return a flitter-orm schema here.
|
||||||
return {
|
return {
|
||||||
|
UUID: {type: String, default: () => uuid()},
|
||||||
Name: String,
|
Name: String,
|
||||||
OrgUserId: ObjectId,
|
OrgUserId: String,
|
||||||
IsPublic: { type: Boolean, default: true },
|
IsPublic: {type: Boolean, default: true},
|
||||||
IsVisibleInMenu: { type: Boolean, default: true },
|
IsVisibleInMenu: {type: Boolean, default: true},
|
||||||
ParentId: ObjectId,
|
ParentId: String,
|
||||||
NodeIds: [ObjectId],
|
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},
|
||||||
CreatedUserId: { type: ObjectId },
|
CreatedUserId: {type: String},
|
||||||
UpdateUserId: { type: ObjectId },
|
UpdateUserId: {type: String},
|
||||||
ChildPageIds: { type: ObjectId }
|
ChildPageIds: [String],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,6 +49,10 @@ class Page extends Model {
|
|||||||
return this.belongs_to_one(Parent, "ParentId", "_id")
|
return this.belongs_to_one(Parent, "ParentId", "_id")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
accessible_by(user, mode = 'view') {
|
||||||
|
return user.can(`page:${this.UUID}:${mode}`)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = exports = Page;
|
module.exports = exports = Page;
|
||||||
|
@ -25,7 +25,7 @@ const index = {
|
|||||||
* handler's exec() method.
|
* handler's exec() method.
|
||||||
*/
|
*/
|
||||||
middleware: [
|
middleware: [
|
||||||
|
'auth:UserOnly',
|
||||||
],
|
],
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -37,11 +37,11 @@ const index = {
|
|||||||
*/
|
*/
|
||||||
get: {
|
get: {
|
||||||
'/hello_world': ['controller::api:v1:Misc.hello_world'],
|
'/hello_world': ['controller::api:v1:Misc.hello_world'],
|
||||||
|
'/page/:PageId': ['controller::api:v1:Page.get_page'],
|
||||||
},
|
},
|
||||||
|
|
||||||
post: {
|
post: {
|
||||||
'/page/save': ['middleware::auth:UserOnly', 'controller::api:v1:Misc.save_page']
|
'/page/:PageId/save': ['controller::api:v1:Page.save_page'],
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,13 +17,13 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"flitter-auth": "^0.18.0",
|
"flitter-auth": "^0.18.1",
|
||||||
"flitter-cli": "^0.15.2",
|
"flitter-cli": "^0.15.2",
|
||||||
"flitter-di": "^0.4.1",
|
"flitter-di": "^0.4.1",
|
||||||
"flitter-flap": "^0.5.2",
|
"flitter-flap": "^0.5.2",
|
||||||
"flitter-forms": "^0.8.1",
|
"flitter-forms": "^0.8.1",
|
||||||
"flitter-orm": "^0.2.0",
|
"flitter-orm": "^0.2.1",
|
||||||
"flitter-upload": "^0.8.0",
|
"flitter-upload": "^0.8.0",
|
||||||
"libflitter": "^0.46.2"
|
"libflitter": "^0.46.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
26
yarn.lock
26
yarn.lock
@ -976,10 +976,10 @@ find-replace@^3.0.0:
|
|||||||
dependencies:
|
dependencies:
|
||||||
array-back "^3.0.1"
|
array-back "^3.0.1"
|
||||||
|
|
||||||
flitter-auth@^0.18.0:
|
flitter-auth@^0.18.1:
|
||||||
version "0.18.0"
|
version "0.18.1"
|
||||||
resolved "https://registry.yarnpkg.com/flitter-auth/-/flitter-auth-0.18.0.tgz#d27bede9ea73147c38e2d9ddb8b52abd12b7ab03"
|
resolved "https://registry.yarnpkg.com/flitter-auth/-/flitter-auth-0.18.1.tgz#e6d2e6a909bb82c3b1994b67e44bdfb8683daecb"
|
||||||
integrity sha512-KTBB7OouBoU7hz1spjgkWIMLuc9IWRhvOiGsPy5Y3Ou/W7oKpOy3P3b0UwXMvwzuhPK4L/QDD8LljGHtzCuOZw==
|
integrity sha512-s3Q5EN54I9EQsWa30MmTIMmzPA7+/F0Fx6bfwHZw7qyyRoaYwu3Uf7TNF2KTTdpeQD3lO2fB93OELcRmbBYxyA==
|
||||||
dependencies:
|
dependencies:
|
||||||
axios "^0.19.0"
|
axios "^0.19.0"
|
||||||
bcrypt "^3.0.4"
|
bcrypt "^3.0.4"
|
||||||
@ -1041,10 +1041,10 @@ flitter-forms@^0.8.1:
|
|||||||
recursive-readdir "^2.2.2"
|
recursive-readdir "^2.2.2"
|
||||||
validator "^10.11.0"
|
validator "^10.11.0"
|
||||||
|
|
||||||
flitter-orm@^0.2.0:
|
flitter-orm@^0.2.1:
|
||||||
version "0.2.0"
|
version "0.2.1"
|
||||||
resolved "https://registry.yarnpkg.com/flitter-orm/-/flitter-orm-0.2.0.tgz#a1ab29e5e2b5659309e4fe3bc6c93c9729d61fe5"
|
resolved "https://registry.yarnpkg.com/flitter-orm/-/flitter-orm-0.2.1.tgz#d73e85c88995e25f62b1e65086e1cdc0f005974b"
|
||||||
integrity sha512-nDhrI4bq+L5jOYsN5gNSf5inEyxtDTK8A9yXIjFWRohw+kUJOgwdD0xd5pOwU6ZshUowJTk1MtDSfCA8uvjk3A==
|
integrity sha512-2e+G1JHm8NQXQNkLI1zH//eM54KtaoKB4mm3Q3AkuZUPbGzNiVNuNw2J/A4dJ505W9PzFBUL09aXz7lGpyVV8w==
|
||||||
dependencies:
|
dependencies:
|
||||||
flitter-di "^0.4.0"
|
flitter-di "^0.4.0"
|
||||||
json-stringify-safe "^5.0.1"
|
json-stringify-safe "^5.0.1"
|
||||||
@ -1508,10 +1508,10 @@ leven@^1.0.2:
|
|||||||
resolved "https://registry.yarnpkg.com/leven/-/leven-1.0.2.tgz#9144b6eebca5f1d0680169f1a6770dcea60b75c3"
|
resolved "https://registry.yarnpkg.com/leven/-/leven-1.0.2.tgz#9144b6eebca5f1d0680169f1a6770dcea60b75c3"
|
||||||
integrity sha1-kUS27ryl8dBoAWnxpncNzqYLdcM=
|
integrity sha1-kUS27ryl8dBoAWnxpncNzqYLdcM=
|
||||||
|
|
||||||
libflitter@^0.46.2:
|
libflitter@^0.46.3:
|
||||||
version "0.46.2"
|
version "0.46.3"
|
||||||
resolved "https://registry.yarnpkg.com/libflitter/-/libflitter-0.46.2.tgz#0f2d441fe6b795d10aabcf982be8bcdd08a1cb1d"
|
resolved "https://registry.yarnpkg.com/libflitter/-/libflitter-0.46.3.tgz#26381648d2bf9e41559dfc28bf9508b0d3c56156"
|
||||||
integrity sha512-aHLJ5GsHxitwBcfhr5CeFPRz7Mna5rq1PZ68UjMXmi+y2HTwMgaAqrvZTNIoZViIHK9VW5TLvdzYeFFBgwYxFQ==
|
integrity sha512-5VS0y8ooz+Dke9M+fPNyO5TUd6lS1FmMcrTugA7a8U9kjpMnnNUfn1GMAM6HycPMmY6SVUetAb20TCL/N3kuUQ==
|
||||||
dependencies:
|
dependencies:
|
||||||
colors "^1.3.3"
|
colors "^1.3.3"
|
||||||
connect-mongodb-session "^2.2.0"
|
connect-mongodb-session "^2.2.0"
|
||||||
@ -1523,7 +1523,7 @@ libflitter@^0.46.2:
|
|||||||
express-graphql "^0.9.0"
|
express-graphql "^0.9.0"
|
||||||
express-session "^1.15.6"
|
express-session "^1.15.6"
|
||||||
flitter-di "^0.4.0"
|
flitter-di "^0.4.0"
|
||||||
flitter-orm "^0.2.0"
|
flitter-orm "^0.2.1"
|
||||||
graphql "^14.5.4"
|
graphql "^14.5.4"
|
||||||
http-status "^1.4.2"
|
http-status "^1.4.2"
|
||||||
mongo-schematic-class "^1.0.3"
|
mongo-schematic-class "^1.0.3"
|
||||||
|
Loading…
Reference in New Issue
Block a user