72.2 User service implementation - MapStruct UserMapper (#8)

This commit is contained in:
Art
2021-09-09 14:43:03 +03:00
parent 5688eb6b23
commit 8fd99f43ce
3 changed files with 67 additions and 17 deletions

View File

@ -15,6 +15,9 @@
<description>Demo project for Spring Boot</description> <description>Demo project for Spring Boot</description>
<properties> <properties>
<java.version>11</java.version> <java.version>11</java.version>
<mapstruct.version>1.4.2.Final</mapstruct.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
@ -53,7 +56,11 @@
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
@ -112,6 +119,37 @@
</excludes> </excludes>
</configuration> </configuration>
</plugin> </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> </plugins>
</build> </build>

View File

@ -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);
}

View File

@ -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.EmailNotFoundException;
import net.shyshkin.study.fullstack.supportportal.backend.exception.domain.UserNotFoundException; 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.exception.domain.UsernameExistsException;
import net.shyshkin.study.fullstack.supportportal.backend.mapper.UserMapper;
import net.shyshkin.study.fullstack.supportportal.backend.repository.UserRepository; import net.shyshkin.study.fullstack.supportportal.backend.repository.UserRepository;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
@ -40,6 +41,7 @@ public class UserServiceImpl implements UserService {
private final PasswordEncoder passwordEncoder; private final PasswordEncoder passwordEncoder;
private final LoginAttemptService loginAttemptService; private final LoginAttemptService loginAttemptService;
private final EmailService emailService; private final EmailService emailService;
private final UserMapper userMapper;
@Override @Override
@Transactional @Transactional
@ -126,22 +128,14 @@ public class UserServiceImpl implements UserService {
Role role = Role.valueOf(userDto.getRole()); Role role = Role.valueOf(userDto.getRole());
User newUser = User.builder() User newUser = userMapper.toEntity(userDto);
.email(email)
.firstName(userDto.getFirstName()) newUser.setPassword(encodedPassword);
.lastName(userDto.getLastName()) newUser.setUserId(generateUserId());
.username(username) newUser.setProfileImageUrl(getTemporaryProfileImageUrl(username));
.password(encodedPassword) newUser.setRole(role.name());
.userId(generateUserId()) newUser.setAuthorities(role.getAuthorities());
.isActive(userDto.isActive())
.isNotLocked(userDto.isNonLocked())
.joinDate(LocalDateTime.now())
.profileImageUrl(getTemporaryProfileImageUrl(username))
.lastLoginDate(null)
.lastLoginDateDisplay(null)
.role(role.name())
.authorities(role.getAuthorities())
.build();
userRepository.save(newUser); userRepository.save(newUser);
saveProfileImage(newUser, userDto.getProfileImage()); saveProfileImage(newUser, userDto.getProfileImage());