You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.8 KiB

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({})
if ( !(await page.is_accessible_by(req.user, 'update')) ) return req.security.deny()
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: '',
})
if ( req.body.Language ) {
code.Language = req.body.Language
}
if ( req.body.code ) {
code.code = req.body.code
}
if ( req.body.UUID ) {
const existingUUID = await Codium.findOne({ UUID: req.body.UUID })
if ( !existingUUID ) {
code.UUID = req.body.UUID
}
}
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({})
if ( !(await page.is_accessible_by(req.user)) ) return req.security.deny()
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({})
if ( !(await page.is_accessible_by(req.user, 'update')) ) return req.security.deny()
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
code.Language = req.body.Language
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({})
if ( !(await page.is_accessible_by(req.user, 'update')) ) return req.security.deny()
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.Active = false
await code.save()
return res.api({})
}
}
module.exports = exports = FormCode