Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Post API 생성, 기존 Update Post API Update Post Folder로 명명 변경 #38

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/modules/posts/dto/index.ts
Original file line number Diff line number Diff line change
@@ -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';
15 changes: 15 additions & 0 deletions src/modules/posts/dto/updatePost.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsMongoId, IsNotEmpty } from 'class-validator';

export class UpdatePostDto {
export class UpdatePostFolderDto {
@IsNotEmpty()
@IsMongoId()
@ApiProperty()
Expand Down
13 changes: 11 additions & 2 deletions src/modules/posts/posts.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down
30 changes: 29 additions & 1 deletion src/modules/posts/posts.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<any> {
const deleteResult = await this.postModel
.deleteOne({
_id: postId,
Expand Down
18 changes: 16 additions & 2 deletions src/modules/posts/posts.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions src/modules/posts/type/type.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
import { Post } from '@src/infrastructure';

export interface PostUpdateableFields
extends Pick<Post, 'title' | 'isFavorite'> {}