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: '', }) 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({}) await code.delete() return res.api({}) } } module.exports = exports = FormCode