diff --git a/src/modules/posts/docs/index.ts b/src/modules/posts/docs/index.ts new file mode 100644 index 0000000..9550b2d --- /dev/null +++ b/src/modules/posts/docs/index.ts @@ -0,0 +1,2 @@ +export * from './createPost.docs'; +export * from './updatePost.docs'; diff --git a/src/modules/posts/docs/updatePost.docs.ts b/src/modules/posts/docs/updatePost.docs.ts new file mode 100644 index 0000000..891d5dd --- /dev/null +++ b/src/modules/posts/docs/updatePost.docs.ts @@ -0,0 +1,9 @@ +import { applyDecorators } from '@nestjs/common'; +import { ApiBearerAuth, ApiOperation } from '@nestjs/swagger'; + +export const UpdatePostDocs = applyDecorators( + ApiOperation({ + summary: 'URL 폴더 변경', + }), + ApiBearerAuth(), +); diff --git a/src/modules/posts/dto/create-post.dto.ts b/src/modules/posts/dto/create-post.dto.ts index ed2a363..dcd8251 100644 --- a/src/modules/posts/dto/create-post.dto.ts +++ b/src/modules/posts/dto/create-post.dto.ts @@ -1,14 +1,13 @@ -import { IsNotEmpty, IsString } from 'class-validator'; +import { IsMongoId, IsNotEmpty, IsUrl } from 'class-validator'; import { ApiProperty } from '@nestjs/swagger'; -import { Types } from 'mongoose'; export class CreatePostDto { - @IsString() + @IsMongoId() @IsNotEmpty() @ApiProperty({ description: '폴더 id', required: true }) folderId: string; - @IsString() + @IsUrl() @IsNotEmpty() @ApiProperty({ description: '저장할 url', required: true }) url: string; diff --git a/src/modules/posts/dto/index.ts b/src/modules/posts/dto/index.ts new file mode 100644 index 0000000..4c8c5b7 --- /dev/null +++ b/src/modules/posts/dto/index.ts @@ -0,0 +1,2 @@ +export * from './create-post.dto'; +export * from './update-post.dto'; diff --git a/src/modules/posts/dto/update-post.dto.ts b/src/modules/posts/dto/update-post.dto.ts new file mode 100644 index 0000000..50eb172 --- /dev/null +++ b/src/modules/posts/dto/update-post.dto.ts @@ -0,0 +1,9 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { IsMongoId, IsNotEmpty } from 'class-validator'; + +export class UpdatePostDto { + @IsNotEmpty() + @IsMongoId() + @ApiProperty() + folderId: string; +} diff --git a/src/modules/posts/posts.controller.ts b/src/modules/posts/posts.controller.ts index 9d0718f..4efb484 100644 --- a/src/modules/posts/posts.controller.ts +++ b/src/modules/posts/posts.controller.ts @@ -1,11 +1,19 @@ -import { Controller, Post, Body, UseGuards } from '@nestjs/common'; +import { + Controller, + Post, + Body, + UseGuards, + Patch, + Param, +} from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; -import { Types } from 'mongoose'; import { PostsService } from '@src/modules/posts/posts.service'; import { CreatePostDto } from '@src/modules/posts/dto/create-post.dto'; import { GetUser } from '@src/common'; import { JwtGuard } from '@src/modules/users/guards'; -import { CreatePostDocs } from '@src/modules/posts/docs/createPost.docs'; +import { UpdatePostDto } from './dto'; +import { UpdatePostDocs } from './docs/updatePost.docs'; +import { CreatePostDocs } from './docs'; @ApiTags('posts') @Controller('posts') @@ -21,4 +29,14 @@ export class PostsController { ): Promise { return await this.postsService.createPost(createPostDto, userId); } + + @Patch(':postId') + @UpdatePostDocs + async updatePost( + @GetUser() userId: string, + @Param('postId') postId: string, + @Body() dto: UpdatePostDto, + ) { + return await this.postsService.updatePost(userId, postId, dto); + } } diff --git a/src/modules/posts/posts.repository.ts b/src/modules/posts/posts.repository.ts index 1a8ee8f..81dccf9 100644 --- a/src/modules/posts/posts.repository.ts +++ b/src/modules/posts/posts.repository.ts @@ -1,4 +1,8 @@ -import { Injectable, InternalServerErrorException } from '@nestjs/common'; +import { + Injectable, + InternalServerErrorException, + NotFoundException, +} from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model, Types } from 'mongoose'; import { Post } from '@src/infrastructure'; @@ -9,6 +13,34 @@ export class PostsRepository { @InjectModel(Post.name) private readonly postModel: Model, ) {} + async findPostOrThrow(userId: string, postId: string) { + const post = await this.postModel + .findById({ + _id: postId, + userId: userId, + }) + .lean(); + if (!post) { + throw new NotFoundException('Post를 찾을 수 없습니다.'); + } + return post; + } + + async updatePost(userId: string, postId: string, folderId: string) { + const updatedPost = await this.postModel + .updateOne( + { + _id: postId, + userId: userId, + }, + { + folderId: folderId, + }, + ) + .lean(); + return updatedPost; + } + async createPost( userId: string, folderId: string, diff --git a/src/modules/posts/posts.service.ts b/src/modules/posts/posts.service.ts index 0e9426a..2a000e8 100644 --- a/src/modules/posts/posts.service.ts +++ b/src/modules/posts/posts.service.ts @@ -1,6 +1,7 @@ import { Injectable } from '@nestjs/common'; import { CreatePostDto } from '@src/modules/posts/dto/create-post.dto'; import { PostsRepository } from '@src/modules/posts/posts.repository'; +import { UpdatePostDto } from './dto'; @Injectable() export class PostsService { @@ -19,4 +20,15 @@ export class PostsService { title, ); } + + async updatePost(userId: string, postId: string, dto: UpdatePostDto) { + // Find if post exist + await this.postRepository.findPostOrThrow(userId, postId); + + // Update post folder id + await this.postRepository.updatePost(userId, postId, dto.folderId); + + //return response; + return true; + } }