File

src/article/article.controller.ts

Prefix

articles

Index

Methods

Methods

Async create
create(userId: number, articleData: CreateArticleDto)
Decorators :
@ApiOperation({title: 'Create article'})
@ApiResponse({status: 201, description: 'The article has been successfully created.'})
@ApiResponse({status: 403, description: 'Forbidden.'})
@Post()
Parameters :
Name Type Optional
userId number No
articleData CreateArticleDto No
Returns : {}
Async createComment
createComment(slug, commentData: CreateCommentDto)
Decorators :
@ApiOperation({title: 'Create comment'})
@ApiResponse({status: 201, description: 'The comment has been successfully created.'})
@ApiResponse({status: 403, description: 'Forbidden.'})
@Post(':slug/comments')
Parameters :
Name Type Optional
slug No
commentData CreateCommentDto No
Returns : {}
Async delete
delete(params)
Decorators :
@ApiOperation({title: 'Delete article'})
@ApiResponse({status: 201, description: 'The article has been successfully deleted.'})
@ApiResponse({status: 403, description: 'Forbidden.'})
@Delete(':slug')
Parameters :
Name Optional
params No
Returns : {}
Async deleteComment
deleteComment(params)
Decorators :
@ApiOperation({title: 'Delete comment'})
@ApiResponse({status: 201, description: 'The article has been successfully deleted.'})
@ApiResponse({status: 403, description: 'Forbidden.'})
@Delete(':slug/comments/:id')
Parameters :
Name Optional
params No
Returns : {}
Async favorite
favorite(userId: number, slug)
Decorators :
@ApiOperation({title: 'Favorite article'})
@ApiResponse({status: 201, description: 'The article has been successfully favorited.'})
@ApiResponse({status: 403, description: 'Forbidden.'})
@Post(':slug/favorite')
Parameters :
Name Type Optional
userId number No
slug No
Returns : {}
Async findAll
findAll(query)
Decorators :
@ApiOperation({title: 'Get all articles'})
@ApiResponse({status: 200, description: 'Return all articles.'})
@Get()
Parameters :
Name Optional
query No
Async findComments
findComments(slug)
Decorators :
@Get(':slug/comments')
Parameters :
Name Optional
slug No
Async findOne
findOne(slug)
Decorators :
@Get(':slug')
Parameters :
Name Optional
slug No
Returns : Promise<ArticleRO>
Async getFeed
getFeed(userId: number, query)
Decorators :
@ApiOperation({title: 'Get article feed'})
@ApiResponse({status: 200, description: 'Return article feed.'})
@ApiResponse({status: 403, description: 'Forbidden.'})
@Get('feed')
Parameters :
Name Type Optional
userId number No
query No
Async unFavorite
unFavorite(userId: number, slug)
Decorators :
@ApiOperation({title: 'Unfavorite article'})
@ApiResponse({status: 201, description: 'The article has been successfully unfavorited.'})
@ApiResponse({status: 403, description: 'Forbidden.'})
@Delete(':slug/favorite')
Parameters :
Name Type Optional
userId number No
slug No
Returns : {}
Async update
update(params, articleData: CreateArticleDto)
Decorators :
@ApiOperation({title: 'Update article'})
@ApiResponse({status: 201, description: 'The article has been successfully updated.'})
@ApiResponse({status: 403, description: 'Forbidden.'})
@Put(':slug')
Parameters :
Name Type Optional
params No
articleData CreateArticleDto No
Returns : {}
import {Get, Post, Body, Put, Delete, Query, Param, Controller} from '@nestjs/common';
import { Request } from 'express';
import { ArticleService } from './article.service';
import { CreateArticleDto, CreateCommentDto } from './dto';
import { ArticlesRO, ArticleRO } from './article.interface';
import { CommentsRO } from './article.interface';
import { User } from '../user/user.decorator';

import {
  ApiUseTags,
  ApiBearerAuth,
  ApiResponse,
  ApiOperation,
} from '@nestjs/swagger';

@ApiBearerAuth()
@ApiUseTags('articles')
@Controller('articles')
export class ArticleController {

  constructor(private readonly articleService: ArticleService) {}

  @ApiOperation({ title: 'Get all articles' })
  @ApiResponse({ status: 200, description: 'Return all articles.'})
  @Get()
  async findAll(@Query() query): Promise<ArticlesRO> {
    return await this.articleService.findAll(query);
  }

  @Get(':slug')
  async findOne(@Param('slug') slug): Promise<ArticleRO> {
    return await this.articleService.findOne({slug});
  }

  @Get(':slug/comments')
  async findComments(@Param('slug') slug): Promise<CommentsRO> {
    return await this.articleService.findComments(slug);
  }

  @ApiOperation({ title: 'Create article' })
  @ApiResponse({ status: 201, description: 'The article has been successfully created.'})
  @ApiResponse({ status: 403, description: 'Forbidden.' })
  @Post()
  async create(@User('id') userId: number, @Body('article') articleData: CreateArticleDto) {
    return this.articleService.create(userId, articleData);
  }

  @ApiOperation({ title: 'Update article' })
  @ApiResponse({ status: 201, description: 'The article has been successfully updated.'})
  @ApiResponse({ status: 403, description: 'Forbidden.' })
  @Put(':slug')
  async update(@Param() params, @Body('article') articleData: CreateArticleDto) {
    // Todo: update slug also when title gets changed
    return this.articleService.update(params.slug, articleData);
  }

  @ApiOperation({ title: '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({ title: 'Create comment' })
  @ApiResponse({ status: 201, description: 'The comment has been successfully created.'})
  @ApiResponse({ status: 403, description: 'Forbidden.' })
  @Post(':slug/comments')
  async createComment(@Param('slug') slug, @Body('comment') commentData: CreateCommentDto) {
    return await this.articleService.addComment(slug, commentData);
  }

  @ApiOperation({ title: 'Delete comment' })
  @ApiResponse({ status: 201, description: 'The article has been successfully deleted.'})
  @ApiResponse({ status: 403, description: 'Forbidden.' })
  @Delete(':slug/comments/:id')
  async deleteComment(@Param() params) {
    const {slug, id} = params;
    return await this.articleService.deleteComment(slug, id);
  }

  @ApiOperation({ title: '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 await this.articleService.favorite(userId, slug);
  }

  @ApiOperation({ title: '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 await this.articleService.unFavorite(userId, slug);
  }

  @ApiOperation({ title: '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<ArticlesRO> {
    return await this.articleService.findFeed(userId, query);
  }

}

result-matching ""

    No results matching ""