Created
July 27, 2015 23:12
-
-
Save dylancwood/3ab0569cff6996c17724 to your computer and use it in GitHub Desktop.
scans route with todos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
//TODO: add '['api']' tags to each route (see auth/login.js for exampe) | |
//TODO: also add notes and description fields for swagger. | |
const boom = require('boom'); | |
exports.register = function(server, options, next) { | |
const path = '/scans'; | |
// TODO: should be Scan | |
const Scans = server.plugins.bookshelf.model('scans'); | |
const relations = server.plugins.hapiRelations; | |
server.route({ | |
// TODO: (maybe later) use query params to fetch specific scans | |
// may not be super complicated if you use JOI schema to validate | |
// the queryparams, then you should be able to put them directly into | |
// bookshelf.query | |
// e.g. /scans?study_id=1234&ursi=M12832932832 | |
// in leu of /studies/1234/subjects/M12832932832/scans | |
method: 'GET', | |
path: path, | |
handler: (request, reply) => { | |
// TODO: user needs to be an object w/ username as a property (you can just use credentials) | |
const user = request.auth.credentials.username; | |
// TODO: line too long | |
relations.study('What can %s read_Scan from?', user, (err, studies) => { | |
new Scans() | |
.whereIn('study_id', studies) | |
// TODO Just use request.auth.credentials (see above) | |
.readAll({username: user}) | |
.then(function (studyCollection) { | |
// TODO: test how this works when no studies matched. | |
reply(studyCollection); | |
}).catch((err) => { | |
reply(boom.wrap(err)); | |
}); | |
}); | |
} | |
}); | |
server.route({ | |
method: 'GET', | |
path: path + '/{id}', | |
handler: (request, reply) => { | |
// TODO: validate {id} using the validate config option for this route | |
// see http://hapijs.com/api#route-options; | |
const user = request.auth.credentials.username; | |
/* jscs: disable */ | |
new Scan({study_id: request.params.id}) //jshint ignore:line | |
/* jscs: enable */ | |
// TODO Just use request.auth.credentials (see above) | |
.read({username: user}) | |
.then(function (study) { | |
reply(study); | |
}).catch((err) => { | |
reply(boom.wrap(err)); | |
}); | |
} | |
}); | |
server.route({ | |
method: 'POST', | |
path: path, | |
/* E.G. | |
config: { | |
validate: { | |
payload: // JOI object | |
} | |
} | |
*/ | |
handler: (request, reply) => { | |
// TODO: use request.payload instead | |
// TODO: validate payload using joi and config.validate.payload | |
new Scan.forge(request.params).save() | |
.then(() => { | |
reply('saved') | |
}).catch((err) => { | |
reply(boom.wrap(err)); | |
}); | |
} | |
}); | |
next(); | |
}; | |
exports.register.attributes = { | |
name: 'scans' | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment