Skip to content

Commit

Permalink
Feat: Post 폴더 이동 API 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Hoplin committed Jun 29, 2024
1 parent f2f8e2e commit 6dc4b18
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/modules/posts/docs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './createPost.docs';
export * from './updatePost.docs';
9 changes: 9 additions & 0 deletions src/modules/posts/docs/updatePost.docs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { applyDecorators } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation } from '@nestjs/swagger';

export const UpdatePostDocs = applyDecorators(
ApiOperation({
summary: 'URL 폴더 변경',
}),
ApiBearerAuth(),
);
7 changes: 3 additions & 4 deletions src/modules/posts/dto/create-post.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/modules/posts/dto/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './create-post.dto';
export * from './update-post.dto';
9 changes: 9 additions & 0 deletions src/modules/posts/dto/update-post.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsMongoId, IsNotEmpty } from 'class-validator';

export class UpdatePostDto {
@IsNotEmpty()
@IsMongoId()
@ApiProperty()
folderId: string;
}
24 changes: 21 additions & 3 deletions src/modules/posts/posts.controller.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -21,4 +29,14 @@ export class PostsController {
): Promise<boolean> {
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);
}
}
34 changes: 33 additions & 1 deletion src/modules/posts/posts.repository.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -9,6 +13,34 @@ export class PostsRepository {
@InjectModel(Post.name) private readonly postModel: Model<Post>,
) {}

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,
Expand Down
12 changes: 12 additions & 0 deletions src/modules/posts/posts.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
}
}

0 comments on commit 6dc4b18

Please sign in to comment.