fix: Fixed user persistence after exchange

This commit is contained in:
Piotr Dec 2024-05-24 17:44:51 +02:00
parent 71109174f7
commit 94dd43c138
Signed by: stawros
GPG key ID: F89F27AD8F881A91
6 changed files with 34 additions and 7 deletions

View file

@ -31,7 +31,7 @@ public class UserService {
}
public User update(User user) {
throw new UnsupportedOperationException("Not implemented yet");
return UserMapper.entityToPojo(userRepository.save(UserMapper.pojoToEntity(user)));
}
}

View file

@ -13,11 +13,19 @@ public class CurrencyMapper {
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) {
return values.stream().map(CurrencyMapper::entityToPojo)
.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() {
}

View file

@ -14,6 +14,11 @@ public class UserMapper {
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) {
return new UserEntity(request.pesel(), request.name(), request.surname(),
List.of(new CurrencyEntity(request.pesel(), "PLN", request.pln())));