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() {
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package eu.ztsh.wymiana;
|
|||
|
||||
import eu.ztsh.wymiana.data.entity.CurrencyEntity;
|
||||
import eu.ztsh.wymiana.data.entity.UserEntity;
|
||||
import eu.ztsh.wymiana.web.model.UserCreateRequest;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -22,6 +23,10 @@ public class EntityCreator {
|
|||
return new UserEntityBuilder();
|
||||
}
|
||||
|
||||
public static UserCreateRequest userRequest() {
|
||||
return new UserCreateRequest(Constants.NAME, Constants.SURNAME, Constants.PESEL, Constants.PLN);
|
||||
}
|
||||
|
||||
public static class UserEntityBuilder {
|
||||
|
||||
String name;
|
||||
|
|
|
@ -6,6 +6,7 @@ import eu.ztsh.wymiana.data.repository.UserRepository;
|
|||
import eu.ztsh.wymiana.exception.UserAlreadyExistsException;
|
||||
import eu.ztsh.wymiana.util.UserMapper;
|
||||
import eu.ztsh.wymiana.web.model.UserCreateRequest;
|
||||
import jakarta.transaction.Transactional;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
|
@ -23,16 +24,17 @@ class UserServiceTest extends RepositoryBasedTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
void createNewUserTest() {
|
||||
userService.create(createRequest());
|
||||
userService.create(EntityCreator.userRequest());
|
||||
var entity = EntityCreator.user().build();
|
||||
expect(entity);
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDuplicatedUser() {
|
||||
var first = createRequest();
|
||||
var second = createRequest();
|
||||
var first = EntityCreator.userRequest();
|
||||
var second = EntityCreator.userRequest();
|
||||
userService.create(first);
|
||||
assertThatThrownBy(() -> userService.create(second))
|
||||
.isInstanceOf(UserAlreadyExistsException.class)
|
||||
|
@ -40,6 +42,7 @@ class UserServiceTest extends RepositoryBasedTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
void getExistingUserTest() {
|
||||
var entity = EntityCreator.user().build();
|
||||
userRepository.save(entity);
|
||||
|
@ -58,9 +61,4 @@ class UserServiceTest extends RepositoryBasedTest {
|
|||
assertThat(userOptional).isEmpty();
|
||||
}
|
||||
|
||||
private UserCreateRequest createRequest() {
|
||||
return new UserCreateRequest(EntityCreator.Constants.NAME, EntityCreator.Constants.SURNAME,
|
||||
EntityCreator.Constants.PESEL, 20.10);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -25,4 +25,13 @@ class UserMapperTest {
|
|||
.isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestToEntityTest() {
|
||||
var request = EntityCreator.userRequest();
|
||||
var expected = EntityCreator.user().build();
|
||||
assertThat(UserMapper.requestToEntity(request))
|
||||
.usingRecursiveComparison()
|
||||
.isEqualTo(expected);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue