feat: UserService implementation
This commit is contained in:
parent
c7ef32e28d
commit
9d6ba747a8
5 changed files with 36 additions and 11 deletions
|
@ -1,7 +1,9 @@
|
|||
package eu.ztsh.wymiana.service;
|
||||
|
||||
import eu.ztsh.wymiana.data.repository.UserRepository;
|
||||
import eu.ztsh.wymiana.exception.UserAlreadyExistsException;
|
||||
import eu.ztsh.wymiana.model.User;
|
||||
import eu.ztsh.wymiana.util.UserMapper;
|
||||
import eu.ztsh.wymiana.web.model.UserCreateRequest;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
@ -15,13 +17,15 @@ public class UserService {
|
|||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
|
||||
public User create(@Valid UserCreateRequest request) {
|
||||
throw new IllegalStateException("Not impplemented yet");
|
||||
if (userRepository.findById(request.pesel()).isPresent()) {
|
||||
throw new UserAlreadyExistsException(request);
|
||||
}
|
||||
return UserMapper.entityToPojo(userRepository.save(UserMapper.requestToEntity(request)));
|
||||
}
|
||||
|
||||
public Optional<User> get(String pesel) {
|
||||
throw new IllegalStateException("Not impplemented yet");
|
||||
return userRepository.findById(pesel).map(UserMapper::entityToPojo);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
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 java.util.List;
|
||||
|
||||
public class UserMapper {
|
||||
|
||||
|
@ -10,6 +14,11 @@ public class UserMapper {
|
|||
CurrencyMapper.entitiesToPojoMap(entity.getCurrencies()));
|
||||
}
|
||||
|
||||
public static UserEntity requestToEntity(UserCreateRequest request) {
|
||||
return new UserEntity(request.pesel(), request.name(), request.surname(),
|
||||
List.of(new CurrencyEntity(request.pesel(), "PLN", request.pln())));
|
||||
}
|
||||
|
||||
private UserMapper() {
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue