Last active
December 9, 2015 12:57
-
-
Save thebergamo/630386548d2d4f4de081 to your computer and use it in GitHub Desktop.
Thin Controller
This file contains 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'; | |
function TodoController (db) { | |
this.database = db; | |
this.model = db.Todo; | |
} | |
TodoController.prototype = { | |
get, | |
create | |
}; | |
module.exports = TodoController; | |
// [GET] /todo/{id} | |
function get (request, reply) { | |
let userId = request.auth.credentials.id; | |
let id = request.params.id; | |
this.model.findOneAsync({_id: id, owner: userId}) | |
.then((todo) => { | |
if (!todo) { | |
return reply.notFound(); | |
} | |
reply(todo); | |
}) | |
.catch((err) => { | |
reply.badImplementation(err.message); | |
}); | |
}; | |
// [POST] /todo | |
function create (request, reply) { | |
let userId = request.auth.credentials.id; | |
let payload = request.payload; | |
payload.owner = userId; | |
this.model.createAsync(payload) | |
.then((todo) => { | |
reply(todo).code(201); | |
}) | |
.catch((err) => { | |
reply.badImplementation(err.message); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment