Skip to content

Instantly share code, notes, and snippets.

@dydx
Created February 9, 2016 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dydx/2a123e046d12d6b9d8b1 to your computer and use it in GitHub Desktop.
Save dydx/2a123e046d12d6b9d8b1 to your computer and use it in GitHub Desktop.
'use strict';
module.exports = (model) => {
/**
* @verb: GET
* @method: index
*
* Renders a list of all users
*/
const index = (req, res, next) =>
model.find()
.then((users) => {
res.json(users);
}).catch((err) => {
next(err);
});
/**
* @verb: GET
* @method: show
*
* Renders a page for a single user
*/
const show = (req, res, next) =>
model.findById(req.params.id)
.then((user) => {
res.json(user);
}).catch((err) => {
next(err);
});
/**
* @verb: GET
* @method: new
*
* Renders a form for creation of a new user
*/
const newForm = (req, res, next) =>
// should return a form with blank fields
res.json({email: '', password: ''});
/**
* @verb: POST
* @method: create
*
* Creates and stores a new user
*/
const create = (req, res, next) =>
model.save(req.body)
.then((newUser) => {
res.json(newUser)
}).catch((err) => {
next(err);
});
/**
* @verb: GET
* @method: edit
*
* Renders a form for editing an existing user
*/
const edit = (req, res, next) =>
model.findById(req.params.id)
.then((user) => {
// should return form with fields
// already populated
res.json(user);
}).catch((err) => {
next(err);
});
/**
* @verb: PUT
* @method: update
*
* Updates an existing user
*/
const update = (req, res, next) =>
model.findByIdAndUpdate(req.params.id, req.body, {new: true})
.then((user) => {
res.json(user);
}).catch((err) => {
next(err);
});
/**
* @verb: DELETE
* @method: destroy
*
* Removes a user
*/
const destroy = (req, res, next) =>
model.findByIdAndRemove(req.params.id)
.then(() => {
res.json({destroyed: true});
}).catch((err) => {
next(err);
});
/**
* external API
*/
return {
index: index,
show: show,
newForm: newForm,
create: create,
edit: edit,
update: update,
destroy: destroy
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment