Add GET /users route

This commit is contained in:
Adrian Obelmejias
2016-03-05 15:13:41 -05:00
parent 1030f80360
commit 9f8a04e556
2 changed files with 84 additions and 39 deletions
+5
View File
@@ -21,3 +21,8 @@ export async function createUser(ctx) {
ctx.throw(422, err) ctx.throw(422, err)
} }
} }
export async function getUsers (ctx) {
const users = await User.find({}, '-password -salt')
ctx.body = users
}
+79 -39
View File
@@ -1,49 +1,89 @@
import { createUser } from './controller' import { ensureUser } from '../../middleware/validators'
import {
createUser,
getUsers
} from './controller'
export default { export default {
base: '/users', base: '/users',
/** /**
* @api {post} /users Create a new user * @api {post} /users Create a new user
* @apiPermission * @apiPermission
* @apiVersion 1.0.0 * @apiVersion 1.0.0
* @apiName CreateUser * @apiName CreateUser
* @apiGroup Users * @apiGroup Users
* *
* @apiExample Example usage: * @apiExample Example usage:
* curl -H "Content-Type: application/json" -X POST -d '{ "user": { "username": "johndoe", "password": "secretpasas" } }' localhost:5000/users * curl -H "Content-Type: application/json" -X POST -d '{ "user": { "username": "johndoe", "password": "secretpasas" } }' localhost:5000/users
* *
* @apiParam {Object} user User object (required) * @apiParam {Object} user User object (required)
* @apiParam {String} user.username Username. * @apiParam {String} user.username Username.
* @apiParam {String} user.password Password. * @apiParam {String} user.password Password.
* *
* @apiSuccess {Object} users User object * @apiSuccess {Object} users User object
* @apiSuccess {ObjectId} users._id User id * @apiSuccess {ObjectId} users._id User id
* @apiSuccess {String} users.name User name * @apiSuccess {String} users.name User name
* @apiSuccess {String} users.username User username * @apiSuccess {String} users.username User username
* *
* @apiSuccessExample {json} Success-Response: * @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK * HTTP/1.1 200 OK
* { * {
* "user": { * "user": {
* "_id": "56bd1da600a526986cf65c80" * "_id": "56bd1da600a526986cf65c80"
* "name": "John Doe" * "name": "John Doe"
* "username": "johndoe" * "username": "johndoe"
* } * }
* } * }
* *
* @apiError UnprocessableEntity Missing required parameters * @apiError UnprocessableEntity Missing required parameters
* *
* @apiErrorExample {json} Error-Response: * @apiErrorExample {json} Error-Response:
* HTTP/1.1 422 Unprocessable Entity * HTTP/1.1 422 Unprocessable Entity
* { * {
* "status": 422, * "status": 422,
* "error": "Unprocessable Entity" * "error": "Unprocessable Entity"
* } * }
*/ */
'Create new users': { 'Create new users': {
method: 'POST', method: 'POST',
route: '/', route: '/',
controller: createUser 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
]
} }
} }