import { Controller , Get , Post , Body , UseGuards , UseInterceptors , Param , NotFoundException , Put , Delete , BadRequestException } from '@nestjs/common' ; import { CreateArticleDto } from './dto/create-article.dto' ; import { ArticleService } from './article.service' ; import { JwtAuthGuard } from '../user/auth/jwt.auth-guard' ; import { Neo4jTypeInterceptor } from 'nest-neo4j/dist' ; import { UpdateArticleDto } from './dto/update-article.dto' ; import { CreateCommentDto } from './dto/create-comment.dto' ; import { JwtModule } from '@nestjs/jwt' ; @ UseInterceptors ( Neo4jTypeInterceptor ) @ Controller ( 'articles' ) export class ArticleController { constructor ( private readonly articleService ) { } @ UseGuards ( JwtAuthGuard . optional ( ) ) @ Get ( ) getList ( ) { return this . articleService . list ( ) ; } @ UseGuards ( JwtAuthGuard ) @ Post ( ) async postList ( @ Body ( ) createArticleDto ) { const article = await this . articleService . create ( createArticleDto . article . title , createArticleDto . article . description , createArticleDto . article . body , createArticleDto . article . tagList ) ; return { article : article . toJson ( ) } ; } @ UseGuards ( JwtAuthGuard ) @ Get ( '/feed' ) async getFeed ( ) { return this . articleService . getFeed ( ) ; } @ UseGuards ( JwtAuthGuard . optional ( ) ) @ Get ( '/:slug' ) async getIndex ( @ Param ( 'slug' ) slug ) { const article = await this . articleService . find ( slug ) ; if ( ! article ) throw new NotFoundException ( ) ; return { article : article . toJson ( ) } ; } @ UseGuards ( JwtAuthGuard ) @ Put ( '/:slug' ) async putIndex ( @ Param ( 'slug' ) slug , @ Body ( ) updateArticleDto ) { const article = await this . articleService . update ( slug , updateArticleDto . article ) ; if ( ! article ) throw new NotFoundException ( ) ; return { article : article . toJson ( ) } ; } @ UseGuards ( JwtAuthGuard ) @ Put ( '/:slug' ) async deleteIndex ( @ Param ( 'slug' ) slug ) { const article = await this . articleService . delete ( slug ) ; if ( ! article ) throw new NotFoundException ( ) ; return 'OK' ; } @ UseGuards ( JwtAuthGuard ) @ Post ( '/:slug/favorite' ) async postFavorite ( @ Param ( 'slug' ) slug ) { const article = await this . articleService . favorite ( slug ) ; if ( ! article ) throw new NotFoundException ( ) ; return { article : article . toJson ( ) } ; } @ UseGuards ( JwtAuthGuard ) @ Delete ( '/:slug/favorite' ) async deleteFavorite ( @ Param ( 'slug' ) slug ) { const article = await this . articleService . unfavorite ( slug ) ; if ( ! article ) throw new NotFoundException ( ) ; return { article : article . toJson ( ) } ; } @ UseGuards ( JwtAuthGuard ) @ Post ( '/:slug/comments' ) async postComments ( @ Param ( 'slug' ) slug , @ Body ( ) createCommentDto ) { const comment = await this . articleService . comment ( slug , createCommentDto . comment . body ) ; if ( ! comment ) throw new NotFoundException ( ) ; return { comment : comment . toJson ( ) } ; } @ Get ( '/:slug/comments' ) async getComments ( @ Param ( 'slug' ) slug ) { const comments = await this . articleService . getComments ( slug ) ; return { comments : comments . map ( comment => comment . toJson ( ) ) , } ; } @ UseGuards ( JwtAuthGuard ) @ Delete ( '/:slug/comments/:commentId' ) async deleteComments ( @ Param ( 'slug' ) slug , @ Param ( 'commentId' ) commentId ) { const outcome = await this . articleService . deleteComment ( slug , commentId ) ; if ( ! outcome ) throw new NotFoundException ( ) ; return 'OK' ; } @ UseGuards ( JwtAuthGuard ) @ Delete ( '/:slug' ) async deleteComment ( @ Param ( 'slug' ) slug ) { const comment = await this . articleService . delete ( slug ) ; if ( ! comment ) throw new NotFoundException ( ) ; return 'OK' ; } }