27.1. Modify userId to be UUID instead of String (#27 refactor http calls to use userId instead of username or email)

This commit is contained in:
Art
2021-09-28 11:53:58 +03:00
parent 6a1a5301b7
commit 685fcf2136
9 changed files with 47 additions and 36 deletions

View File

@ -23,6 +23,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.validation.Valid;
import java.io.IOException;
import java.util.UUID;
import static org.springframework.http.HttpStatus.OK;
@ -93,7 +94,7 @@ public class UserResource {
@DeleteMapping("{userId}")
@PreAuthorize("hasAuthority('user:delete')")
public HttpResponse deleteUser(@PathVariable String userId) {
public HttpResponse deleteUser(@PathVariable UUID userId) {
userService.deleteUser(userId);
return HttpResponse.builder()
.httpStatusCode(OK.value())
@ -114,12 +115,12 @@ public class UserResource {
}
@GetMapping(path = "image/profile/{userId}/{filename}", produces = MediaType.IMAGE_JPEG_VALUE)
public byte[] getProfileImageByUserId(@PathVariable String userId, @PathVariable String filename) throws IOException {
public byte[] getProfileImageByUserId(@PathVariable UUID userId, @PathVariable String filename) throws IOException {
return userService.getImageByUserId(userId, filename);
}
@GetMapping(path = "image/profile/{userId}", produces = MediaType.IMAGE_JPEG_VALUE)
public byte[] getDefaultProfileImage(@PathVariable String userId) {
public byte[] getDefaultProfileImage(@PathVariable UUID userId) {
return userService.getDefaultProfileImage(userId);
}

View File

@ -2,13 +2,12 @@ package net.shyshkin.study.fullstack.supportportal.backend.domain;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import org.hibernate.annotations.Type;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.UUID;
@Entity
@ -27,7 +26,9 @@ public class User implements Serializable {
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Long id;
private String userId;
@Type(type="org.hibernate.type.UUIDCharType")
@Column(length = 36, columnDefinition = "varchar(255)", updatable = false, nullable = false )
private UUID userId;
private String firstName;
private String lastName;
private String username;

View File

@ -4,6 +4,7 @@ import net.shyshkin.study.fullstack.supportportal.backend.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
import java.util.UUID;
public interface UserRepository extends JpaRepository<User, Long> {
@ -15,6 +16,6 @@ public interface UserRepository extends JpaRepository<User, Long> {
Boolean existsByEmail(String email);
Optional<User> findByUserId(String userId);
Optional<User> findByUserId(UUID userId);
}

View File

@ -8,6 +8,7 @@ import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.UUID;
public interface UserService extends UserDetailsService {
@ -23,7 +24,7 @@ public interface UserService extends UserDetailsService {
User updateUser(String username, UserDto userDto);
void deleteUser(String userId);
void deleteUser(UUID userId);
void resetPassword(String email);
@ -31,7 +32,7 @@ public interface UserService extends UserDetailsService {
byte[] getProfileImage(String username) throws IOException;
byte[] getImageByUserId(String userId, String filename) throws IOException;
byte[] getImageByUserId(UUID userId, String filename) throws IOException;
byte[] getDefaultProfileImage(String userId);
byte[] getDefaultProfileImage(UUID userId);
}

View File

@ -102,17 +102,17 @@ public class UserServiceImpl implements UserService {
return addNewUser(newUserDto);
}
private String generateDefaultProfileImageUrl(String userId) {
private String generateDefaultProfileImageUrl(UUID userId) {
return ServletUriComponentsBuilder.fromCurrentContextPath()
.path(DEFAULT_USER_IMAGE_PATH)
.pathSegment(userId)
.pathSegment(userId.toString())
.toUriString();
}
private String generateProfileImageUrl(String userId) {
private String generateProfileImageUrl(UUID userId) {
return ServletUriComponentsBuilder.fromCurrentContextPath()
.path(DEFAULT_USER_IMAGE_PATH)
.pathSegment(userId)
.pathSegment(userId.toString())
.pathSegment(USER_IMAGE_FILENAME)
.toUriString();
}
@ -121,8 +121,8 @@ public class UserServiceImpl implements UserService {
return RandomStringUtils.randomAscii(10);
}
private String generateUserId() {
return UUID.randomUUID().toString();
private UUID generateUserId() {
return UUID.randomUUID();
}
@Override
@ -182,7 +182,7 @@ public class UserServiceImpl implements UserService {
throw new NotAnImageFileException(profileImage.getOriginalFilename() + " is not an image file. Please upload an image");
}
Path userFolder = Paths.get(USER_FOLDER, user.getUserId());
Path userFolder = Paths.get(USER_FOLDER, user.getUserId().toString());
try {
if (Files.notExists(userFolder)) {
Files.createDirectories(userFolder);
@ -200,7 +200,7 @@ public class UserServiceImpl implements UserService {
private void deleteProfileImageFolder(User user) {
Path userFolder = Paths.get(USER_FOLDER, user.getUserId());
Path userFolder = Paths.get(USER_FOLDER, user.getUserId().toString());
try {
FileSystemUtils.deleteRecursively(userFolder);
} catch (IOException exception) {
@ -232,7 +232,7 @@ public class UserServiceImpl implements UserService {
}
@Override
public void deleteUser(String userId) {
public void deleteUser(UUID userId) {
User userToBeDeleted = userRepository
.findByUserId(userId)
.orElseThrow(() -> new UserNotFoundException("User was not found"));
@ -270,14 +270,14 @@ public class UserServiceImpl implements UserService {
}
@Override
public byte[] getImageByUserId(String userId, String filename) throws IOException {
public byte[] getImageByUserId(UUID userId, String filename) throws IOException {
Path userProfileImagePath = Paths
.get(USER_FOLDER, userId, filename);
.get(USER_FOLDER, userId.toString(), filename);
return Files.readAllBytes(userProfileImagePath);
}
@Override
public byte[] getDefaultProfileImage(String userId) {
public byte[] getDefaultProfileImage(UUID userId) {
// "https://robohash.org/11951691-d373-4126-bef2-84d157a6546b"
RequestEntity<Void> requestEntity = RequestEntity
.get("/{userId}", userId)