backend/app/controllers/api/v1/FormCode.controller.js
garrettmills 55a22a4f4c
All checks were successful
continuous-integration/drone/push Build is passing
Start /api/v1/offline/sync endpoint
2020-10-28 22:55:04 -05:00

113 lines
3.8 KiB
JavaScript

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