From 9f8a04e556459c416efe7c21433cffd81668e1bf Mon Sep 17 00:00:00 2001 From: Adrian Obelmejias Date: Sat, 5 Mar 2016 15:13:41 -0500 Subject: [PATCH] Add `GET /users` route --- src/modules/users/controller.js | 5 ++ src/modules/users/router.js | 118 +++++++++++++++++++++----------- 2 files changed, 84 insertions(+), 39 deletions(-) diff --git a/src/modules/users/controller.js b/src/modules/users/controller.js index 1de195f..97e5c96 100644 --- a/src/modules/users/controller.js +++ b/src/modules/users/controller.js @@ -21,3 +21,8 @@ export async function createUser(ctx) { ctx.throw(422, err) } } + +export async function getUsers (ctx) { + const users = await User.find({}, '-password -salt') + ctx.body = users +} diff --git a/src/modules/users/router.js b/src/modules/users/router.js index 909c0e8..e47a889 100644 --- a/src/modules/users/router.js +++ b/src/modules/users/router.js @@ -1,49 +1,89 @@ -import { createUser } from './controller' +import { ensureUser } from '../../middleware/validators' +import { + createUser, + getUsers +} from './controller' export default { base: '/users', -/** - * @api {post} /users Create a new user - * @apiPermission - * @apiVersion 1.0.0 - * @apiName CreateUser - * @apiGroup Users - * - * @apiExample Example usage: - * curl -H "Content-Type: application/json" -X POST -d '{ "user": { "username": "johndoe", "password": "secretpasas" } }' localhost:5000/users - * - * @apiParam {Object} user User object (required) - * @apiParam {String} user.username Username. - * @apiParam {String} user.password Password. - * - * @apiSuccess {Object} users User object - * @apiSuccess {ObjectId} users._id User id - * @apiSuccess {String} users.name User name - * @apiSuccess {String} users.username User username - * - * @apiSuccessExample {json} Success-Response: - * HTTP/1.1 200 OK - * { - * "user": { - * "_id": "56bd1da600a526986cf65c80" - * "name": "John Doe" - * "username": "johndoe" - * } - * } - * - * @apiError UnprocessableEntity Missing required parameters - * - * @apiErrorExample {json} Error-Response: - * HTTP/1.1 422 Unprocessable Entity - * { - * "status": 422, - * "error": "Unprocessable Entity" - * } - */ + /** + * @api {post} /users Create a new user + * @apiPermission + * @apiVersion 1.0.0 + * @apiName CreateUser + * @apiGroup Users + * + * @apiExample Example usage: + * curl -H "Content-Type: application/json" -X POST -d '{ "user": { "username": "johndoe", "password": "secretpasas" } }' localhost:5000/users + * + * @apiParam {Object} user User object (required) + * @apiParam {String} user.username Username. + * @apiParam {String} user.password Password. + * + * @apiSuccess {Object} users User object + * @apiSuccess {ObjectId} users._id User id + * @apiSuccess {String} users.name User name + * @apiSuccess {String} users.username User username + * + * @apiSuccessExample {json} Success-Response: + * HTTP/1.1 200 OK + * { + * "user": { + * "_id": "56bd1da600a526986cf65c80" + * "name": "John Doe" + * "username": "johndoe" + * } + * } + * + * @apiError UnprocessableEntity Missing required parameters + * + * @apiErrorExample {json} Error-Response: + * HTTP/1.1 422 Unprocessable Entity + * { + * "status": 422, + * "error": "Unprocessable Entity" + * } + */ 'Create new users': { method: 'POST', route: '/', controller: createUser + }, + + /** + * @api {get} /users Get all users + * @apiPermission user + * @apiVersion 1.0.0 + * @apiName GetUsers + * @apiGroup Users + * + * @apiExample Example usage: + * curl -H "Content-Type: application/json" -X GET localhost:5000/users + * + * @apiSuccess {Object[]} users Array of user objects + * @apiSuccess {ObjectId} users._id User id + * @apiSuccess {String} users.name User name + * @apiSuccess {String} users.username User username + * + * @apiSuccessExample {json} Success-Response: + * HTTP/1.1 200 OK + * { + * "users": [{ + * "_id": "56bd1da600a526986cf65c80" + * "name": "John Doe" + * "username": "johndoe" + * }] + * } + * + * @apiUse TokenError + */ + 'Get all users': { + method: 'GET', + route: '/', + controller: getUsers, + middleware: [ + ensureUser + ] } }