package com.sb.web.account.controller; import com.sb.web.account.service.FxService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.math.BigDecimal; import java.time.Instant; import java.util.HashMap; import java.util.Map; /** * 환율 조회 API. 외화 결제 입력 시 통화→원화 환율 자동 채움용. * GET /api/fx/rate?from=USD&to=KRW → { from, to, rate } */ @RestController @RequestMapping("/api/fx") public class FxController { private final FxService fxService; public FxController(FxService fxService) { this.fxService = fxService; } @GetMapping("/rate") public Map rate(@RequestParam String from, @RequestParam(defaultValue = "KRW") String to) { BigDecimal rate = fxService.getRate(from, to); Instant updatedAt = fxService.getCachedAt(from); Map res = new HashMap<>(); res.put("from", from == null ? null : from.toUpperCase()); res.put("to", to == null ? null : to.toUpperCase()); res.put("rate", rate); // 조회 실패 시 null res.put("updatedAt", updatedAt != null ? updatedAt.toString() : null); return res; } }