2020-02-09 07:58:29 +00:00
|
|
|
const Controller = require('libflitter/controller/Controller')
|
|
|
|
const Codium = require('../../../models/api/Codium.model')
|
|
|
|
const Page = require('../../../models/api/Page.model')
|
|
|
|
const Node = require('../../../models/api/Node.model')
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FormCode Controller
|
|
|
|
* -------------------------------------------------------------
|
|
|
|
* Put some description here!
|
|
|
|
*/
|
|
|
|
class FormCode 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 07:58:29 +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 code = new Codium({
|
|
|
|
NodeId: node.UUID,
|
|
|
|
PageId: page.UUID,
|
|
|
|
code: '',
|
|
|
|
})
|
|
|
|
|
|
|
|
await code.save()
|
|
|
|
return res.api(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 07:58:29 +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 code = await Codium.findOne({UUID: req.params.CodiumId})
|
|
|
|
if ( !code ) return res.status(404).message('Unable to find code with that ID.').api({})
|
|
|
|
|
|
|
|
return res.api(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
async set_values(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 07:58:29 +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 code = await Codium.findOne({UUID: req.params.CodiumId})
|
|
|
|
if ( !code ) return res.status(404).message('Unable to find code with that ID.').api({})
|
|
|
|
|
|
|
|
code.code = req.body.code
|
2020-10-13 14:24:28 +00:00
|
|
|
code.Language = req.body.Language
|
2020-02-09 07:58:29 +00:00
|
|
|
code.NodeId = node.UUID
|
|
|
|
code.PageId = page.UUID
|
|
|
|
await code.save()
|
|
|
|
return res.api(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
async drop_code(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 07:58:29 +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 code = await Codium.findOne({UUID: req.params.CodiumId})
|
|
|
|
if ( !code ) return res.status(404).message('Unable to find code with that ID.').api({})
|
|
|
|
|
2020-10-13 15:16:21 +00:00
|
|
|
code.Active = false
|
|
|
|
await code.save()
|
2020-02-09 07:58:29 +00:00
|
|
|
return res.api({})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = FormCode
|