72.2 User service implementation - MapStruct UserMapper (#8)
This commit is contained in:
@ -15,6 +15,9 @@
|
||||
<description>Demo project for Spring Boot</description>
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
|
||||
<mapstruct.version>1.4.2.Final</mapstruct.version>
|
||||
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@ -53,7 +56,11 @@
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct</artifactId>
|
||||
<version>${mapstruct.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
@ -112,6 +119,37 @@
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok-mapstruct-binding</artifactId>
|
||||
<version>0.2.0</version>
|
||||
</path>
|
||||
<path>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct-processor</artifactId>
|
||||
<version>${mapstruct.version}</version>
|
||||
</path>
|
||||
<path>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
</path>
|
||||
|
||||
</annotationProcessorPaths>
|
||||
<compilerArgs>
|
||||
<compilerArg>
|
||||
-Amapstruct.defaultComponentModel=spring
|
||||
</compilerArg>
|
||||
</compilerArgs>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
@ -0,0 +1,18 @@
|
||||
package net.shyshkin.study.fullstack.supportportal.backend.mapper;
|
||||
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.domain.User;
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.domain.dto.UserDto;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Mapper(imports = {LocalDateTime.class})
|
||||
public interface UserMapper {
|
||||
|
||||
@Mapping(target = "isNotLocked", source = "nonLocked")
|
||||
@Mapping(target = "isActive", source = "active")
|
||||
@Mapping(target = "joinDate", expression = "java( LocalDateTime.now() )")
|
||||
User toEntity(UserDto userDto);
|
||||
|
||||
}
|
||||
@ -11,6 +11,7 @@ import net.shyshkin.study.fullstack.supportportal.backend.exception.domain.Email
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.exception.domain.EmailNotFoundException;
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.exception.domain.UserNotFoundException;
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.exception.domain.UsernameExistsException;
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.mapper.UserMapper;
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.repository.UserRepository;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
@ -40,6 +41,7 @@ public class UserServiceImpl implements UserService {
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
private final LoginAttemptService loginAttemptService;
|
||||
private final EmailService emailService;
|
||||
private final UserMapper userMapper;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
@ -126,22 +128,14 @@ public class UserServiceImpl implements UserService {
|
||||
|
||||
Role role = Role.valueOf(userDto.getRole());
|
||||
|
||||
User newUser = User.builder()
|
||||
.email(email)
|
||||
.firstName(userDto.getFirstName())
|
||||
.lastName(userDto.getLastName())
|
||||
.username(username)
|
||||
.password(encodedPassword)
|
||||
.userId(generateUserId())
|
||||
.isActive(userDto.isActive())
|
||||
.isNotLocked(userDto.isNonLocked())
|
||||
.joinDate(LocalDateTime.now())
|
||||
.profileImageUrl(getTemporaryProfileImageUrl(username))
|
||||
.lastLoginDate(null)
|
||||
.lastLoginDateDisplay(null)
|
||||
.role(role.name())
|
||||
.authorities(role.getAuthorities())
|
||||
.build();
|
||||
User newUser = userMapper.toEntity(userDto);
|
||||
|
||||
newUser.setPassword(encodedPassword);
|
||||
newUser.setUserId(generateUserId());
|
||||
newUser.setProfileImageUrl(getTemporaryProfileImageUrl(username));
|
||||
newUser.setRole(role.name());
|
||||
newUser.setAuthorities(role.getAuthorities());
|
||||
|
||||
userRepository.save(newUser);
|
||||
saveProfileImage(newUser, userDto.getProfileImage());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user