diff --git a/BE/src/stock/detail/stock-detail.controller.ts b/BE/src/stock/detail/stock-detail.controller.ts index 86149af2..533b67ca 100644 --- a/BE/src/stock/detail/stock-detail.controller.ts +++ b/BE/src/stock/detail/stock-detail.controller.ts @@ -1,6 +1,7 @@ -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'; @@ -8,6 +9,24 @@ import { InquirePriceChartResponseDto } from './dto/stock-detail-chart-response. 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({ @@ -30,7 +49,7 @@ export class StockDetailController { description: '국내주식기간별시세(일/주/월/년) 조회 성공', type: InquirePriceChartResponseDto, }) - getStockDetail( + getStockDetailChart( @Param('stockCode') stockCode: string, @Body() body: StockDetailChartRequestDto, ) { diff --git a/BE/src/stock/detail/stock-detail.service.ts b/BE/src/stock/detail/stock-detail.service.ts index e66b0736..8031c4d6 100644 --- a/BE/src/stock/detail/stock-detail.service.ts +++ b/BE/src/stock/detail/stock-detail.service.ts @@ -3,8 +3,13 @@ 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 { @@ -12,6 +17,58 @@ export class StockDetailService { 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( + '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 - 종목코드 @@ -38,7 +95,7 @@ export class StockDetailService { fid_org_adj_prc: '0', }; - const response = await this.requestApi( + const response = await this.requestApi( 'FHKST03010100', '/uapi/domestic-stock/v1/quotations/inquire-daily-itemchartprice', queryParams, @@ -64,7 +121,7 @@ export class StockDetailService { * * @author uuuo3o */ - private formatStockInquirePriceData(response: InquirePriceApiResponse) { + private formatStockInquirePriceData(response: InquirePriceChartApiResponse) { const { output2 } = response; return output2.map((info) => {