diff --git a/src/main/java/me/snaptime/album/service/impl/AlbumServiceImpl.java b/src/main/java/me/snaptime/album/service/impl/AlbumServiceImpl.java index d97aed97..bbc2fb0d 100644 --- a/src/main/java/me/snaptime/album/service/impl/AlbumServiceImpl.java +++ b/src/main/java/me/snaptime/album/service/impl/AlbumServiceImpl.java @@ -43,7 +43,8 @@ public List findAllAlbumsByLoginId(String uid) { User foundUser = userRepository.findByLoginId(uid).orElseThrow(() -> new CustomException(ExceptionCode.USER_NOT_EXIST)); List foundAlbums = albumRepository.findAlbumsByUser(foundUser); return foundAlbums.stream().map(album -> { - Optional firstSnapOptional = album.getSnap().isEmpty() ? Optional.empty() : Optional.of(album.getSnap().get(0)); + int lastNumber = album.getSnap().size(); + Optional firstSnapOptional = album.getSnap().isEmpty() ? Optional.empty() : Optional.of(album.getSnap().get(lastNumber-1)); String photoUrl = firstSnapOptional.map(snap -> urlComponent.makePhotoURL(snap.getFileName(), snap.isPrivate())).orElse(null); return FindAllAlbumsResDto.builder() .id(album.getId()) diff --git a/src/main/java/me/snaptime/profilePhoto/service/impl/ProfilePhotoServiceImpl.java b/src/main/java/me/snaptime/profilePhoto/service/impl/ProfilePhotoServiceImpl.java index 6cc6ef52..5bccb795 100644 --- a/src/main/java/me/snaptime/profilePhoto/service/impl/ProfilePhotoServiceImpl.java +++ b/src/main/java/me/snaptime/profilePhoto/service/impl/ProfilePhotoServiceImpl.java @@ -10,7 +10,7 @@ import me.snaptime.profilePhoto.service.ProfilePhotoService; import me.snaptime.user.domain.User; import me.snaptime.user.repository.UserRepository; -import me.snaptime.util.ProfilePhotoNameGenerator; +import me.snaptime.util.FileNameGenerator; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -76,7 +76,7 @@ public ProfilePhotoResDto updatePhotoFromFileSystem(String loginId, MultipartFil User updateUser = userRepository.findByLoginId(loginId).orElseThrow(()-> new CustomException(ExceptionCode.USER_NOT_EXIST)); ProfilePhoto profilePhoto = profilePhotoRepository.findById(updateUser.getProfilePhoto().getProfilePhotoId()).orElseThrow(()-> new CustomException(ExceptionCode.PROFILE_PHOTO_NOT_FOUND)); - String updateFileName = ProfilePhotoNameGenerator.generatorProfilePhotoName(updateFile.getOriginalFilename()); + String updateFileName = FileNameGenerator.generatorName(updateFile.getOriginalFilename()); String updateFilePath = FOLDER_PATH + updateFileName; try{ diff --git a/src/main/java/me/snaptime/util/FileNameGenerator.java b/src/main/java/me/snaptime/util/FileNameGenerator.java index 86738e96..b7028d53 100644 --- a/src/main/java/me/snaptime/util/FileNameGenerator.java +++ b/src/main/java/me/snaptime/util/FileNameGenerator.java @@ -6,9 +6,9 @@ public class FileNameGenerator { - static String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("MMddHHmmssSSS")); public static String generatorName(String fileName) { + String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("MMddHHmmssSSS")); Random rd = new Random(); int nonNegativeInt = Math.abs(rd.nextInt()); // 음수를 방지하기 위해 절대값을 취합니다. return currentTime + "_" + nonNegativeInt + "_" + fileName; diff --git a/src/main/java/me/snaptime/util/ProfilePhotoNameGenerator.java b/src/main/java/me/snaptime/util/ProfilePhotoNameGenerator.java deleted file mode 100644 index 1519672e..00000000 --- a/src/main/java/me/snaptime/util/ProfilePhotoNameGenerator.java +++ /dev/null @@ -1,12 +0,0 @@ -package me.snaptime.util; - -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; - -public class ProfilePhotoNameGenerator { - static String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("MMddHHmmssSSS")); - - public static String generatorProfilePhotoName(String fileName) { - return currentTime + fileName; - } -} diff --git a/src/test/java/me/snaptime/user/service/ProfilePhotoServiceTest.java b/src/test/java/me/snaptime/user/service/ProfilePhotoServiceTest.java index 05623cd1..d4c219c8 100644 --- a/src/test/java/me/snaptime/user/service/ProfilePhotoServiceTest.java +++ b/src/test/java/me/snaptime/user/service/ProfilePhotoServiceTest.java @@ -6,7 +6,7 @@ import me.snaptime.profilePhoto.service.impl.ProfilePhotoServiceImpl; import me.snaptime.user.domain.User; import me.snaptime.user.repository.UserRepository; -import me.snaptime.util.ProfilePhotoNameGenerator; +import me.snaptime.util.FileNameGenerator; import org.junit.jupiter.api.*; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -70,7 +70,7 @@ void updatePhotoToFileSystemTest() throws Exception { "profile/jpeg", new byte[]{}); - String fileName = ProfilePhotoNameGenerator.generatorProfilePhotoName(updateFile.getOriginalFilename()); + String fileName = FileNameGenerator.generatorName(updateFile.getOriginalFilename()); String filePath = FOLDER_PATH + fileName; /*업데이트 할 프로필 사진*/