Skip to content
This repository has been archived by the owner on Oct 1, 2019. It is now read-only.

Commit

Permalink
refactor controller userFavouredPosts to expose empty array for non
Browse files Browse the repository at this point in the history
existing user
  • Loading branch information
RusAlex authored and voidxnull committed Oct 30, 2016
1 parent 30314af commit 3c4d458
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/api/controller.js
Expand Up @@ -384,14 +384,18 @@ export default class ApiController {

userFavouredPosts = async (ctx) => {
try {
const user_id = await this.bookshelf.knex
.select('id')
const user = await this.bookshelf.knex
.first('id')
.from('users')
.where('users.username', '=', ctx.params.user)
.map(row => row.id);
.where('users.username', '=', ctx.params.user);

const posts = await this.getFavouredPosts(user_id[0]);
ctx.body = posts;
if (user) {
const posts = await this.getFavouredPosts(user.id);
ctx.body = posts;
} else {
ctx.app.logger.warn(`Someone tried read favoured posts for '${ctx.params.user}', but there's no such user`);
ctx.body = [];
}
} catch (e) {
ctx.status = 500;
ctx.body = e.message;
Expand Down

0 comments on commit 3c4d458

Please sign in to comment.