fix: Fixed user persistence after exchange
This commit is contained in:
parent
71109174f7
commit
94dd43c138
6 changed files with 34 additions and 7 deletions
|
@ -31,7 +31,7 @@ public class UserService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public User update(User user) {
|
public User update(User user) {
|
||||||
throw new UnsupportedOperationException("Not implemented yet");
|
return UserMapper.entityToPojo(userRepository.save(UserMapper.pojoToEntity(user)));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,11 +13,19 @@ public class CurrencyMapper {
|
||||||
return new Currency(entity.getSymbol(), entity.getAmount());
|
return new Currency(entity.getSymbol(), entity.getAmount());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static CurrencyEntity pojoToEntity(Currency pojo, String pesel) {
|
||||||
|
return new CurrencyEntity(pesel, pojo.symbol(), pojo.amount());
|
||||||
|
}
|
||||||
|
|
||||||
public static Map<String, Currency> entitiesToPojoMap(List<CurrencyEntity> values) {
|
public static Map<String, Currency> entitiesToPojoMap(List<CurrencyEntity> values) {
|
||||||
return values.stream().map(CurrencyMapper::entityToPojo)
|
return values.stream().map(CurrencyMapper::entityToPojo)
|
||||||
.collect(Collectors.toMap(Currency::symbol, pojo -> pojo));
|
.collect(Collectors.toMap(Currency::symbol, pojo -> pojo));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<CurrencyEntity> pojoMapToEntities(Map<String, Currency> currencies, String pesel) {
|
||||||
|
return currencies.values().stream().map(entry -> pojoToEntity(entry, pesel)).toList();
|
||||||
|
}
|
||||||
|
|
||||||
private CurrencyMapper() {
|
private CurrencyMapper() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,11 @@ public class UserMapper {
|
||||||
CurrencyMapper.entitiesToPojoMap(entity.getCurrencies()));
|
CurrencyMapper.entitiesToPojoMap(entity.getCurrencies()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static UserEntity pojoToEntity(User pojo) {
|
||||||
|
return new UserEntity(pojo.pesel(), pojo.name(), pojo.surname(),
|
||||||
|
CurrencyMapper.pojoMapToEntities(pojo.currencies(), pojo.pesel()));
|
||||||
|
}
|
||||||
|
|
||||||
public static UserEntity requestToEntity(UserCreateRequest request) {
|
public static UserEntity requestToEntity(UserCreateRequest request) {
|
||||||
return new UserEntity(request.pesel(), request.name(), request.surname(),
|
return new UserEntity(request.pesel(), request.name(), request.surname(),
|
||||||
List.of(new CurrencyEntity(request.pesel(), "PLN", request.pln())));
|
List.of(new CurrencyEntity(request.pesel(), "PLN", request.pln())));
|
||||||
|
|
|
@ -62,8 +62,8 @@ public class EntityCreator {
|
||||||
String name;
|
String name;
|
||||||
String surname;
|
String surname;
|
||||||
String pesel;
|
String pesel;
|
||||||
double pln;
|
double pln = -1;
|
||||||
double usd;
|
double usd = -1;
|
||||||
|
|
||||||
public UserEntityBuilder name(String name) {
|
public UserEntityBuilder name(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
@ -93,10 +93,10 @@ public class EntityCreator {
|
||||||
public UserEntity build() {
|
public UserEntity build() {
|
||||||
var nonnulPesel = Optional.ofNullable(pesel).orElse(Constants.PESEL);
|
var nonnulPesel = Optional.ofNullable(pesel).orElse(Constants.PESEL);
|
||||||
List<CurrencyEntity> currencies = new ArrayList<>();
|
List<CurrencyEntity> currencies = new ArrayList<>();
|
||||||
if (pln > 0) {
|
if (pln > -1) {
|
||||||
currencies.add(new CurrencyEntity(nonnulPesel, "PLN", pln));
|
currencies.add(new CurrencyEntity(nonnulPesel, "PLN", pln));
|
||||||
}
|
}
|
||||||
if (usd > 0) {
|
if (usd > -1) {
|
||||||
currencies.add(new CurrencyEntity(nonnulPesel, "USD", usd));
|
currencies.add(new CurrencyEntity(nonnulPesel, "USD", usd));
|
||||||
}
|
}
|
||||||
if (currencies.isEmpty()) {
|
if (currencies.isEmpty()) {
|
||||||
|
|
|
@ -69,7 +69,7 @@ class CurrencyServiceTest extends RepositoryBasedTest {
|
||||||
@Transactional
|
@Transactional
|
||||||
@Test
|
@Test
|
||||||
void usdToPlnToSellSuccessTest() {
|
void usdToPlnToSellSuccessTest() {
|
||||||
var entity = EntityCreator.user().pln(0).usd(USD_SELL).build();
|
var entity = EntityCreator.user().pln(-1).usd(USD_SELL).build();
|
||||||
userRepository.save(entity);
|
userRepository.save(entity);
|
||||||
var result = currencyService.exchange(EntityCreator.exchangeRequest()
|
var result = currencyService.exchange(EntityCreator.exchangeRequest()
|
||||||
.from(USD_SYMBOL)
|
.from(USD_SYMBOL)
|
||||||
|
@ -85,7 +85,7 @@ class CurrencyServiceTest extends RepositoryBasedTest {
|
||||||
@Transactional
|
@Transactional
|
||||||
@Test
|
@Test
|
||||||
void usdToPlnToBuySuccessTest() {
|
void usdToPlnToBuySuccessTest() {
|
||||||
var entity = EntityCreator.user().pln(0).usd(USD_SELL).build();
|
var entity = EntityCreator.user().pln(-1).usd(USD_SELL).build();
|
||||||
userRepository.save(entity);
|
userRepository.save(entity);
|
||||||
var result = currencyService.exchange(EntityCreator.exchangeRequest()
|
var result = currencyService.exchange(EntityCreator.exchangeRequest()
|
||||||
.from(USD_SYMBOL)
|
.from(USD_SYMBOL)
|
||||||
|
|
|
@ -25,6 +25,20 @@ class UserMapperTest {
|
||||||
.isEqualTo(expected);
|
.isEqualTo(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void pojoToEntityTest() {
|
||||||
|
var entity = new User(
|
||||||
|
EntityCreator.Constants.NAME,
|
||||||
|
EntityCreator.Constants.SURNAME,
|
||||||
|
EntityCreator.Constants.PESEL,
|
||||||
|
Map.of("PLN", new Currency("PLN", EntityCreator.Constants.PLN))
|
||||||
|
);
|
||||||
|
var expected = EntityCreator.user().build();
|
||||||
|
assertThat(UserMapper.pojoToEntity(entity))
|
||||||
|
.usingRecursiveComparison()
|
||||||
|
.isEqualTo(expected);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void requestToEntityTest() {
|
void requestToEntityTest() {
|
||||||
var request = EntityCreator.userRequest().build();
|
var request = EntityCreator.userRequest().build();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue