Skip to content

Commit

Permalink
Feature/#38 가격 하락률 기준 주식 리스트 조회 기능 구현 (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
xjfcnfw3 authored Nov 18, 2024
2 parents bcc9a09 + 6710d43 commit 0d77901
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
6 changes: 6 additions & 0 deletions packages/backend/src/stock/stock.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,10 @@ export class StockController {
async getTopStocksByGainers(@LimitQuery(20) limit: number) {
return await this.stockService.getTopStocksByGainers(limit);
}

@Get('topLosers')
@ApiGetStocks('가격 하락률 기반 주식 리스트 조회 API')
async getTopStocksByLosers(@LimitQuery(20) limit: number) {
return await this.stockService.getTopStocksByLosers(limit);
}
}
42 changes: 21 additions & 21 deletions packages/backend/src/stock/stock.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,23 +278,15 @@ describe('StockService 테스트', () => {
expect(result).toBe(false);
});

test('주식 조회수 기준 상위 데이터를 반환한다.', async () => {
const limit = 5;
test('주식 하락률 기준 상위 데이터를 반환한다.', async () => {
const limit = 20;
// QueryBuilder Mock
const queryBuilderMock = {
leftJoin: jest.fn().mockReturnThis(),
select: jest.fn().mockReturnThis(),
orderBy: jest.fn().mockReturnThis(),
limit: jest.fn().mockReturnThis(),
getRawMany: jest.fn().mockResolvedValue([
{
id: 'A005930',
name: '삼성전자',
currentPrice: '100000.0',
changeRate: '2.5',
volume: '500000',
marketCap: '500000000000.00',
},
{
id: 'A051910',
name: 'LG화학',
Expand All @@ -303,6 +295,14 @@ describe('StockService 테스트', () => {
volume: '300000',
marketCap: '20000000000.00',
},
{
id: 'A005930',
name: '삼성전자',
currentPrice: '100000.0',
changeRate: '2.5',
volume: '500000',
marketCap: '500000000000.00',
},
]),
};

Expand All @@ -315,25 +315,17 @@ describe('StockService 테스트', () => {
const dataSource = createDataSourceMock(managerMock);
const stockService = new StockService(dataSource as DataSource, logger);

const result = await stockService.getTopStocksByViews(limit);
const result = await stockService.getTopStocksByLosers(limit);

expect(managerMock.getRepository).toHaveBeenCalledWith(Stock);
expect(queryBuilderMock.orderBy).toHaveBeenCalledWith(
'stock.views',
'DESC',
'stockLiveData.changeRate',
'ASC',
);
expect(queryBuilderMock.limit).toHaveBeenCalledWith(limit);
expect(queryBuilderMock.getRawMany).toHaveBeenCalled();

expect(instanceToPlain(result)).toEqual([
{
id: 'A005930',
name: '삼성전자',
currentPrice: 100000.0,
changeRate: 2.5,
volume: 500000,
marketCap: '500000000000.00',
},
{
id: 'A051910',
name: 'LG화학',
Expand All @@ -342,6 +334,14 @@ describe('StockService 테스트', () => {
volume: 300000,
marketCap: '20000000000.00',
},
{
id: 'A005930',
name: '삼성전자',
currentPrice: 100000.0,
changeRate: 2.5,
volume: 500000,
marketCap: '500000000000.00',
},
]);
});
});
9 changes: 9 additions & 0 deletions packages/backend/src/stock/stock.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,13 @@ export class StockService {

return plainToInstance(StocksResponse, rawData);
}

async getTopStocksByLosers(limit: number) {
const rawData = await this.StocksQuery()
.orderBy('stockLiveData.changeRate', 'ASC')
.limit(limit)
.getRawMany();

return plainToInstance(StocksResponse, rawData);
}
}

0 comments on commit 0d77901

Please sign in to comment.