feat: Currency symbols enum
This commit is contained in:
parent
f42dcce74b
commit
11d6e41c98
22 changed files with 248 additions and 129 deletions
|
@ -0,0 +1,9 @@
|
|||
package eu.ztsh.wymiana.config;
|
||||
|
||||
import eu.ztsh.wymiana.model.Symbol;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@ConfigurationProperties("currency")
|
||||
public record CurrencyProperties(Symbol initial) {
|
||||
|
||||
}
|
|
@ -1,6 +1,9 @@
|
|||
package eu.ztsh.wymiana.data.entity;
|
||||
|
||||
import eu.ztsh.wymiana.model.Symbol;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.EnumType;
|
||||
import jakarta.persistence.Enumerated;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
@ -19,7 +22,8 @@ public class CurrencyEntity {
|
|||
@Id
|
||||
String pesel;
|
||||
@Id
|
||||
String symbol;
|
||||
@Enumerated(EnumType.STRING)
|
||||
Symbol symbol;
|
||||
BigDecimal amount;
|
||||
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package eu.ztsh.wymiana.exception;
|
||||
|
||||
import eu.ztsh.wymiana.model.Symbol;
|
||||
|
||||
public class NoDataException extends RuntimeException {
|
||||
|
||||
public NoDataException(String code, String date) {
|
||||
public NoDataException(Symbol code, String date) {
|
||||
super("No data for code %s and date %s".formatted(code, date));
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,6 @@ package eu.ztsh.wymiana.model;
|
|||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public record Currency(String symbol, BigDecimal amount) {
|
||||
public record Currency(Symbol symbol, BigDecimal amount) {
|
||||
|
||||
}
|
||||
|
|
6
src/main/java/eu/ztsh/wymiana/model/Symbol.java
Normal file
6
src/main/java/eu/ztsh/wymiana/model/Symbol.java
Normal file
|
@ -0,0 +1,6 @@
|
|||
package eu.ztsh.wymiana.model;
|
||||
|
||||
public enum Symbol {
|
||||
PLN,
|
||||
USD
|
||||
}
|
|
@ -2,6 +2,6 @@ package eu.ztsh.wymiana.model;
|
|||
|
||||
import java.util.Map;
|
||||
|
||||
public record User(String name, String surname, String pesel, Map<String, Currency> currencies) {
|
||||
public record User(String name, String surname, String pesel, Map<Symbol, Currency> currencies) {
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
package eu.ztsh.wymiana.model;
|
||||
|
||||
import eu.ztsh.wymiana.web.model.UserCreateRequest;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
public class UserCreateRequestConfiguredWrapper {
|
||||
|
||||
private final UserCreateRequest request;
|
||||
private final Symbol initialSymbol;
|
||||
|
||||
public String name() {
|
||||
return request.name();
|
||||
}
|
||||
|
||||
public String surname() {
|
||||
return request.surname();
|
||||
}
|
||||
|
||||
public String pesel() {
|
||||
return request.pesel();
|
||||
}
|
||||
|
||||
public BigDecimal initial() {
|
||||
return request.initial();
|
||||
}
|
||||
|
||||
public Symbol initialSymbol() {
|
||||
return initialSymbol;
|
||||
}
|
||||
|
||||
private UserCreateRequestConfiguredWrapper(Builder builder) {
|
||||
this.request = builder.request;
|
||||
this.initialSymbol = builder.initial;
|
||||
}
|
||||
|
||||
public static Builder of(UserCreateRequest request) {
|
||||
return new Builder().withRequest(request);
|
||||
}
|
||||
|
||||
public static final class Builder {
|
||||
|
||||
private UserCreateRequest request;
|
||||
private Symbol initial;
|
||||
|
||||
private Builder() {
|
||||
}
|
||||
|
||||
private Builder withRequest(UserCreateRequest request) {
|
||||
this.request = request;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder withInitial(Symbol initial) {
|
||||
this.initial = initial;
|
||||
return this;
|
||||
}
|
||||
|
||||
public UserCreateRequestConfiguredWrapper build() {
|
||||
return new UserCreateRequestConfiguredWrapper(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -4,6 +4,7 @@ import eu.ztsh.wymiana.exception.ExchangeFailedException;
|
|||
import eu.ztsh.wymiana.exception.InsufficientFundsException;
|
||||
import eu.ztsh.wymiana.exception.UserNotFoundException;
|
||||
import eu.ztsh.wymiana.model.Currency;
|
||||
import eu.ztsh.wymiana.model.Symbol;
|
||||
import eu.ztsh.wymiana.model.User;
|
||||
import eu.ztsh.wymiana.validation.InstanceValidator;
|
||||
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
|
||||
|
@ -28,23 +29,17 @@ public class CurrencyService {
|
|||
public User exchange(CurrencyExchangeRequest request) {
|
||||
validator.validate(request);
|
||||
return userService.get(request.pesel()).map(user -> {
|
||||
if (!request.from().equalsIgnoreCase("PLN") && !request.to().equalsIgnoreCase("PLN")) {
|
||||
if (!request.from().equals(Symbol.PLN) && !request.to().equals(Symbol.PLN)) {
|
||||
throw new ExchangeFailedException("Either 'from' or 'to' has to be PLN");
|
||||
}
|
||||
// As we support only USD now, we need to limit second parameter too
|
||||
// Begin: unlock other currencies
|
||||
if (!request.from().equalsIgnoreCase("USD") && !request.to().equalsIgnoreCase("USD")) {
|
||||
throw new ExchangeFailedException("Either 'from' or 'to' has to be USD");
|
||||
}
|
||||
// End: unlock other currencies
|
||||
|
||||
var from = user.currencies().get(request.from().toUpperCase());
|
||||
var from = user.currencies().get(request.from());
|
||||
if (from == null) {
|
||||
// There is no currency 'from' opened so no need to check if user has funds to exchange
|
||||
throw new InsufficientFundsException();
|
||||
}
|
||||
var exchanged = performExchange(from,
|
||||
Optional.ofNullable(user.currencies().get(request.to().toUpperCase())).orElse(create(request.to())),
|
||||
Optional.ofNullable(user.currencies().get(request.to())).orElse(create(request.to())),
|
||||
Optional.ofNullable(request.toSell()).orElse(BigDecimal.ZERO),
|
||||
Optional.ofNullable(request.toBuy()).orElse(BigDecimal.ZERO));
|
||||
user.currencies().putAll(exchanged);
|
||||
|
@ -53,16 +48,15 @@ public class CurrencyService {
|
|||
.orElseThrow(() -> new UserNotFoundException(request));
|
||||
}
|
||||
|
||||
private Currency create(String symbol) {
|
||||
// TODO: check if supported - now limited to PLN <-> USD
|
||||
return new Currency(symbol.toUpperCase(), BigDecimal.ZERO);
|
||||
private Currency create(Symbol symbol) {
|
||||
return new Currency(symbol, BigDecimal.ZERO);
|
||||
}
|
||||
|
||||
private Map<String, Currency> performExchange(Currency from, Currency to, BigDecimal toSell, BigDecimal toBuy) {
|
||||
private Map<Symbol, Currency> performExchange(Currency from, Currency to, BigDecimal toSell, BigDecimal toBuy) {
|
||||
BigDecimal exchangeRate;
|
||||
BigDecimal neededFromAmount;
|
||||
BigDecimal requestedToAmount;
|
||||
if (from.symbol().equalsIgnoreCase("PLN")) {
|
||||
if (from.symbol().equals(Symbol.PLN)) {
|
||||
exchangeRate = nbpService.getSellRate(to.symbol());
|
||||
neededFromAmount = round(toBuy.signum() != 0 ? toBuy.multiply(exchangeRate) : toSell);
|
||||
requestedToAmount = round(toBuy.signum() != 0 ? toBuy : divide(toSell, exchangeRate));
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package eu.ztsh.wymiana.service;
|
||||
|
||||
import eu.ztsh.wymiana.config.CurrencyProperties;
|
||||
import eu.ztsh.wymiana.data.repository.UserRepository;
|
||||
import eu.ztsh.wymiana.exception.UserAlreadyExistsException;
|
||||
import eu.ztsh.wymiana.model.User;
|
||||
import eu.ztsh.wymiana.model.UserCreateRequestConfiguredWrapper;
|
||||
import eu.ztsh.wymiana.util.UserMapper;
|
||||
import eu.ztsh.wymiana.validation.InstanceValidator;
|
||||
import eu.ztsh.wymiana.web.model.UserCreateRequest;
|
||||
|
@ -20,13 +22,16 @@ public class UserService {
|
|||
|
||||
private final UserRepository userRepository;
|
||||
private final InstanceValidator validator;
|
||||
private final CurrencyProperties currencyProperties;
|
||||
|
||||
public User create(UserCreateRequest request) {
|
||||
validator.validate(request);
|
||||
if (userRepository.findById(request.pesel()).isPresent()) {
|
||||
throw new UserAlreadyExistsException(request);
|
||||
}
|
||||
return UserMapper.entityToPojo(userRepository.save(UserMapper.requestToEntity(request)));
|
||||
return UserMapper.entityToPojo(userRepository.save(UserMapper.requestToEntity(
|
||||
UserCreateRequestConfiguredWrapper.of(request).withInitial(currencyProperties.initial()).build()
|
||||
)));
|
||||
}
|
||||
|
||||
public Optional<User> get(@PESEL String pesel) {
|
||||
|
|
|
@ -2,6 +2,7 @@ package eu.ztsh.wymiana.util;
|
|||
|
||||
import eu.ztsh.wymiana.data.entity.CurrencyEntity;
|
||||
import eu.ztsh.wymiana.model.Currency;
|
||||
import eu.ztsh.wymiana.model.Symbol;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -17,12 +18,12 @@ public class CurrencyMapper {
|
|||
return new CurrencyEntity(pesel, pojo.symbol(), pojo.amount());
|
||||
}
|
||||
|
||||
public static Map<String, Currency> entitiesToPojoMap(List<CurrencyEntity> values) {
|
||||
public static Map<Symbol, Currency> entitiesToPojoMap(List<CurrencyEntity> values) {
|
||||
return values.stream().map(CurrencyMapper::entityToPojo)
|
||||
.collect(Collectors.toMap(Currency::symbol, pojo -> pojo));
|
||||
}
|
||||
|
||||
public static List<CurrencyEntity> pojoMapToEntities(Map<String, Currency> currencies, String pesel) {
|
||||
public static List<CurrencyEntity> pojoMapToEntities(Map<Symbol, Currency> currencies, String pesel) {
|
||||
return currencies.values().stream().map(entry -> pojoToEntity(entry, pesel)).toList();
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ package eu.ztsh.wymiana.util;
|
|||
import eu.ztsh.wymiana.data.entity.CurrencyEntity;
|
||||
import eu.ztsh.wymiana.data.entity.UserEntity;
|
||||
import eu.ztsh.wymiana.model.User;
|
||||
import eu.ztsh.wymiana.web.model.UserCreateRequest;
|
||||
import eu.ztsh.wymiana.model.UserCreateRequestConfiguredWrapper;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -19,9 +19,9 @@ public class UserMapper {
|
|||
CurrencyMapper.pojoMapToEntities(pojo.currencies(), pojo.pesel()));
|
||||
}
|
||||
|
||||
public static UserEntity requestToEntity(UserCreateRequest request) {
|
||||
public static UserEntity requestToEntity(UserCreateRequestConfiguredWrapper request) {
|
||||
return new UserEntity(request.pesel(), request.name(), request.surname(),
|
||||
List.of(new CurrencyEntity(request.pesel(), "PLN", request.initial())));
|
||||
List.of(new CurrencyEntity(request.pesel(), request.initialSymbol(), request.initial())));
|
||||
}
|
||||
|
||||
private UserMapper() {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package eu.ztsh.wymiana.web.model;
|
||||
|
||||
import eu.ztsh.wymiana.model.Symbol;
|
||||
import eu.ztsh.wymiana.validation.ValidExchangeRequest;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Builder;
|
||||
|
@ -11,8 +12,8 @@ import java.math.BigDecimal;
|
|||
@ValidExchangeRequest
|
||||
public record CurrencyExchangeRequest(
|
||||
@PESEL String pesel,
|
||||
@NotNull String from,
|
||||
@NotNull String to,
|
||||
@NotNull Symbol from,
|
||||
@NotNull Symbol to,
|
||||
BigDecimal toBuy,
|
||||
BigDecimal toSell
|
||||
) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue