-
-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy patharticle.controller.ts
More file actions
101 lines (87 loc) · 4.15 KB
/
Copy patharticle.controller.ts
File metadata and controls
101 lines (87 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { Body, Controller, Delete, Get, Param, Post, Put, Query } from '@nestjs/common';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { User } from '../user/user.decorator';
import { IArticleRO, IArticlesRO, ICommentsRO } from './article.interface';
import { ArticleService } from './article.service';
import { CreateArticleDto, CreateCommentDto } from './dto';
@ApiBearerAuth()
@ApiTags('articles')
@Controller('articles')
export class ArticleController {
constructor(private readonly articleService: ArticleService) {}
@ApiOperation({ summary: 'Get all articles' })
@ApiResponse({ status: 200, description: 'Return all articles.' })
@Get()
async findAll(@User('id') user: number, @Query() query): Promise<IArticlesRO> {
return this.articleService.findAll(+user, query);
}
@ApiOperation({ summary: 'Get article feed' })
@ApiResponse({ status: 200, description: 'Return article feed.' })
@ApiResponse({ status: 403, description: 'Forbidden.' })
@Get('feed')
async getFeed(@User('id') userId: number, @Query() query): Promise<IArticlesRO> {
return this.articleService.findFeed(+userId, query);
}
@Get(':slug')
async findOne(@User('id') userId: number, @Param('slug') slug): Promise<IArticleRO> {
return this.articleService.findOne(userId, { slug });
}
@Get(':slug/comments')
async findComments(@Param('slug') slug): Promise<ICommentsRO> {
return this.articleService.findComments(slug);
}
@ApiOperation({ summary: 'Create article' })
@ApiBody({ type: CreateArticleDto })
@ApiResponse({ status: 201, description: 'The article has been successfully created.' })
@ApiResponse({ status: 403, description: 'Forbidden.' })
@Post()
async create(@User('id') userId: number, @Body() articleData: CreateArticleDto) {
return this.articleService.create(userId, articleData);
}
@ApiOperation({ summary: 'Update article' })
@ApiBody({ type: CreateArticleDto })
@ApiResponse({ status: 201, description: 'The article has been successfully updated.' })
@ApiResponse({ status: 403, description: 'Forbidden.' })
@Put(':slug')
async update(@User('id') user: number, @Param() params, @Body() articleData: CreateArticleDto) {
// Todo: update slug also when title gets changed
return this.articleService.update(+user, params.slug, articleData);
}
@ApiOperation({ summary: 'Delete article' })
@ApiResponse({ status: 201, description: 'The article has been successfully deleted.' })
@ApiResponse({ status: 403, description: 'Forbidden.' })
@Delete(':slug')
async delete(@Param() params) {
return this.articleService.delete(params.slug);
}
@ApiOperation({ summary: 'Create comment' })
@ApiBody({ type: CreateCommentDto })
@ApiResponse({ status: 201, description: 'The comment has been successfully created.' })
@ApiResponse({ status: 403, description: 'Forbidden.' })
@Post(':slug/comments')
async createComment(@User('id') user: number, @Param('slug') slug, @Body() commentData: CreateCommentDto) {
return this.articleService.addComment(user, slug, commentData);
}
@ApiOperation({ summary: 'Delete comment' })
@ApiResponse({ status: 201, description: 'The article has been successfully deleted.' })
@ApiResponse({ status: 403, description: 'Forbidden.' })
@Delete(':slug/comments/:id')
async deleteComment(@User('id') user: number, @Param() params) {
const { slug, id } = params;
return this.articleService.deleteComment(+user, slug, +id);
}
@ApiOperation({ summary: 'Favorite article' })
@ApiResponse({ status: 201, description: 'The article has been successfully favorited.' })
@ApiResponse({ status: 403, description: 'Forbidden.' })
@Post(':slug/favorite')
async favorite(@User('id') userId: number, @Param('slug') slug) {
return this.articleService.favorite(userId, slug);
}
@ApiOperation({ summary: 'Unfavorite article' })
@ApiResponse({ status: 201, description: 'The article has been successfully unfavorited.' })
@ApiResponse({ status: 403, description: 'Forbidden.' })
@Delete(':slug/favorite')
async unFavorite(@User('id') userId: number, @Param('slug') slug) {
return this.articleService.unFavorite(userId, slug);
}
}