update ORM, refactor models, add Page API endpoints

This commit is contained in:
garrettmills 2020-02-08 06:57:38 -06:00
parent 7f832c0d92
commit 26d6bf5721
8 changed files with 108 additions and 93 deletions

View File

@ -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

View 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

View File

@ -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
};
}

View File

@ -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;

View File

@ -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,
OrgUserId: String,
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 }
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;

View File

@ -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'],
},
}

View File

@ -17,13 +17,13 @@
"license": "MIT",
"dependencies": {
"cors": "^2.8.5",
"flitter-auth": "^0.18.0",
"flitter-auth": "^0.18.1",
"flitter-cli": "^0.15.2",
"flitter-di": "^0.4.1",
"flitter-flap": "^0.5.2",
"flitter-forms": "^0.8.1",
"flitter-orm": "^0.2.0",
"flitter-orm": "^0.2.1",
"flitter-upload": "^0.8.0",
"libflitter": "^0.46.2"
"libflitter": "^0.46.3"
}
}

View File

@ -976,10 +976,10 @@ find-replace@^3.0.0:
dependencies:
array-back "^3.0.1"
flitter-auth@^0.18.0:
version "0.18.0"
resolved "https://registry.yarnpkg.com/flitter-auth/-/flitter-auth-0.18.0.tgz#d27bede9ea73147c38e2d9ddb8b52abd12b7ab03"
integrity sha512-KTBB7OouBoU7hz1spjgkWIMLuc9IWRhvOiGsPy5Y3Ou/W7oKpOy3P3b0UwXMvwzuhPK4L/QDD8LljGHtzCuOZw==
flitter-auth@^0.18.1:
version "0.18.1"
resolved "https://registry.yarnpkg.com/flitter-auth/-/flitter-auth-0.18.1.tgz#e6d2e6a909bb82c3b1994b67e44bdfb8683daecb"
integrity sha512-s3Q5EN54I9EQsWa30MmTIMmzPA7+/F0Fx6bfwHZw7qyyRoaYwu3Uf7TNF2KTTdpeQD3lO2fB93OELcRmbBYxyA==
dependencies:
axios "^0.19.0"
bcrypt "^3.0.4"
@ -1041,10 +1041,10 @@ flitter-forms@^0.8.1:
recursive-readdir "^2.2.2"
validator "^10.11.0"
flitter-orm@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/flitter-orm/-/flitter-orm-0.2.0.tgz#a1ab29e5e2b5659309e4fe3bc6c93c9729d61fe5"
integrity sha512-nDhrI4bq+L5jOYsN5gNSf5inEyxtDTK8A9yXIjFWRohw+kUJOgwdD0xd5pOwU6ZshUowJTk1MtDSfCA8uvjk3A==
flitter-orm@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/flitter-orm/-/flitter-orm-0.2.1.tgz#d73e85c88995e25f62b1e65086e1cdc0f005974b"
integrity sha512-2e+G1JHm8NQXQNkLI1zH//eM54KtaoKB4mm3Q3AkuZUPbGzNiVNuNw2J/A4dJ505W9PzFBUL09aXz7lGpyVV8w==
dependencies:
flitter-di "^0.4.0"
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"
integrity sha1-kUS27ryl8dBoAWnxpncNzqYLdcM=
libflitter@^0.46.2:
version "0.46.2"
resolved "https://registry.yarnpkg.com/libflitter/-/libflitter-0.46.2.tgz#0f2d441fe6b795d10aabcf982be8bcdd08a1cb1d"
integrity sha512-aHLJ5GsHxitwBcfhr5CeFPRz7Mna5rq1PZ68UjMXmi+y2HTwMgaAqrvZTNIoZViIHK9VW5TLvdzYeFFBgwYxFQ==
libflitter@^0.46.3:
version "0.46.3"
resolved "https://registry.yarnpkg.com/libflitter/-/libflitter-0.46.3.tgz#26381648d2bf9e41559dfc28bf9508b0d3c56156"
integrity sha512-5VS0y8ooz+Dke9M+fPNyO5TUd6lS1FmMcrTugA7a8U9kjpMnnNUfn1GMAM6HycPMmY6SVUetAb20TCL/N3kuUQ==
dependencies:
colors "^1.3.3"
connect-mongodb-session "^2.2.0"
@ -1523,7 +1523,7 @@ libflitter@^0.46.2:
express-graphql "^0.9.0"
express-session "^1.15.6"
flitter-di "^0.4.0"
flitter-orm "^0.2.0"
flitter-orm "^0.2.1"
graphql "^14.5.4"
http-status "^1.4.2"
mongo-schematic-class "^1.0.3"