Skip to content

Commit

Permalink
Merge pull request #73 from mash-up-kr/fix/check-folder-duplication
Browse files Browse the repository at this point in the history
Fix/check folder duplication
  • Loading branch information
J-Hoplin authored Jul 20, 2024
2 parents c32bd49 + 22f4240 commit cf1fa3a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/modules/folders/docs/folder-api.docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
} from '@nestjs/swagger';
import {
FolderListResponse,
FolderPostResponse,
FolderResponse,
PostListInFolderResponse,
} from '../responses';

export const CreateFolderDocs = applyDecorators(
Expand Down Expand Up @@ -50,7 +50,7 @@ export const FindLinksInFolderDocs = applyDecorators(
description: '',
}),
ApiResponse({
type: PostListInFolderResponse,
type: FolderPostResponse,
}),
);

Expand Down
7 changes: 6 additions & 1 deletion src/modules/folders/error/index.ts
Original file line number Diff line number Diff line change
@@ -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', '폴더가 존재하지 않습니다!');
10 changes: 6 additions & 4 deletions src/modules/folders/folders.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/modules/folders/folders.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FolderDocument>) {
const folder = await this.folderModel.findOne(param).exec();
if (!folder) {
Expand Down
12 changes: 11 additions & 1 deletion src/modules/folders/folders.service.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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,
Expand Down
23 changes: 10 additions & 13 deletions src/modules/folders/responses/post-list-in-folder.response.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit cf1fa3a

Please sign in to comment.