๐Ÿ“ฆ cityzenKIM / toy_project_board

๐Ÿ“„ posts.controller.ts ยท 94 lines
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
94import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  UseGuards,
  UseInterceptors,
  UploadedFile,
  ParseIntPipe,
  Put,
  Query,
} from '@nestjs/common';
import { PostsService } from './posts.service';
import { CreatePostDto } from './dto/create-post.dto';
import { UpdatePostDto } from './dto/update-post.dto';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { JwtAuthGuard } from 'src/auth/jwt/jwt-auth.guard';
import { FileInterceptor } from '@nestjs/platform-express';
import { PostCategory } from 'src/entities/Posts';
import { CurrentUser } from 'src/common/decorators/user.decorator';
import { SearchPostDto } from './dto/search-post.dto';

@ApiTags('POSTS')
@Controller('api/posts')
export class PostsController {
  constructor(private readonly postsService: PostsService) {}

  @ApiOperation({ summary: '๊ธ€ ์ž‘์„ฑ' })
  @UseGuards(JwtAuthGuard)
  @UseInterceptors(FileInterceptor('image'))
  @Post()
  async createPost(
    @CurrentUser() user,
    @Body() createPostDto: CreatePostDto,
    @UploadedFile() image?: Express.Multer.File,
  ) {
    return await this.postsService.createPost(user.id, createPostDto, image);
  }

  @ApiOperation({ summary: '์ „์ œ ๊ธ€ ์กฐํšŒ' })
  @Get()
  async getAllPosts() {
    return await this.postsService.getAllPosts();
  }

  @ApiOperation({ summary: '์ „์ œ ๊ธ€ ๊ธฐ๊ฐ„ ๋ณ„ ์ธ๊ธฐ์ˆœ ์กฐํšŒ' })
  @Get('views/:period')
  async getAllPostsByViewsAndPeriod(@Param('period') period: string) {
    return await this.postsService.getAllPostsByViewsAndPeriod(period);
  }

  @ApiOperation({ summary: '์นดํ…Œ๊ณ ๋ฆฌ ๋ณ„ ๊ธ€ ์กฐํšŒ' })
  @Get('category/:category')
  async getAllPostsByCategory(@Param('category') category: PostCategory) {
    return await this.postsService.getAllPostsByCategory(category);
  }

  @ApiOperation({ summary: '๊ธ€ ๊ฒ€์ƒ‰' })
  @Get('search')
  async searchPosts(@Query() searchPostDto: SearchPostDto) {
    return await this.postsService.searchPosts(searchPostDto);
  }

  @ApiOperation({ summary: '๊ธ€ ์ƒ์„ธ ์กฐํšŒ' })
  @Get(':id')
  getPostById(@Param('id', ParseIntPipe) id: number) {
    return this.postsService.getPostByIdForViewing(id);
  }

  @ApiOperation({ summary: '๊ธ€ ์ˆ˜์ •' })
  @UseGuards(JwtAuthGuard)
  @Put(':id')
  update(
    @CurrentUser() user,
    @Param('id', ParseIntPipe) id: number,
    @Body() updatePostDto: UpdatePostDto,
  ) {
    return this.postsService.updatePost(id, user.id, user.role, updatePostDto);
  }

  @ApiOperation({ summary: '๊ธ€ ์‚ญ์ œ' })
  @UseGuards(JwtAuthGuard)
  @Delete(':id')
  async deletePost(@CurrentUser() user, @Param('id', ParseIntPipe) id: number) {
    await this.postsService.deletePost(id, user.id, user.role);
    return {
      statusCode: 200,
      message: '๊ธ€ ์‚ญ์ œ ์„ฑ๊ณต',
    };
  }
}