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.
devbug/app/controllers/dash/v1.controller.js

118 lines
3.7 KiB

5 years ago
/*
* v1 Controller
* -------------------------------------------------------------
* Put some description here!
*/
const Project = _flitter.model('v1:Project')
const Out = _flitter.model('v1:Out')
class v1 {
/*
* Serve the main page.
*/
async main(req, res){
const projects = await Project.find({ archived: false, user_id: req.session.auth.uuid })
/*
* Return the main view.
* It must be passed the response.
* View parameters can be passed as an optional third
* argument to the view() method.
*/
return _flitter.view(res, 'dash_v1:main', { projects })
}
new_project_show(req, res, next){
5 years ago
return _flitter.view(res, 'dash_v1:project', { show_back: true, title: 'Create New Project' })
}
async project_edit_show(req, res, next){
const project = await Project.findById(req.params.id)
if ( !project ){
return _flitter.error(res, 404, 'Project not found with the specified ID.')
}
return _flitter.view(res, 'dash_v1:project', { show_back: true, title: 'Update Project', project_name: project.name})
}
async project_edit_do(req, res, next){
const project = await Project.findById(req.params.id)
if ( !project ){
return _flitter.error(res, 404, 'Project not found with the specified ID.')
}
if ( !req.body || !req.body.name ){
return _flitter.view(res, 'dash_v1:project', {show_back: true, title: 'Update Project', project_name: project.name, errors: ['Project name is required.']})
}
project.name = req.body.name
await project.save()
return res.redirect('/dash/v1')
5 years ago
}
async new_project_do(req, res, next){
if ( !req.body.name ){
5 years ago
return _flitter.view(res, 'dash_v1:project', {show_back: true, title: 'Create Project', errors: ['Project name is required.']})
5 years ago
}
const project = new Project({
name: req.body.name,
user_id: req.session.auth.uuid,
data: JSON.stringify({
created: Date.now(),
modified: Date.now()
}),
shared_user_ids: [],
})
await project.save()
return res.redirect('/dash/v1')
}
async project_view(req, res, next){
const project = await Project.findById(req.params.id)
if ( !project ){
_flitter.error(res, 404, 'Project not found.')
}
5 years ago
const outs = await Out.find({ project_id: project.id }).sort('-created')
return _flitter.view(res, 'dash_v1:view', { project, outs, show_back: true, title: 'View: '+project.name })
5 years ago
}
async out_view(req, res, next){
const out = await Out.findById(req.params.id)
console.log(out.data)
const pretty = JSON.stringify(JSON.parse(out.data), null, 4)
// TODO permission access check
5 years ago
return _flitter.view(res, 'dash_v1:out', {out, prettyd:pretty, show_back: true, title: out.brief, title_small: true });
}
project_delete_show(req, res, next){
return _flitter.view(res, 'dash_v1:confirm', {show_back: true, title: 'Are you sure?', text: 'Deleting this project will remove all stored breakpoint data. This action cannot be undone.', destination: '/dash/v1/project/delete/'+req.params.id})
}
async project_delete_do(req, res, next){
const project = await Project.findById(req.params.id)
if ( project ){
const outs = await Out.find({project_id: project.id})
for ( const key in outs ){
await outs[key].delete()
}
await project.delete()
}
return res.redirect('/dash/v1')
5 years ago
}
}
module.exports = exports = v1