51. Code clean up (#4)

This commit is contained in:
Art
2021-09-07 15:27:17 +03:00
parent c4138f9ad3
commit 02de28d4e5
2 changed files with 12 additions and 6 deletions

View File

@ -28,6 +28,12 @@ import java.util.UUID;
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {
public static final String DEFAULT_USER_IMG_PATH = "/user/image/profile/temp";
public static final String USERNAME_NOT_FOUND_MSG = "User with username `%s` not found";
public static final String USERNAME_EXISTS_MSG = "Username `%s` is already taken. Please select another one";
public static final String EMAIL_NOT_FOUND_MSG = "User with email `%s` not found";
public static final String EMAIL_EXISTS_MSG = "User with email `%s` is already registered";
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
@ -36,7 +42,7 @@ public class UserServiceImpl implements UserService {
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository
.findByUsername(username)
.orElseThrow(() -> new UsernameNotFoundException("User with username `" + username + "` not found"));
.orElseThrow(() -> new UserNotFoundException(String.format(USERNAME_NOT_FOUND_MSG, username)));
user.setLastLoginDateDisplay(user.getLastLoginDate());
user.setLastLoginDate(LocalDateTime.now());
return new UserPrincipal(user);
@ -73,7 +79,7 @@ public class UserServiceImpl implements UserService {
}
private String getTemporaryProfileImageUrl() {
return ServletUriComponentsBuilder.fromCurrentContextPath().path("/user/image/profile/temp").build().toString();
return ServletUriComponentsBuilder.fromCurrentContextPath().path(DEFAULT_USER_IMG_PATH).build().toString();
}
private String generatePassword() {
@ -93,14 +99,14 @@ public class UserServiceImpl implements UserService {
public User findByUsername(String username) {
return userRepository
.findByUsername(username)
.orElseThrow(() -> new UserNotFoundException("User with username `" + username + "` not found"));
.orElseThrow(() -> new UserNotFoundException(String.format(USERNAME_NOT_FOUND_MSG, username)));
}
@Override
public User findByEmail(String email) {
return userRepository
.findByEmail(email)
.orElseThrow(() -> new EmailNotFoundException("User with email `" + email + "` not found"));
.orElseThrow(() -> new EmailNotFoundException(String.format(EMAIL_NOT_FOUND_MSG, email)));
}
private void validateNewUsernameAndEmail(String username, String email) {

View File

@ -3,11 +3,11 @@ package net.shyshkin.study.fullstack.supportportal.backend.service;
import lombok.extern.slf4j.Slf4j;
import net.shyshkin.study.fullstack.supportportal.backend.common.BaseUserTest;
import net.shyshkin.study.fullstack.supportportal.backend.domain.User;
import net.shyshkin.study.fullstack.supportportal.backend.exception.domain.UserNotFoundException;
import org.assertj.core.api.ThrowableAssert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import java.time.temporal.ChronoUnit;
import java.util.Optional;
@ -58,7 +58,7 @@ class UserServiceTest extends BaseUserTest {
//then
assertThatThrownBy(execution)
.isInstanceOf(UsernameNotFoundException.class)
.isInstanceOf(UserNotFoundException.class)
.hasMessage("User with username `" + username + "` not found");
}
}