Skip to content

Instantly share code, notes, and snippets.

@thebergamo
Last active December 9, 2015 12:57
Show Gist options
  • Save thebergamo/630386548d2d4f4de081 to your computer and use it in GitHub Desktop.
Save thebergamo/630386548d2d4f4de081 to your computer and use it in GitHub Desktop.
Thin Controller
'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