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)
}
}
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 {
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
]
}
}