11package com .synergyx .trading .service .stockService .search ;
22
3- // import com.synergyx.trading.dto.stockSearch.StockSearchFastApiWrapperDTO;
3+ import com .synergyx .trading .dto .stockSearch .StockSearchFastApiWrapperDTO ;
44import com .synergyx .trading .apiPayload .code .status .ErrorStatus ;
55import com .synergyx .trading .apiPayload .exception .GeneralException ;
66import com .synergyx .trading .dto .stockSearch .StockSearchResponseDTO ;
7+ import com .synergyx .trading .model .Stock ;
78import com .synergyx .trading .repository .StockRepository ;
89import lombok .RequiredArgsConstructor ;
9- // import org.springframework.http.MediaType;
10- // import org.springframework.http.client.MultipartBodyBuilder;
10+ import org .springframework .http .MediaType ;
11+ import org .springframework .http .client .MultipartBodyBuilder ;
1112import org .springframework .stereotype .Service ;
1213import org .springframework .web .multipart .MultipartFile ;
13- // import org.springframework.web.reactive.function.client.WebClient;
14+ import org .springframework .web .reactive .function .client .WebClient ;
1415
1516import java .util .List ;
1617
1920public class StockSearchQueryServiceImpl implements StockSearchQueryService {
2021
2122 private final StockRepository stockRepository ;
22- // private final WebClient fastApiWebClient;
23+ private final WebClient fastApiWebClient ;
2324
2425 /**
2526 * 종목명을 기준으로 종목 리스트를 검색합니다.
@@ -62,35 +63,34 @@ public StockSearchResponseDTO searchStockByImage(MultipartFile image) {
6263 throw new GeneralException (ErrorStatus .INVALID_IMAGE_FILE_TYPE );
6364 }
6465
65- // TODO: FastAPI 연결 후 주석 삭제
66- // try {
67- // MultipartBodyBuilder builder = new MultipartBodyBuilder();
68- // builder.part("image", image.getResource());
69- //
70- // StockSearchFastApiWrapperDTO response = fastApiWebClient.post()
71- // .uri("/image-search")
72- // .contentType(MediaType.MULTIPART_FORM_DATA)
73- // .bodyValue(builder.build())
74- // .retrieve()
75- // .bodyToMono(StockSearchFastApiWrapperDTO.class)
76- // .block();
77- //
78- // if (response == null || !response.isSuccess()) {
79- // throw new GeneralException(ErrorStatus.IMAGE_STOCK_NOT_FOUND);
80- // }
81- //
82- // return response.getData();
83- //
84- // } catch (Exception e) {
85- // throw new GeneralException(ErrorStatus.IMAGE_STOCK_NOT_FOUND);
86- // }
87- // }
88- // TODO: FastAPI 연결 후 삭제
89- // 목데이터
90- return StockSearchResponseDTO .builder ()
91- .id (1L )
92- .name ("삼성전자" )
93- .imageUrl ("https://picsum.photos/30/30" )
94- .build ();
66+ try {
67+ MultipartBodyBuilder builder = new MultipartBodyBuilder ();
68+ builder .part ("image" , image .getResource ());
69+
70+ StockSearchFastApiWrapperDTO response = fastApiWebClient .post ()
71+ .uri ("/api/v1/stocks/search-by-image" )
72+ .contentType (MediaType .MULTIPART_FORM_DATA )
73+ .bodyValue (builder .build ())
74+ .retrieve ()
75+ .bodyToMono (StockSearchFastApiWrapperDTO .class )
76+ .block ();
77+
78+ if (response == null || !response .isSuccess ()) {
79+ throw new GeneralException (ErrorStatus .IMAGE_STOCK_NOT_FOUND );
80+ }
81+
82+ Long stockId = response .getData ().getId ();
83+ Stock stock = stockRepository .findById (stockId )
84+ .orElseThrow (() -> new GeneralException (ErrorStatus .STOCK_NOT_FOUND ));
85+
86+ return StockSearchResponseDTO .builder ()
87+ .id (stock .getId ())
88+ .name (stock .getName ())
89+ .imageUrl (stock .getImageUrl ())
90+ .build ();
91+
92+ } catch (Exception e ) {
93+ throw new GeneralException (ErrorStatus .IMAGE_STOCK_NOT_FOUND );
94+ }
9595 }
9696}
0 commit comments