From 1a134140168f6bc6924c6bda2cf599ce8e636c68 Mon Sep 17 00:00:00 2001 From: J-hoplin1 Date: Sun, 21 Jul 2024 00:29:11 +0900 Subject: [PATCH 1/2] Fix: Add folder duplication check API --- src/modules/folders/error/index.ts | 7 ++++++- src/modules/folders/folders.repository.ts | 10 ++++++++++ src/modules/folders/folders.service.ts | 12 +++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/modules/folders/error/index.ts b/src/modules/folders/error/index.ts index 1c6c7c9..298daa9 100644 --- a/src/modules/folders/error/index.ts +++ b/src/modules/folders/error/index.ts @@ -1,5 +1,10 @@ import { createErrorObject } from '@src/common'; -export const F001 = createErrorObject('F001', '폴더 이름이 중복되었습니다!'); +export const F001 = (folderName: string) => { + return createErrorObject( + 'F001', + `폴더 이름이 중복되었습니다 - ${folderName}`, + ); +}; export const F002 = createErrorObject('F002', '폴더가 존재하지 않습니다!'); diff --git a/src/modules/folders/folders.repository.ts b/src/modules/folders/folders.repository.ts index 7f94d86..9beabd1 100644 --- a/src/modules/folders/folders.repository.ts +++ b/src/modules/folders/folders.repository.ts @@ -34,6 +34,16 @@ export class FolderRepository { return folders; } + async checkUserHasFolder(userId: string, name: string) { + const checkFolder = await this.folderModel + .findOne({ + userId: userId, + name: name, + }) + .exec(); + return checkFolder ? true : false; + } + async findOneOrFail(param: FilterQuery) { const folder = await this.folderModel.findOne(param).exec(); if (!folder) { diff --git a/src/modules/folders/folders.service.ts b/src/modules/folders/folders.service.ts index b140123..40fa80d 100644 --- a/src/modules/folders/folders.service.ts +++ b/src/modules/folders/folders.service.ts @@ -1,10 +1,11 @@ -import { Injectable } from '@nestjs/common'; +import { BadRequestException, Injectable } from '@nestjs/common'; import { sum } from '@src/common'; import { FolderType } from '@src/infrastructure/database/types/folder-type.enum'; import { Schema as MongooseSchema } from 'mongoose'; import { PostsRepository } from '../posts/posts.repository'; import { FolderListServiceDto } from './dto/folder-with-count.dto'; import { CreateFolderDto, UpdateFolderDto } from './dto/mutate-folder.dto'; +import { F001 } from './error'; import { FolderRepository } from './folders.repository'; @Injectable() @@ -15,6 +16,15 @@ export class FoldersService { ) {} async createMany(userId: string, createFolderDto: CreateFolderDto) { + for (const folderName of createFolderDto.names) { + const isExist = await this.folderRepository.checkUserHasFolder( + userId, + folderName, + ); + if (isExist) { + throw new BadRequestException(F001(folderName)); + } + } const folders = createFolderDto.names.map((name) => ({ userId: userId, name, From 22f4240cd02f5784654d2f976547db724a06b986 Mon Sep 17 00:00:00 2001 From: J-hoplin1 Date: Sun, 21 Jul 2024 00:37:58 +0900 Subject: [PATCH 2/2] Fix: Fix folder post response type --- src/modules/folders/docs/folder-api.docs.ts | 4 ++-- src/modules/folders/folders.controller.ts | 10 ++++---- .../responses/post-list-in-folder.response.ts | 23 ++++++++----------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/modules/folders/docs/folder-api.docs.ts b/src/modules/folders/docs/folder-api.docs.ts index baef103..f90180a 100644 --- a/src/modules/folders/docs/folder-api.docs.ts +++ b/src/modules/folders/docs/folder-api.docs.ts @@ -6,8 +6,8 @@ import { } from '@nestjs/swagger'; import { FolderListResponse, + FolderPostResponse, FolderResponse, - PostListInFolderResponse, } from '../responses'; export const CreateFolderDocs = applyDecorators( @@ -50,7 +50,7 @@ export const FindLinksInFolderDocs = applyDecorators( description: '', }), ApiResponse({ - type: PostListInFolderResponse, + type: FolderPostResponse, }), ); diff --git a/src/modules/folders/folders.controller.ts b/src/modules/folders/folders.controller.ts index e4bd5bf..fb76a44 100644 --- a/src/modules/folders/folders.controller.ts +++ b/src/modules/folders/folders.controller.ts @@ -9,7 +9,7 @@ import { Query, UseGuards, } from '@nestjs/common'; -import { GetUser } from '@src/common'; +import { GetUser, PaginationMetadata } from '@src/common'; import { GetPostQueryDto } from '../posts/dto/find-in-folder.dto'; import { PostsService } from '../posts/posts.service'; import { JwtGuard } from '../users/guards'; @@ -26,10 +26,11 @@ import { CreateFolderDto, UpdateFolderDto } from './dto'; import { FoldersService } from './folders.service'; import { FolderListResponse, + FolderPostResponse, FolderResponse, FolderSummaryResponse, - PostListInFolderResponse, } from './responses'; +import { PostResponse } from './responses/post.response'; @FolderControllerDocs @UseGuards(JwtGuard) @@ -88,12 +89,13 @@ export class FoldersController { query, ); - return new PostListInFolderResponse( + const metadata = new PaginationMetadata( query.page, query.limit, result.count, - result.posts, ); + const posts = result.posts.map((post) => new PostResponse(post)); + return new FolderPostResponse(metadata, posts); } @UpdateFolderDocs diff --git a/src/modules/folders/responses/post-list-in-folder.response.ts b/src/modules/folders/responses/post-list-in-folder.response.ts index d0d7e90..1765ed3 100644 --- a/src/modules/folders/responses/post-list-in-folder.response.ts +++ b/src/modules/folders/responses/post-list-in-folder.response.ts @@ -1,21 +1,18 @@ import { ApiProperty } from '@nestjs/swagger'; -import { FolderResponse } from './folder.response'; -import { PostResponse } from './post.response'; -import { PostDocument } from '@src/infrastructure'; import { PaginationMetadata } from '@src/common'; +import { PostResponse } from './post.response'; + +export class FolderPostResponse { + @ApiProperty({ + type: PaginationMetadata, + }) + meatadata: PaginationMetadata; -export class PostListInFolderResponse extends PaginationMetadata { @ApiProperty({ type: PostResponse, isArray: true }) list: PostResponse[]; - constructor( - page: number, - limit: number, - total: number, - list: PostDocument[], - ) { - super(page, limit, total); - - this.list = list.map((post) => new PostResponse(post)); + constructor(metadata: PaginationMetadata, posts: PostResponse[]) { + this.meatadata = metadata; + this.list = posts; } }