Skip to content

Commit

Permalink
Merge branch 'develop' into feat/#105/free_post_comment
Browse files Browse the repository at this point in the history
  • Loading branch information
rrgks6221 committed Dec 14, 2023
2 parents 78fe26c + edefa74 commit f36c172
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 55 deletions.
7 changes: 4 additions & 3 deletions src/apis/common-posts/common-posts.module.ts
Original file line number Diff line number Diff line change
@@ -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<RequiredCommonPostColumn>): DynamicModule {
return {
module: CommonPostsModule,
providers: [
{
provide: COMMON_POST_REPOSITORY_TOKEN,
useValue: postRepository,
useClass: postEntity,
},
],
};
Expand Down
16 changes: 3 additions & 13 deletions src/apis/common-posts/services/common-posts.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -10,29 +9,20 @@ const mockPostRepository = {
};

describe(CommonPostsService.name, () => {
let service: CommonPostsService;
let service: CommonPostsService<any>;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
CommonPostsService,
{
provide: COMMON_POST_REPOSITORY_TOKEN,
useValue: class PostRepository {},
},
{
provide: ModuleRef,
useValue: {
get() {
return mockPostRepository;
},
},
useValue: mockPostRepository,
},
],
}).compile();

service = module.get<CommonPostsService>(CommonPostsService);
service.onModuleInit();
service = module.get<CommonPostsService<any>>(CommonPostsService);
});

beforeEach(() => {
Expand Down
25 changes: 6 additions & 19 deletions src/apis/common-posts/services/common-posts.service.ts
Original file line number Diff line number Diff line change
@@ -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<any>;

export class CommonPostsService<E extends RequiredCommonPostColumn> {
constructor(
@Inject(COMMON_POST_REPOSITORY_TOKEN)
private readonly PostRepository: typeof Repository<any>,

private readonly moduleRef: ModuleRef,
private readonly postRepository: Repository<E>,
) {}

onModuleInit() {
this.postRepository = this.moduleRef.get<Repository<any>>(
this.PostRepository,
{
strict: false,
},
);
}

async incrementHit(postId: number): Promise<void> {
const updateResult = await this.postRepository.increment(
{
id: postId,
status: CommonPostStatus.Posting,
},
} as FindOptionsWhere<E>,
'hit',
1,
);
Expand Down
5 changes: 5 additions & 0 deletions src/apis/common-posts/types/common-post.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface RequiredCommonPostColumn {
id: number;
status: unknown;
hit: number;
}
12 changes: 2 additions & 10 deletions src/apis/free-posts/controllers/free-posts.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -21,10 +17,6 @@ describe(FreePostsController.name, () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [FreePostsController],
providers: [
{
provide: CommonPostsService,
useValue: mockCommonPostsService,
},
{
provide: FreePostsService,
useValue: mockFreePostsService,
Expand Down Expand Up @@ -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),
Expand Down
8 changes: 2 additions & 6 deletions src/apis/free-posts/controllers/free-posts.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { CreateFreePostCommentDto } from '@src/apis/free-posts/dto/create-free-post-comment.dto';
import { CreateFreePostReplyCommentDto } from '@src/apis/free-posts/dto/create-free-post-reply-comment.dto';
Expand Down Expand Up @@ -43,10 +42,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)
Expand Down Expand Up @@ -129,7 +125,7 @@ export class FreePostsController {
incrementHit(
@Param('freePostId', ParsePositiveIntPipe) freePostId: number,
): Promise<void> {
return this.commonPostsService.incrementHit(freePostId);
return this.freePostsService.incrementHit(freePostId);
}

@ApiFreePost.CreateComment({ summary: '자유 게시글 댓글 생성' })
Expand Down
3 changes: 2 additions & 1 deletion src/apis/free-posts/free-posts.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { FreePostCommentRepository } from '@src/apis/free-posts/repositories/fre
import { FreePostReplyCommentRepository } from '@src/apis/free-posts/repositories/free-post-reply-comment.repository';
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';
Expand All @@ -17,7 +18,7 @@ import { FreePostsService } from './services/free-posts.service';
FreePostCommentRepository,
FreePostReplyCommentRepository,
]),
CommonPostsModule.forFeature(FreePostRepository),
CommonPostsModule.forFeature(FreePost),
],
controllers: [FreePostsController],
providers: [FreePostsService, QueryHelper],
Expand Down
10 changes: 9 additions & 1 deletion src/apis/free-posts/services/free-posts.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';

Expand All @@ -28,6 +32,10 @@ describe(FreePostsService.name, () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
FreePostsService,
{
provide: CommonPostsService,
useValue: mockCommonPostsService,
},
{
provide: FreePostHistoryService,
useValue: mockFreePostHistoryService,
Expand Down
7 changes: 7 additions & 0 deletions src/apis/free-posts/services/free-posts.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Injectable } from '@nestjs/common';
import { CommonPostsService } from '@src/apis/common-posts/services/common-posts.service';
import { FreePostCommentStatus } from '@src/apis/free-posts/constants/free-post-comment.enum';
import { FreePostStatus } from '@src/apis/free-posts/constants/free-post.enum';
import { CreateFreePostCommentDto } from '@src/apis/free-posts/dto/create-free-post-comment.dto';
Expand All @@ -23,6 +24,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 { FreePostComment } from '@src/entities/FreePostComment';
import { FreePostReplyComment } from '@src/entities/FreePostReplyComment';
import { QueryHelper } from '@src/helpers/query.helper';
Expand All @@ -44,6 +46,7 @@ export class FreePostsService {

constructor(
private readonly freePostHistoryService: FreePostHistoryService,
private readonly commonPostsService: CommonPostsService<FreePost>,

private readonly queryHelper: QueryHelper,

Expand Down Expand Up @@ -344,6 +347,10 @@ export class FreePostsService {
}
}

incrementHit(freePostId: number): Promise<void> {
return this.commonPostsService.incrementHit(freePostId);
}

async createComment(
userId: number,
freePostId: number,
Expand Down
4 changes: 2 additions & 2 deletions test/mock/mock.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ export const mockUserHistoryService: MockProvider<UserHistoryService> = {
create: jest.fn(),
};

export const mockCommonPostsService: MockProvider<CommonPostsService> = {
onModuleInit: jest.fn(),
export const mockCommonPostsService: MockProvider<CommonPostsService<any>> = {
incrementHit: jest.fn(),
};

Expand All @@ -59,6 +58,7 @@ export const mockFreePostsService: MockProvider<FreePostsService> = {
findOneOrNotFound: jest.fn(),
putUpdate: jest.fn(),
remove: jest.fn(),
incrementHit: jest.fn(),
};

export const mockFreePostHistoryService: MockProvider<FreePostHistoryService> =
Expand Down

0 comments on commit f36c172

Please sign in to comment.