Skip to content

Commit

Permalink
Add options in GET /api/users
Browse files Browse the repository at this point in the history
  • Loading branch information
kunalkapadia committed Jan 28, 2016
1 parent 83e86d8 commit c8f1834
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
7 changes: 4 additions & 3 deletions server/controllers/user.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { User } from '../models';

export function get(req, res, next, id) {
export function load(req, res, next, id) {
User.get(id).then((user) => {
req.user = user; // eslint-disable-line no-param-reassign
return next();
}).error((e) => next(e));
}

export function getById(req, res) {
export function get(req, res) {
return res.json(req.user);
}

Expand All @@ -33,7 +33,8 @@ export function update(req, res, next) {
}

export function list(req, res, next) {
User.list().then((users) => res.json(users))
const { limit = 50, skip = 0 } = req.query;
User.list({ limit, skip }).then((users) => res.json(users))
.error((e) => next(e));
}

Expand Down
9 changes: 8 additions & 1 deletion server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const UserSchema = new mongoose.Schema({
type: String,
required: true,
match: [/^[1-9][0-9]{9}$/, 'The value of path {PATH} ({VALUE}) is not a valid mobile number.']
},
createdAt: {
type: Date,
default: Date.now
}
});

Expand All @@ -27,8 +31,11 @@ UserSchema.statics = {
});
},

list() {
list({ skip = 0, limit = 50 } = {}) {
return this.find()
.sort({ createdAt: -1 })
.skip(skip)
.limit(limit)
.execAsync();
}
};
Expand Down
4 changes: 2 additions & 2 deletions server/routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ router.route('/')
.post(userCtrl.create);

router.route('/:userId')
.get(userCtrl.getById)
.get(userCtrl.get)
.put(userCtrl.update)
.delete(userCtrl.remove);

router.param('userId', userCtrl.get);
router.param('userId', userCtrl.load);

export default router;

0 comments on commit c8f1834

Please sign in to comment.