From aea7a9b8bec14cb07edb511b77f3e25aa372ece1 Mon Sep 17 00:00:00 2001 From: LeeSeokHo Date: Sun, 10 Dec 2023 20:03:16 +0900 Subject: [PATCH] refactor: common post module --- src/apis/common-posts/common-posts.module.ts | 7 +++--- .../services/common-posts.service.spec.ts | 16 +++--------- .../services/common-posts.service.ts | 25 +++++-------------- .../common-posts/types/common-post.type.ts | 5 ++++ .../controllers/free-posts.controller.spec.ts | 12 ++------- .../controllers/free-posts.controller.ts | 8 ++---- src/apis/free-posts/free-posts.module.ts | 3 ++- .../services/free-posts.service.spec.ts | 10 +++++++- .../free-posts/services/free-posts.service.ts | 7 ++++++ test/mock/mock.service.ts | 4 +-- 10 files changed, 42 insertions(+), 55 deletions(-) create mode 100644 src/apis/common-posts/types/common-post.type.ts diff --git a/src/apis/common-posts/common-posts.module.ts b/src/apis/common-posts/common-posts.module.ts index 45e1ff57..dc336c2d 100644 --- a/src/apis/common-posts/common-posts.module.ts +++ b/src/apis/common-posts/common-posts.module.ts @@ -1,19 +1,20 @@ -import { DynamicModule, Module } from '@nestjs/common'; +import { DynamicModule, Module, Type } from '@nestjs/common'; import { COMMON_POST_REPOSITORY_TOKEN } from '@src/apis/common-posts/constants/common-posts.token'; import { CommonPostsService } from '@src/apis/common-posts/services/common-posts.service'; +import { RequiredCommonPostColumn } from '@src/apis/common-posts/types/common-post.type'; @Module({ providers: [CommonPostsService], exports: [CommonPostsService], }) export class CommonPostsModule { - static forFeature(postRepository: any): DynamicModule { + static forFeature(postEntity: Type): DynamicModule { return { module: CommonPostsModule, providers: [ { provide: COMMON_POST_REPOSITORY_TOKEN, - useValue: postRepository, + useClass: postEntity, }, ], }; diff --git a/src/apis/common-posts/services/common-posts.service.spec.ts b/src/apis/common-posts/services/common-posts.service.spec.ts index 048f2a8b..ed82ebc7 100644 --- a/src/apis/common-posts/services/common-posts.service.spec.ts +++ b/src/apis/common-posts/services/common-posts.service.spec.ts @@ -1,5 +1,4 @@ import { faker } from '@faker-js/faker'; -import { ModuleRef } from '@nestjs/core'; import { Test, TestingModule } from '@nestjs/testing'; import { COMMON_POST_REPOSITORY_TOKEN } from '@src/apis/common-posts/constants/common-posts.token'; import { HttpNotFoundException } from '@src/http-exceptions/exceptions/http-not-found.exception'; @@ -10,7 +9,7 @@ const mockPostRepository = { }; describe(CommonPostsService.name, () => { - let service: CommonPostsService; + let service: CommonPostsService; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ @@ -18,21 +17,12 @@ describe(CommonPostsService.name, () => { CommonPostsService, { provide: COMMON_POST_REPOSITORY_TOKEN, - useValue: class PostRepository {}, - }, - { - provide: ModuleRef, - useValue: { - get() { - return mockPostRepository; - }, - }, + useValue: mockPostRepository, }, ], }).compile(); - service = module.get(CommonPostsService); - service.onModuleInit(); + service = module.get>(CommonPostsService); }); beforeEach(() => { diff --git a/src/apis/common-posts/services/common-posts.service.ts b/src/apis/common-posts/services/common-posts.service.ts index bfcbe491..2590bdf1 100644 --- a/src/apis/common-posts/services/common-posts.service.ts +++ b/src/apis/common-posts/services/common-posts.service.ts @@ -1,37 +1,24 @@ -import { Inject, Injectable, OnModuleInit } from '@nestjs/common'; -import { ModuleRef } from '@nestjs/core'; +import { Inject, Injectable } from '@nestjs/common'; import { CommonPostStatus } from '@src/apis/common-posts/constants/common-posts.enum'; import { COMMON_POST_REPOSITORY_TOKEN } from '@src/apis/common-posts/constants/common-posts.token'; +import { RequiredCommonPostColumn } from '@src/apis/common-posts/types/common-post.type'; import { COMMON_ERROR_CODE } from '@src/constants/error/common/common-error-code.constant'; import { HttpNotFoundException } from '@src/http-exceptions/exceptions/http-not-found.exception'; -import { Repository } from 'typeorm'; +import { FindOptionsWhere, Repository } from 'typeorm'; @Injectable() -export class CommonPostsService implements OnModuleInit { - private postRepository: Repository; - +export class CommonPostsService { constructor( @Inject(COMMON_POST_REPOSITORY_TOKEN) - private readonly PostRepository: typeof Repository, - - private readonly moduleRef: ModuleRef, + private readonly postRepository: Repository, ) {} - onModuleInit() { - this.postRepository = this.moduleRef.get>( - this.PostRepository, - { - strict: false, - }, - ); - } - async incrementHit(postId: number): Promise { const updateResult = await this.postRepository.increment( { id: postId, status: CommonPostStatus.Posting, - }, + } as FindOptionsWhere, 'hit', 1, ); diff --git a/src/apis/common-posts/types/common-post.type.ts b/src/apis/common-posts/types/common-post.type.ts new file mode 100644 index 00000000..de105d7b --- /dev/null +++ b/src/apis/common-posts/types/common-post.type.ts @@ -0,0 +1,5 @@ +export interface RequiredCommonPostColumn { + id: number; + status: unknown; + hit: number; +} diff --git a/src/apis/free-posts/controllers/free-posts.controller.spec.ts b/src/apis/free-posts/controllers/free-posts.controller.spec.ts index 72622aad..195c642f 100644 --- a/src/apis/free-posts/controllers/free-posts.controller.spec.ts +++ b/src/apis/free-posts/controllers/free-posts.controller.spec.ts @@ -1,16 +1,12 @@ import { faker } from '@faker-js/faker'; import { Test, TestingModule } from '@nestjs/testing'; -import { CommonPostsService } from '@src/apis/common-posts/services/common-posts.service'; import { CreateFreePostDto } from '@src/apis/free-posts/dto/create-free-post.dto'; import { FindFreePostListQueryDto } from '@src/apis/free-posts/dto/find-free-post-list-query.dto'; import { FreePostDto } from '@src/apis/free-posts/dto/free-post.dto'; import { PatchUpdateFreePostDto } from '@src/apis/free-posts/dto/patch-update-free-post.dto.td'; import { PutUpdateFreePostDto } from '@src/apis/free-posts/dto/put-update-free-post.dto'; import { UserDto } from '@src/apis/users/dto/user.dto'; -import { - mockCommonPostsService, - mockFreePostsService, -} from '@test/mock/mock.service'; +import { mockFreePostsService } from '@test/mock/mock.service'; import { FreePostsService } from '../services/free-posts.service'; import { FreePostsController } from './free-posts.controller'; @@ -21,10 +17,6 @@ describe(FreePostsController.name, () => { const module: TestingModule = await Test.createTestingModule({ controllers: [FreePostsController], providers: [ - { - provide: CommonPostsService, - useValue: mockCommonPostsService, - }, { provide: FreePostsService, useValue: mockFreePostsService, @@ -181,7 +173,7 @@ describe(FreePostsController.name, () => { it('increment hit', async () => { freePostId = faker.number.int(); - mockCommonPostsService.incrementHit.mockResolvedValue(undefined); + mockFreePostsService.incrementHit.mockResolvedValue(undefined); await expect( controller.incrementHit(freePostId), diff --git a/src/apis/free-posts/controllers/free-posts.controller.ts b/src/apis/free-posts/controllers/free-posts.controller.ts index c8be2192..c6ea9f93 100644 --- a/src/apis/free-posts/controllers/free-posts.controller.ts +++ b/src/apis/free-posts/controllers/free-posts.controller.ts @@ -14,7 +14,6 @@ import { } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; import { JwtAuthGuard } from '@src/apis/auth/jwt/jwt.guard'; -import { CommonPostsService } from '@src/apis/common-posts/services/common-posts.service'; import { ApiFreePost } from '@src/apis/free-posts/controllers/free-posts.swagger'; import { FindFreePostListQueryDto } from '@src/apis/free-posts/dto/find-free-post-list-query.dto'; import { FreePostDto } from '@src/apis/free-posts/dto/free-post.dto'; @@ -33,10 +32,7 @@ import { FreePostsService } from '../services/free-posts.service'; @ApiTags('free-posts') @Controller('free-posts') export class FreePostsController { - constructor( - private readonly freePostsService: FreePostsService, - private readonly commonPostsService: CommonPostsService, - ) {} + constructor(private readonly freePostsService: FreePostsService) {} @ApiFreePost.Create({ summary: '자유 게시글 생성' }) @UseGuards(JwtAuthGuard) @@ -119,6 +115,6 @@ export class FreePostsController { incrementHit( @Param('freePostId', ParsePositiveIntPipe) freePostId: number, ): Promise { - return this.commonPostsService.incrementHit(freePostId); + return this.freePostsService.incrementHit(freePostId); } } diff --git a/src/apis/free-posts/free-posts.module.ts b/src/apis/free-posts/free-posts.module.ts index 85575ca5..38ff1982 100644 --- a/src/apis/free-posts/free-posts.module.ts +++ b/src/apis/free-posts/free-posts.module.ts @@ -3,6 +3,7 @@ import { CommonPostsModule } from '@src/apis/common-posts/common-posts.module'; import { FreePostHistoryModule } from '@src/apis/free-posts/free-post-history/free-post-history.module'; import { FreePostRepository } from '@src/apis/free-posts/repositories/free-post.repository'; import { TypeOrmExModule } from '@src/core/type-orm/type-orm-ex.module'; +import { FreePost } from '@src/entities/FreePost'; import { QueryHelper } from '@src/helpers/query.helper'; import { FreePostsController } from './controllers/free-posts.controller'; import { FreePostsService } from './services/free-posts.service'; @@ -11,7 +12,7 @@ import { FreePostsService } from './services/free-posts.service'; imports: [ FreePostHistoryModule, TypeOrmExModule.forCustomRepository([FreePostRepository]), - CommonPostsModule.forFeature(FreePostRepository), + CommonPostsModule.forFeature(FreePost), ], controllers: [FreePostsController], providers: [FreePostsService, QueryHelper], diff --git a/src/apis/free-posts/services/free-posts.service.spec.ts b/src/apis/free-posts/services/free-posts.service.spec.ts index b846a97e..d8b14ca8 100644 --- a/src/apis/free-posts/services/free-posts.service.spec.ts +++ b/src/apis/free-posts/services/free-posts.service.spec.ts @@ -1,5 +1,6 @@ import { faker } from '@faker-js/faker'; import { Test, TestingModule } from '@nestjs/testing'; +import { CommonPostsService } from '@src/apis/common-posts/services/common-posts.service'; import { CreateFreePostDto } from '@src/apis/free-posts/dto/create-free-post.dto'; import { FindFreePostListQueryDto } from '@src/apis/free-posts/dto/find-free-post-list-query.dto'; import { FreePostDto } from '@src/apis/free-posts/dto/free-post.dto'; @@ -17,7 +18,10 @@ import { mockDataSource, mockFreePostRepository, } from '@test/mock/mock.repository'; -import { mockFreePostHistoryService } from '@test/mock/mock.service'; +import { + mockCommonPostsService, + mockFreePostHistoryService, +} from '@test/mock/mock.service'; import { DataSource } from 'typeorm'; import { FreePostsService } from './free-posts.service'; @@ -28,6 +32,10 @@ describe(FreePostsService.name, () => { const module: TestingModule = await Test.createTestingModule({ providers: [ FreePostsService, + { + provide: CommonPostsService, + useValue: mockCommonPostsService, + }, { provide: FreePostHistoryService, useValue: mockFreePostHistoryService, diff --git a/src/apis/free-posts/services/free-posts.service.ts b/src/apis/free-posts/services/free-posts.service.ts index c194819a..f8c2625d 100644 --- a/src/apis/free-posts/services/free-posts.service.ts +++ b/src/apis/free-posts/services/free-posts.service.ts @@ -1,4 +1,5 @@ import { Injectable } from '@nestjs/common'; +import { CommonPostsService } from '@src/apis/common-posts/services/common-posts.service'; import { FreePostStatus } from '@src/apis/free-posts/constants/free-post.enum'; import { FindFreePostListQueryDto } from '@src/apis/free-posts/dto/find-free-post-list-query.dto'; import { FreePostDto } from '@src/apis/free-posts/dto/free-post.dto'; @@ -10,6 +11,7 @@ import { FreePostRepository } from '@src/apis/free-posts/repositories/free-post. import { HistoryAction } from '@src/constants/enum'; import { COMMON_ERROR_CODE } from '@src/constants/error/common/common-error-code.constant'; import { ERROR_CODE } from '@src/constants/error/error-code.constant'; +import { FreePost } from '@src/entities/FreePost'; import { QueryHelper } from '@src/helpers/query.helper'; import { HttpBadRequestException } from '@src/http-exceptions/exceptions/http-bad-request.exception'; import { HttpForbiddenException } from '@src/http-exceptions/exceptions/http-forbidden.exception'; @@ -28,6 +30,7 @@ export class FreePostsService { constructor( private readonly freePostHistoryService: FreePostHistoryService, + private readonly commonPostsService: CommonPostsService, private readonly queryHelper: QueryHelper, @@ -325,4 +328,8 @@ export class FreePostsService { } } } + + incrementHit(freePostId: number): Promise { + return this.commonPostsService.incrementHit(freePostId); + } } diff --git a/test/mock/mock.service.ts b/test/mock/mock.service.ts index 29f73370..dde06ca7 100644 --- a/test/mock/mock.service.ts +++ b/test/mock/mock.service.ts @@ -47,8 +47,7 @@ export const mockUserHistoryService: MockProvider = { create: jest.fn(), }; -export const mockCommonPostsService: MockProvider = { - onModuleInit: jest.fn(), +export const mockCommonPostsService: MockProvider> = { incrementHit: jest.fn(), }; @@ -59,6 +58,7 @@ export const mockFreePostsService: MockProvider = { findOneOrNotFound: jest.fn(), putUpdate: jest.fn(), remove: jest.fn(), + incrementHit: jest.fn(), }; export const mockFreePostHistoryService: MockProvider =