Skip to content

Commit 8f5ab17

Browse files
committed
Fix articles feed endpoint
1 parent ae45f2e commit 8f5ab17

File tree

6 files changed

+19
-13
lines changed

6 files changed

+19
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nestjs-realworld-example-app",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "[![Build Status](https://travis-ci.org/anishkny/node-express-realworld-example-app.svg?branch=master)](https://travis-ci.org/anishkny/node-express-realworld-example-app)",
55
"main": "index.js",
66
"scripts": {

src/article/article.controller.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ export class ArticleController {
2727
return await this.articleService.findAll(query);
2828
}
2929

30+
31+
@ApiOperation({ title: 'Get article feed' })
32+
@ApiResponse({ status: 200, description: 'Return article feed.'})
33+
@ApiResponse({ status: 403, description: 'Forbidden.' })
34+
@Get('feed')
35+
async getFeed(@User('id') userId: number, @Query() query): Promise<ArticlesRO> {
36+
return await this.articleService.findFeed(userId, query);
37+
}
38+
3039
@Get(':slug')
3140
async findOne(@Param('slug') slug): Promise<ArticleRO> {
3241
return await this.articleService.findOne({slug});
@@ -95,12 +104,4 @@ export class ArticleController {
95104
return await this.articleService.unFavorite(userId, slug);
96105
}
97106

98-
@ApiOperation({ title: 'Get article feed' })
99-
@ApiResponse({ status: 200, description: 'Return article feed.'})
100-
@ApiResponse({ status: 403, description: 'Forbidden.' })
101-
@Get('feed')
102-
async getFeed(@User('id') userId: number, @Query() query): Promise<ArticlesRO> {
103-
return await this.articleService.findFeed(userId, query);
104-
}
105-
106107
}

src/article/article.service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ export class ArticleService {
6565

6666
async findFeed(userId: number, query): Promise<ArticlesRO> {
6767
const _follows = await this.followsRepository.find( {followerId: userId});
68+
69+
if (!(Array.isArray(_follows) && _follows.length > 0)) {
70+
return {articles: [], articlesCount: 0};
71+
}
72+
6873
const ids = _follows.map(el => el.followingId);
6974

7075
const qb = await getRepository(ArticleEntity)

src/user/user.decorator.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ import { SECRET } from '../config';
33
import * as jwt from 'jsonwebtoken';
44

55
export const User = createParamDecorator((data, req) => {
6-
76
// if route is protected, there is a user set in auth.middleware
87
if (!!req.user) {
98
return !!data ? req.user[data] : req.user;
10-
};
9+
}
1110

1211
// in case a route is not protected, we still want to get the optional auth user from jwt
1312
const token = req.headers.authorization ? (req.headers.authorization as string).split(' ') : null;

src/user/user.entity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Entity, PrimaryGeneratedColumn, Column, BeforeInsert, JoinTable, ManyToMany, OneToMany} from "typeorm";
1+
import {Entity, PrimaryGeneratedColumn, Column, BeforeInsert, JoinTable, ManyToMany, OneToMany} from 'typeorm';
22
import { IsEmail } from 'class-validator';
33
import * as argon2 from 'argon2';
44
import { ArticleEntity } from '../article/article.entity';

src/user/user.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class UserService {
9090
if (!user) {
9191
const errors = {User: ' not found'};
9292
throw new HttpException({errors}, 401);
93-
};
93+
}
9494

9595
return this.buildUserRO(user);
9696
}
@@ -115,6 +115,7 @@ export class UserService {
115115

116116
private buildUserRO(user: UserEntity) {
117117
const userRO = {
118+
id: user.id,
118119
username: user.username,
119120
email: user.email,
120121
bio: user.bio,

0 commit comments

Comments
 (0)