2020-02-09 05:09:18 +00:00
|
|
|
const Controller = require('libflitter/controller/Controller')
|
|
|
|
const Page = require('../../../models/api/Page.model')
|
|
|
|
const Node = require('../../../models/api/Node.model')
|
|
|
|
const Database = require('../../../models/api/db/Database.model')
|
|
|
|
const ColumnDef = require('../../../models/api/db/ColumnDef.model')
|
|
|
|
const DBEntry = require('../../../models/api/db/DBEntry.model')
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Database Controller
|
|
|
|
* -------------------------------------------------------------
|
|
|
|
* Put some description here!
|
|
|
|
*/
|
|
|
|
class FormDatabase extends Controller {
|
|
|
|
|
|
|
|
async create_new(req, res) {
|
|
|
|
const PageId = req.params.PageId
|
|
|
|
|
|
|
|
let page = await Page.findOne({UUID: PageId})
|
|
|
|
if ( !page ) return res.status(404).message('Page not found with that ID.').api({})
|
2020-02-14 06:11:16 +00:00
|
|
|
if ( !(await page.is_accessible_by(req.user, 'update')) ) return req.security.deny()
|
2020-02-09 05:09:18 +00:00
|
|
|
|
|
|
|
const NodeId = req.params.NodeId
|
|
|
|
|
|
|
|
let node = await Node.findOne({UUID: NodeId})
|
|
|
|
if ( !node ) return res.status(404).message('Node not found with that ID.').api({})
|
|
|
|
|
|
|
|
const db = new Database({
|
|
|
|
Name: req.body.name ? req.body.name : 'New Database',
|
|
|
|
NodeId: node.UUID,
|
|
|
|
PageId: page.UUID,
|
|
|
|
})
|
|
|
|
|
|
|
|
await db.save()
|
|
|
|
|
|
|
|
req.user.allow(`database:${db.UUID}`);
|
|
|
|
await req.user.save()
|
|
|
|
|
|
|
|
return res.api(db)
|
|
|
|
}
|
|
|
|
|
|
|
|
async get_config(req, res) {
|
|
|
|
const PageId = req.params.PageId
|
|
|
|
|
|
|
|
let page = await Page.findOne({UUID: PageId})
|
|
|
|
if ( !page ) return res.status(404).message('Page not found with that ID.').api({})
|
2020-02-14 06:11:16 +00:00
|
|
|
if ( !(await page.is_accessible_by(req.user)) ) return req.security.deny()
|
2020-02-09 05:09:18 +00:00
|
|
|
|
|
|
|
const NodeId = req.params.NodeId
|
|
|
|
|
|
|
|
let node = await Node.findOne({UUID: NodeId})
|
|
|
|
if ( !node ) return res.status(404).message('Node not found with that ID.').api({})
|
|
|
|
|
|
|
|
const DatabaseId = req.params.DatabaseId
|
|
|
|
const db = await Database.findOne({UUID: DatabaseId})
|
|
|
|
if ( !db ) return res.status(404).message('Database not found with that ID.').api({})
|
2020-02-14 06:11:16 +00:00
|
|
|
// if ( !db.accessible_by(req.user) ) return req.security.deny()
|
2020-02-09 05:09:18 +00:00
|
|
|
|
|
|
|
return res.api(db)
|
|
|
|
}
|
|
|
|
|
|
|
|
async get_columns(req, res) {
|
|
|
|
const PageId = req.params.PageId
|
|
|
|
|
|
|
|
let page = await Page.findOne({UUID: PageId})
|
|
|
|
if ( !page ) return res.status(404).message('Page not found with that ID.').api({})
|
2020-02-14 06:11:16 +00:00
|
|
|
if ( !(await page.is_accessible_by(req.user)) ) return req.security.deny()
|
2020-02-09 05:09:18 +00:00
|
|
|
|
|
|
|
const NodeId = req.params.NodeId
|
|
|
|
|
|
|
|
let node = await Node.findOne({UUID: NodeId})
|
|
|
|
if ( !node ) return res.status(404).message('Node not found with that ID.').api({})
|
|
|
|
|
|
|
|
const DatabaseId = req.params.DatabaseId
|
|
|
|
const db = await Database.findOne({UUID: DatabaseId})
|
|
|
|
if ( !db ) return res.status(404).message('Database not found with that ID.').api({})
|
2020-02-14 06:11:16 +00:00
|
|
|
// if ( !db.accessible_by(req.user) ) return req.security.deny()
|
2020-02-09 05:09:18 +00:00
|
|
|
|
2020-02-18 17:17:49 +00:00
|
|
|
const columns = (await ColumnDef.find({ DatabaseId: db.UUID })).map(x => {
|
|
|
|
x.additionalData = x.data()
|
|
|
|
return x
|
|
|
|
})
|
2020-02-09 05:09:18 +00:00
|
|
|
return res.api(columns)
|
|
|
|
}
|
|
|
|
|
|
|
|
async set_columns(req, res) {
|
|
|
|
const PageId = req.params.PageId
|
|
|
|
|
|
|
|
let page = await Page.findOne({UUID: PageId})
|
|
|
|
if ( !page ) return res.status(404).message('Page not found with that ID.').api({})
|
2020-02-14 06:11:16 +00:00
|
|
|
if ( !(await page.is_accessible_by(req.user, 'update')) ) return req.security.deny()
|
2020-02-09 05:09:18 +00:00
|
|
|
|
|
|
|
const NodeId = req.params.NodeId
|
|
|
|
|
|
|
|
let node = await Node.findOne({UUID: NodeId})
|
|
|
|
if ( !node ) return res.status(404).message('Node not found with that ID.').api({})
|
|
|
|
|
|
|
|
const DatabaseId = req.params.DatabaseId
|
|
|
|
const db = await Database.findOne({UUID: DatabaseId})
|
|
|
|
if ( !db ) return res.status(404).message('Database not found with that ID.').api({})
|
2020-02-14 06:11:16 +00:00
|
|
|
// if ( !db.accessible_by(req.user, 'update') ) return req.security.deny()
|
2020-02-09 05:09:18 +00:00
|
|
|
|
|
|
|
const existing_columns = await ColumnDef.find({ DatabaseId: db.UUID })
|
|
|
|
const assoc_columns = []
|
|
|
|
existing_columns.forEach(col => assoc_columns[col.UUID] = col)
|
|
|
|
|
|
|
|
const update_columns = []
|
|
|
|
for ( const col of req.body.columns ) {
|
|
|
|
if ( col.UUID && assoc_columns[col.UUID] ) {
|
|
|
|
assoc_columns[col.UUID].headerName = col.headerName
|
|
|
|
assoc_columns[col.UUID].field = col.field
|
|
|
|
assoc_columns[col.UUID].DatabaseId = db.UUID
|
|
|
|
assoc_columns[col.UUID].Type = col.Type
|
2020-02-18 17:17:49 +00:00
|
|
|
assoc_columns[col.UUID].additionalData = JSON.stringify(col.additionalData)
|
2020-02-09 05:09:18 +00:00
|
|
|
await assoc_columns[col.UUID].save()
|
|
|
|
update_columns.push(assoc_columns[col.UUID])
|
|
|
|
} else {
|
|
|
|
const new_col = new ColumnDef({
|
|
|
|
headerName: col.headerName,
|
|
|
|
field: col.field,
|
|
|
|
DatabaseId: db.UUID,
|
|
|
|
Type: col.Type,
|
2020-02-18 17:17:49 +00:00
|
|
|
additionalData: JSON.stringify(col.additionalData),
|
2020-02-09 05:09:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
await new_col.save()
|
|
|
|
update_columns.push(new_col)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( const col of existing_columns ) {
|
|
|
|
if ( !update_columns.includes(col) ) {
|
|
|
|
await col.delete()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.api(update_columns)
|
|
|
|
}
|
|
|
|
|
|
|
|
async get_data(req, res) {
|
|
|
|
const PageId = req.params.PageId
|
|
|
|
|
|
|
|
let page = await Page.findOne({UUID: PageId})
|
|
|
|
if ( !page ) return res.status(404).message('Page not found with that ID.').api({})
|
2020-02-14 06:11:16 +00:00
|
|
|
if ( !(await page.is_accessible_by(req.user)) ) return req.security.deny()
|
2020-02-09 05:09:18 +00:00
|
|
|
|
|
|
|
const NodeId = req.params.NodeId
|
|
|
|
|
|
|
|
let node = await Node.findOne({UUID: NodeId})
|
|
|
|
if ( !node ) return res.status(404).message('Node not found with that ID.').api({})
|
|
|
|
|
|
|
|
const DatabaseId = req.params.DatabaseId
|
|
|
|
const db = await Database.findOne({UUID: DatabaseId})
|
|
|
|
if ( !db ) return res.status(404).message('Database not found with that ID.').api({})
|
2020-02-14 06:11:16 +00:00
|
|
|
// if ( !db.accessible_by(req.user) ) return req.security.deny()
|
2020-02-09 05:09:18 +00:00
|
|
|
|
|
|
|
const entries = await DBEntry.find({DatabaseId: db.UUID})
|
|
|
|
entries.forEach(entry => entry.RowData.UUID = entry.UUID)
|
|
|
|
|
|
|
|
return res.api(entries)
|
|
|
|
}
|
|
|
|
|
|
|
|
async set_data(req, res) {
|
|
|
|
const PageId = req.params.PageId
|
|
|
|
|
|
|
|
let page = await Page.findOne({UUID: PageId})
|
|
|
|
if ( !page ) return res.status(404).message('Page not found with that ID.').api({})
|
2020-02-14 06:11:16 +00:00
|
|
|
if ( !(await page.is_accessible_by(req.user, 'update')) ) return req.security.deny()
|
2020-02-09 05:09:18 +00:00
|
|
|
|
|
|
|
const NodeId = req.params.NodeId
|
|
|
|
|
|
|
|
let node = await Node.findOne({UUID: NodeId})
|
|
|
|
if ( !node ) return res.status(404).message('Node not found with that ID.').api({})
|
|
|
|
|
|
|
|
const DatabaseId = req.params.DatabaseId
|
|
|
|
const db = await Database.findOne({UUID: DatabaseId})
|
|
|
|
if ( !db ) return res.status(404).message('Database not found with that ID.').api({})
|
2020-02-14 06:11:16 +00:00
|
|
|
// if ( !db.accessible_by(req.user) ) return req.security.deny()
|
2020-02-09 05:09:18 +00:00
|
|
|
|
|
|
|
await DBEntry.deleteMany({DatabaseId: db.UUID})
|
|
|
|
|
|
|
|
const new_recs = []
|
|
|
|
for ( const rec of req.body ) {
|
|
|
|
const data = {DatabaseId: db.UUID}
|
|
|
|
if ( rec.UUID ) data.UUID = rec.UUID
|
|
|
|
delete rec.UUID
|
|
|
|
data.RowData = rec
|
|
|
|
|
|
|
|
const dbe = new DBEntry(data)
|
|
|
|
await dbe.save()
|
|
|
|
dbe.RowData.UUID = dbe.UUID
|
|
|
|
new_recs.push(dbe)
|
|
|
|
}
|
|
|
|
return res.api(new_recs)
|
|
|
|
}
|
|
|
|
|
|
|
|
async drop_database(req, res) {
|
|
|
|
const PageId = req.params.PageId
|
|
|
|
|
|
|
|
let page = await Page.findOne({UUID: PageId})
|
|
|
|
if ( !page ) return res.status(404).message('Page not found with that ID.').api({})
|
2020-02-14 06:11:16 +00:00
|
|
|
if ( !(await page.is_accessible_by(req.user, 'update')) ) return req.security.deny()
|
2020-02-09 05:09:18 +00:00
|
|
|
|
|
|
|
const NodeId = req.params.NodeId
|
|
|
|
|
|
|
|
let node = await Node.findOne({UUID: NodeId})
|
|
|
|
if ( !node ) return res.status(404).message('Node not found with that ID.').api({})
|
|
|
|
|
|
|
|
const DatabaseId = req.params.DatabaseId
|
|
|
|
const db = await Database.findOne({UUID: DatabaseId})
|
|
|
|
if ( !db ) return res.status(404).message('Database not found with that ID.').api({})
|
2020-02-14 06:11:16 +00:00
|
|
|
// if ( !db.accessible_by(req.user) ) return req.security.deny()
|
2020-02-09 05:09:18 +00:00
|
|
|
|
|
|
|
await DBEntry.deleteMany({DatabaseId: db.UUID})
|
|
|
|
await db.delete()
|
2020-02-09 07:58:29 +00:00
|
|
|
return res.api({})
|
2020-02-09 05:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = FormDatabase
|