-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor /users, add search capabilities
- Loading branch information
1 parent
66379b9
commit f3bdcf9
Showing
6 changed files
with
77 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,68 @@ | ||
import Router from 'koa-66'; | ||
import Qs from 'qs'; | ||
import _ from "lodash"; | ||
import Qs from "qs"; | ||
import Sequelize from "sequelize"; | ||
|
||
let log = require('logfilename')(__filename); | ||
export default function UserRouter(app) { | ||
const { models } = app.data.sequelize; | ||
|
||
export function UserHttpController(app, userApi){ | ||
log.debug("UserHttpController"); | ||
const api = { | ||
pathname: "/users", | ||
middlewares: [ | ||
app.server.auth.isAuthenticated, | ||
app.server.auth.isAuthorized | ||
], | ||
ops: { | ||
getAll: { | ||
pathname: "/", | ||
method: "get", | ||
handler: async context => { | ||
const filter = Qs.parse(context.request.querystring); | ||
_.defaults(filter, { | ||
limit: 100, | ||
order: "DESC", | ||
offset: 0 | ||
}); | ||
const result = await models.User.findAndCountAll({ | ||
limit: filter.limit, | ||
order: [["createdAt", filter.order]], | ||
offset: filter.offset, | ||
where: filter.search && { | ||
$or: [ | ||
{ username: { $like: `%${filter.search}%` } }, | ||
{ email: { $like: `%${filter.search}%` } } | ||
] | ||
} | ||
}); | ||
context.body = { | ||
count: result.count, | ||
data: result.rows.map(user => user.toJSON()) | ||
}; | ||
context.status = 200; | ||
} | ||
}, | ||
getOne: { | ||
pathname: "/:id", | ||
method: "get", | ||
handler: async context => { | ||
const user = await models.User.findByUserId(context.state.user.id); | ||
|
||
let respond = app.utils.http.respond; | ||
return { | ||
async getAll(context) { | ||
return respond( | ||
context, | ||
userApi, | ||
userApi.getAll, | ||
[Qs.parse(context.request.querystring)]); | ||
}, | ||
async getOne(context) { | ||
let userId = context.params.id; | ||
return respond(context, userApi, userApi.getOne, [userId]); | ||
if (!user) { | ||
context.status = 404; | ||
context.body = { | ||
error: { | ||
code: 404, | ||
name: "NotFound" | ||
} | ||
}; | ||
} else { | ||
context.body = user.get(); | ||
context.status = 200; | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
export default function UserRouter(app, userApi){ | ||
let router = new Router(); | ||
let userHttpController = UserHttpController(app, userApi); | ||
|
||
router.use(app.server.auth.isAuthenticated); | ||
router.use(app.server.auth.isAuthorized); | ||
|
||
router.get('/', userHttpController.getAll); | ||
router.get('/:id', userHttpController.getOne); | ||
|
||
app.server.baseRouter().mount("/users", router); | ||
return router; | ||
app.server.createRouter(api); | ||
return {}; | ||
} |