Skip to content

Commit

Permalink
✨ feat: 주식 현재가 API 요청 로직 구현#54
Browse files Browse the repository at this point in the history
  • Loading branch information
uuuo3o committed Nov 13, 2024
1 parent fc9a917 commit 5f7d33c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
23 changes: 21 additions & 2 deletions BE/src/stock/detail/stock-detail.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
import { Body, Controller, Param, Post } from '@nestjs/common';
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { ApiBody, ApiOperation, ApiParam, ApiResponse } from '@nestjs/swagger';
import { StockDetailService } from './stock-detail.service';
import { InquirePriceResponseDto } from './dto/stock-detail-response.dto';
import { StockDetailChartRequestDto } from './dto/stock-detail-chart-request.dto';
import { InquirePriceChartResponseDto } from './dto/stock-detail-chart-response.dto';

@Controller('/api/stocks')
export class StockDetailController {
constructor(private readonly stockDetailService: StockDetailService) {}

@Get(':stockCode')
@ApiOperation({ summary: '단일 주식 종목 detail 페이지 상단부 조회 API' })
@ApiParam({
name: 'stockCode',
required: true,
description:
'종목 코드\n' +
'(ex) 005930 삼성전자 / 005380 현대차 / 001500 현대차증권',
})
@ApiResponse({
status: 200,
description: '단일 주식 종목 기본값 조회 성공',
type: InquirePriceResponseDto,
})
getStockDetail(@Param('stockCode') stockCode: string) {
return this.stockDetailService.getInquirePrice(stockCode);
}

@Post(':stockCode')
@ApiOperation({ summary: '국내주식기간별시세(일/주/월/년) 조회 API' })
@ApiParam({
Expand All @@ -30,7 +49,7 @@ export class StockDetailController {
description: '국내주식기간별시세(일/주/월/년) 조회 성공',
type: InquirePriceChartResponseDto,
})
getStockDetail(
getStockDetailChart(
@Param('stockCode') stockCode: string,
@Body() body: StockDetailChartRequestDto,
) {
Expand Down
63 changes: 60 additions & 3 deletions BE/src/stock/detail/stock-detail.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,72 @@ import { Injectable, Logger } from '@nestjs/common';
import { KoreaInvestmentService } from '../../koreaInvestment/korea-investment.service';
import { getHeader } from '../../util/get-header';
import { getFullURL } from '../../util/get-full-URL';
import { InquirePriceApiResponse } from './interface/stock-detail-chart.interface';
import { InquirePriceChartApiResponse } from './interface/stock-detail-chart.interface';
import { InquirePriceChartDataDto } from './dto/stock-detail-chart-data.dto';
import {
InquirePriceApiResponse,
InquirePriceOutputData,
} from './interface/stock-detail.interface';
import { InquirePriceResponseDto } from './dto/stock-detail-response.dto';

@Injectable()
export class StockDetailService {
private readonly logger = new Logger();

constructor(private readonly koreaInvetmentService: KoreaInvestmentService) {}

/**
* 주식현재가 시세 데이터를 반환하는 함수
* @param {string} stockCode - 종목코드
* @returns - 주식현재가 시세 데이터 객체 반환
*
* @author uuuo3o
*/
async getInquirePrice(stockCode: string) {
try {
const queryParams = {
fid_cond_mrkt_div_code: 'J',
fid_input_iscd: stockCode,
};

const response = await this.requestApi<InquirePriceApiResponse>(
'FHKST01010100',
'/uapi/domestic-stock/v1/quotations/inquire-price',
queryParams,
);

return this.formatStockData(response.output);
} catch (error) {
this.logger.error('API Error Details:', {
status: error.response?.status,
statusText: error.response?.statusText,
data: error.response?.data,
headers: error.response?.config?.headers, // 실제 요청 헤더
message: error.message,
});
throw error;
}
}

/**
* @private API에서 받은 주식현재가 시세 데이터를 필요한 정보로 정제하는 함수
* @param {InquirePriceOutputData} stock - API 응답에서 받은 원시 데이터
* @returns - 필요한 정보만 추출한 데이터 배열
*
* @author uuuo3o
*/
private formatStockData(stock: InquirePriceOutputData) {
const stockData = new InquirePriceResponseDto();
stockData.stck_shrn_iscd = stock.stck_shrn_iscd;
stockData.stck_prpr = stock.stck_prpr;
stockData.prdy_vrss = stock.prdy_vrss;
stockData.prdy_vrss_sign = stock.prdy_vrss_sign;
stockData.prdy_ctrt = stock.prdy_ctrt;
stockData.hts_avls = stock.hts_avls;
stockData.per = stock.per;
return stockData;
}

/**
* 특정 주식의 기간별시세 데이터를 반환하는 함수
* @param {string} stockCode - 종목코드
Expand All @@ -38,7 +95,7 @@ export class StockDetailService {
fid_org_adj_prc: '0',
};

const response = await this.requestApi<InquirePriceApiResponse>(
const response = await this.requestApi<InquirePriceChartApiResponse>(
'FHKST03010100',
'/uapi/domestic-stock/v1/quotations/inquire-daily-itemchartprice',
queryParams,
Expand All @@ -64,7 +121,7 @@ export class StockDetailService {
*
* @author uuuo3o
*/
private formatStockInquirePriceData(response: InquirePriceApiResponse) {
private formatStockInquirePriceData(response: InquirePriceChartApiResponse) {
const { output2 } = response;

return output2.map((info) => {
Expand Down

0 comments on commit 5f7d33c

Please sign in to comment.