From 60033b6527312de7a5c1b5b44eecfc3c174e218d Mon Sep 17 00:00:00 2001 From: J-Hoplin Date: Thu, 4 Jul 2024 01:40:59 +0900 Subject: [PATCH] Feat/Fix: Rename updatePost to updatePostFolder and create updatePost API --- src/modules/posts/dto/index.ts | 3 +- src/modules/posts/dto/updatePost.dto.ts | 15 ++++++++++ ...te-post.dto.ts => updatePostFolder.dto.ts} | 2 +- src/modules/posts/posts.controller.ts | 13 ++++++-- src/modules/posts/posts.repository.ts | 30 ++++++++++++++++++- src/modules/posts/posts.service.ts | 18 +++++++++-- src/modules/posts/type/type.d.ts | 2 ++ 7 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 src/modules/posts/dto/updatePost.dto.ts rename src/modules/posts/dto/{update-post.dto.ts => updatePostFolder.dto.ts} (83%) diff --git a/src/modules/posts/dto/index.ts b/src/modules/posts/dto/index.ts index ef06569..f85f9a3 100644 --- a/src/modules/posts/dto/index.ts +++ b/src/modules/posts/dto/index.ts @@ -1,3 +1,4 @@ export * from './create-post.dto'; -export * from './update-post.dto'; +export * from './updatePostFolder.dto'; export * from './list-post.dto'; +export * from './updatePost.dto'; diff --git a/src/modules/posts/dto/updatePost.dto.ts b/src/modules/posts/dto/updatePost.dto.ts new file mode 100644 index 0000000..dfc499c --- /dev/null +++ b/src/modules/posts/dto/updatePost.dto.ts @@ -0,0 +1,15 @@ +import { ApiProperty } from '@nestjs/swagger'; +import { PostUpdateableFields } from '../type/type'; +import { IsBoolean, IsOptional } from 'class-validator'; + +export class UpdatePostDto implements PostUpdateableFields { + // Temporary ignore in MVP level + title: string; + @ApiProperty({ + required: false, + default: false, + }) + @IsOptional() + @IsBoolean() + isFavorite: boolean; +} diff --git a/src/modules/posts/dto/update-post.dto.ts b/src/modules/posts/dto/updatePostFolder.dto.ts similarity index 83% rename from src/modules/posts/dto/update-post.dto.ts rename to src/modules/posts/dto/updatePostFolder.dto.ts index 50eb172..400208f 100644 --- a/src/modules/posts/dto/update-post.dto.ts +++ b/src/modules/posts/dto/updatePostFolder.dto.ts @@ -1,7 +1,7 @@ import { ApiProperty } from '@nestjs/swagger'; import { IsMongoId, IsNotEmpty } from 'class-validator'; -export class UpdatePostDto { +export class UpdatePostFolderDto { @IsNotEmpty() @IsMongoId() @ApiProperty() diff --git a/src/modules/posts/posts.controller.ts b/src/modules/posts/posts.controller.ts index 3fc9d41..b8da122 100644 --- a/src/modules/posts/posts.controller.ts +++ b/src/modules/posts/posts.controller.ts @@ -13,7 +13,7 @@ import { PostsService } from '@src/modules/posts/posts.service'; import { CreatePostDto } from '@src/modules/posts/dto/create-post.dto'; import { GetUser, PaginationMetadata } from '@src/common'; import { JwtGuard } from '@src/modules/users/guards'; -import { ListPostQueryDto, UpdatePostDto } from './dto'; +import { ListPostQueryDto, UpdatePostDto, UpdatePostFolderDto } from './dto'; import { CreatePostDocs, DeletePostDocs, @@ -47,12 +47,21 @@ export class PostsController { return await this.postsService.createPost(createPostDto, userId); } + @Patch(':postId') + async updateFolder( + @GetUser() userId: string, + @Param('postId') postId: string, + @Body() dto: UpdatePostDto, + ) { + return await this.postsService.updatePost(userId, postId, dto); + } + @Patch(':postId/move') @UpdatePostFolderDocs async updatePostFolder( @GetUser() userId: string, @Param('postId') postId: string, - @Body() dto: UpdatePostDto, + @Body() dto: UpdatePostFolderDto, ) { return await this.postsService.updatePostFolder(userId, postId, dto); } diff --git a/src/modules/posts/posts.repository.ts b/src/modules/posts/posts.repository.ts index 40d0af3..ce95c7c 100644 --- a/src/modules/posts/posts.repository.ts +++ b/src/modules/posts/posts.repository.ts @@ -7,6 +7,7 @@ import { InjectModel } from '@nestjs/mongoose'; import { FilterQuery, Model } from 'mongoose'; import { Post } from '@src/infrastructure'; import { OrderType } from '@src/common'; +import { PostUpdateableFields } from './type/type'; @Injectable() export class PostsRepository { @@ -93,7 +94,34 @@ export class PostsRepository { } } - async deletePost(userId: string, postId: string) { + async updatePost( + userId: string, + postId: string, + updateFields: PostUpdateableFields, + ) { + const updateResult = await this.postModel + .updateOne( + { + _id: postId, + userId: userId, + }, + { + $set: { + ...updateFields, + }, + }, + ) + .exec(); + + if (!updateResult.modifiedCount) { + throw new NotFoundException('Post를 찾을 수 없습니다.'); + } + return updateResult; + } + + // 해당 이슈로 인해 임시로 any타입 명시 + // https://github.com/microsoft/TypeScript/issues/42873 + async deletePost(userId: string, postId: string): Promise { const deleteResult = await this.postModel .deleteOne({ _id: postId, diff --git a/src/modules/posts/posts.service.ts b/src/modules/posts/posts.service.ts index 52d248c..dd94bf6 100644 --- a/src/modules/posts/posts.service.ts +++ b/src/modules/posts/posts.service.ts @@ -1,7 +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 { ListPostQueryDto, UpdatePostDto } from './dto'; +import { ListPostQueryDto, UpdatePostDto, UpdatePostFolderDto } from './dto'; @Injectable() export class PostsService { @@ -39,7 +39,21 @@ export class PostsService { ); } - async updatePostFolder(userId: string, postId: string, dto: UpdatePostDto) { + async updatePost(userId: string, postId: string, dto: UpdatePostDto) { + // Find if user post exist + await this.postRepository.findPostOrThrow(userId, postId); + + // Update post data + await this.postRepository.updatePost(userId, postId, dto); + + return true; + } + + async updatePostFolder( + userId: string, + postId: string, + dto: UpdatePostFolderDto, + ) { // Find if post exist await this.postRepository.findPostOrThrow(userId, postId); diff --git a/src/modules/posts/type/type.d.ts b/src/modules/posts/type/type.d.ts index d20521b..7af2cbb 100644 --- a/src/modules/posts/type/type.d.ts +++ b/src/modules/posts/type/type.d.ts @@ -1,2 +1,4 @@ +import { Post } from '@src/infrastructure'; + export interface PostUpdateableFields extends Pick {}