-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: import 문 최적화 * feat: 피드 resource 생성 * feat: checklist entity에 카테고리 컬럼 추가 * feat: feedmodel 정의 private checklist model에서 likeCount와 downloadCount 컬럼 추가 * chore: 사용하지 않는 dto 파일 삭제 * chore: 카테고리 데이터 추가 * chore: 안쓰는 테스트 파일 삭제 * feat: 피드 화면 api 구현 * feat: feeds.service.spec.ts 테스트 코드 작성 * feat: api 에러 핸들링 로직 추가 * test: feeds.service.spec.ts 예외 케이스에 대한 테스트 코드 추가 * feat: 에러메시지 수정
- Loading branch information
1 parent
55ade94
commit 0014655
Showing
10 changed files
with
271 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; | ||
import { PrivateChecklistModel } from '../../folders/private-checklists/entities/private-checklist.entity'; | ||
|
||
@Entity() | ||
export class FeedModel extends PrivateChecklistModel { | ||
@PrimaryGeneratedColumn() | ||
feedId: number; | ||
|
||
@Column({ default: 0 }) | ||
likeCount: number; | ||
|
||
@Column({ default: 0 }) | ||
downloadCount: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Controller, Get, Param, Post, Query } from '@nestjs/common'; | ||
import { FeedsService } from './feeds.service'; | ||
|
||
@Controller('feeds') | ||
export class FeedsController { | ||
constructor(private readonly feedsService: FeedsService) {} | ||
|
||
@Get('category') | ||
getAllFeedsByCategory(@Query('category') category: string) { | ||
return this.feedsService.findAllFeedsByCategory(category); | ||
} | ||
|
||
@Post('like/:checklistId') | ||
postLike(@Param('checklistId') id: number) { | ||
return this.feedsService.updateLikeCount(id); | ||
} | ||
|
||
@Post('download/:checklistId') | ||
postDownload(@Param('checklistId') id: number) { | ||
return this.feedsService.updateDownloadCount(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { FeedsService } from './feeds.service'; | ||
import { FeedsController } from './feeds.controller'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { FeedModel } from './entity/feed.entity'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([FeedModel])], | ||
controllers: [FeedsController], | ||
providers: [FeedsService], | ||
}) | ||
export class FeedsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { BadRequestException } from '@nestjs/common'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { getRepositoryToken } from '@nestjs/typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { FeedModel } from './entity/feed.entity'; | ||
import { FeedsService } from './feeds.service'; | ||
|
||
type MockRepository<T = any> = Partial<Record<keyof Repository<T>, jest.Mock>>; | ||
|
||
describe('FeedsService', () => { | ||
let service: FeedsService; | ||
let mockFeedsRepository: MockRepository<FeedModel>; | ||
|
||
beforeEach(async () => { | ||
mockFeedsRepository = { | ||
findOne: jest.fn(), | ||
find: jest.fn(), | ||
save: jest.fn(), | ||
}; | ||
|
||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
FeedsService, | ||
{ | ||
provide: getRepositoryToken(FeedModel), | ||
useValue: mockFeedsRepository, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<FeedsService>(FeedsService); | ||
}); | ||
|
||
it('findFeedById(feedId): 피드 ID로 피드를 찾는다', async () => { | ||
const feedId = 1; | ||
const mockFeed = { feedId, likeCount: 10, downloadCount: 5 }; | ||
mockFeedsRepository.findOne.mockResolvedValue(mockFeed); | ||
|
||
const result = await service.findFeedById(feedId); | ||
|
||
expect(mockFeedsRepository.findOne).toHaveBeenCalledWith({ | ||
where: { feedId }, | ||
}); | ||
expect(result).toEqual(mockFeed); | ||
}); | ||
|
||
it('findFeedById(feedId): 존재하지 않는 피드 ID에 대한 예외 처리', async () => { | ||
mockFeedsRepository.findOne.mockResolvedValue(undefined); | ||
|
||
await expect(service.findFeedById(9999)).rejects.toThrow( | ||
BadRequestException, | ||
); | ||
}); | ||
|
||
it('findAllFeedsByCategory(mainCategory): 주어진 카테고리의 모든 피드를 찾는다', async () => { | ||
const mainCategory = 'Sports'; | ||
const mockFeeds = [ | ||
{ feedId: 1, mainCategory }, | ||
{ feedId: 2, mainCategory }, | ||
]; | ||
mockFeedsRepository.find.mockResolvedValue(mockFeeds); | ||
|
||
const result = await service.findAllFeedsByCategory(mainCategory); | ||
|
||
expect(mockFeedsRepository.find).toHaveBeenCalledWith({ | ||
where: { mainCategory }, | ||
}); | ||
expect(result).toEqual(mockFeeds); | ||
}); | ||
|
||
it('findAllFeedsByCategory(mainCategory): 주어진 카테고리에 해당하는 피드가 없을 경우 예외를 던진다', async () => { | ||
const mainCategory = 'NonExistingCategory'; | ||
mockFeedsRepository.find.mockResolvedValue([]); | ||
|
||
await expect(service.findAllFeedsByCategory(mainCategory)).rejects.toThrow( | ||
BadRequestException, | ||
); | ||
}); | ||
|
||
it('updateLikeCount(feedId): 피드의 좋아요 수를 업데이트한다', async () => { | ||
const feedId = 1; | ||
const mockFeed = { feedId, likeCount: 10, downloadCount: 5 }; | ||
mockFeedsRepository.findOne.mockResolvedValue(mockFeed); | ||
mockFeedsRepository.save.mockResolvedValue({ ...mockFeed, likeCount: 11 }); | ||
|
||
const result = await service.updateLikeCount(feedId); | ||
|
||
expect(mockFeedsRepository.findOne).toHaveBeenCalledWith({ | ||
where: { feedId }, | ||
}); | ||
expect(mockFeedsRepository.save).toHaveBeenCalledWith({ | ||
...mockFeed, | ||
likeCount: 11, | ||
}); | ||
expect(result.likeCount).toEqual(11); | ||
}); | ||
|
||
it('updateDownloadCount(feedId): 피드의 다운로드 수를 업데이트한다', async () => { | ||
const feedId = 1; | ||
const mockFeed = { feedId, likeCount: 10, downloadCount: 5 }; | ||
mockFeedsRepository.findOne.mockResolvedValue(mockFeed); | ||
mockFeedsRepository.save.mockResolvedValue({ | ||
...mockFeed, | ||
downloadCount: 6, | ||
}); | ||
|
||
const result = await service.updateDownloadCount(feedId); | ||
|
||
expect(mockFeedsRepository.findOne).toHaveBeenCalledWith({ | ||
where: { feedId }, | ||
}); | ||
expect(mockFeedsRepository.save).toHaveBeenCalledWith({ | ||
...mockFeed, | ||
downloadCount: 6, | ||
}); | ||
expect(result.downloadCount).toEqual(6); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { BadRequestException, Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { FeedModel } from './entity/feed.entity'; | ||
import { Repository } from 'typeorm'; | ||
|
||
@Injectable() | ||
export class FeedsService { | ||
constructor( | ||
@InjectRepository(FeedModel) | ||
private readonly repository: Repository<FeedModel>, | ||
) {} | ||
|
||
async findFeedById(feedId: number) { | ||
const feed = await this.repository.findOne({ where: { feedId } }); | ||
if (!feed) { | ||
throw new BadRequestException( | ||
`${feedId}는 존재하지 않는 피드 id 입니다.`, | ||
); | ||
} | ||
return feed; | ||
} | ||
|
||
async findAllFeedsByCategory(mainCategory: string) { | ||
const feed = await this.repository.find({ where: { mainCategory } }); | ||
if (feed.length === 0) { | ||
throw new BadRequestException( | ||
`${mainCategory}에 대한 피드가 존재하지 않습니다.`, | ||
); | ||
} | ||
return feed; | ||
} | ||
|
||
async updateLikeCount(feedId: number) { | ||
const feed = await this.findFeedById(feedId); | ||
feed.likeCount += 1; | ||
return this.repository.save(feed); | ||
} | ||
|
||
async updateDownloadCount(feedId: number) { | ||
const feed = await this.findFeedById(feedId); | ||
feed.downloadCount += 1; | ||
return this.repository.save(feed); | ||
} | ||
} |