/ *
* 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 ) {
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' )
}
async new _project _do ( req , res , next ) {
if ( ! req . body . name ) {
return _flitter . view ( res , 'dash_v1:project' , { show _back : true , title : 'Create Project' , errors : [ 'Project name is required.' ] } )
}
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.' )
}
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 } )
}
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
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' )
}
}
module . exports = exports = v1