feat: Currency symbols enum

This commit is contained in:
Piotr Dec 2024-06-25 00:24:18 +02:00
parent f42dcce74b
commit 11d6e41c98
Signed by: stawros
GPG key ID: F89F27AD8F881A91
22 changed files with 248 additions and 129 deletions

View file

@ -2,6 +2,7 @@ package eu.ztsh.wymiana.service;
import eu.ztsh.wymiana.exception.NoDataException;
import eu.ztsh.wymiana.model.Rates;
import eu.ztsh.wymiana.model.Symbol;
import lombok.RequiredArgsConstructor;
import org.assertj.core.util.VisibleForTesting;
import org.springframework.http.HttpStatusCode;
@ -29,17 +30,17 @@ public class NbpService {
private static final String URI_PATTERN = "/api/exchangerates/rates/c/{code}/{date}/";
private final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private final ConcurrentMap<String, RatesCache> cache = new ConcurrentHashMap<>(1);
private final ConcurrentMap<Symbol, RatesCache> cache = new ConcurrentHashMap<>(1);
public BigDecimal getSellRate(String currency) {
return getCurrency(currency.toUpperCase()).sell();
public BigDecimal getSellRate(Symbol currency) {
return getCurrency(currency).sell();
}
public BigDecimal getBuyRate(String currency) {
return getCurrency(currency.toUpperCase()).buy();
public BigDecimal getBuyRate(Symbol currency) {
return getCurrency(currency).buy();
}
private synchronized RatesCache getCurrency(String currency) {
private synchronized RatesCache getCurrency(Symbol currency) {
var today = getFetchDate();
var cacheObject = cache.get(currency);
if (cacheObject == null || cacheObject.date().isBefore(today)) {
@ -50,7 +51,7 @@ public class NbpService {
rate.getBid(),
rate.getAsk()
);
cache.put(fresh.getCode(), cacheObject);
cache.put(Symbol.valueOf(fresh.getCode().toUpperCase()), cacheObject);
}
return cacheObject;
}
@ -69,8 +70,8 @@ public class NbpService {
}
@VisibleForTesting
Rates fetchData(String code, String date) {
return restClient.get().uri(URI_PATTERN, code.toLowerCase(), date)
Rates fetchData(Symbol code, String date) {
return restClient.get().uri(URI_PATTERN, code.name().toLowerCase(), date)
.retrieve()
.onStatus(HttpStatusCode::is4xxClientError, ((request, response) -> {
throw new NoDataException(code, date);