-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BE] 이벤트당 이미지 업로드 수량 제한 #819
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
server/src/main/java/server/haengdong/application/EventImageFacadeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package server.haengdong.application; | ||
|
||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class EventImageFacadeService { | ||
|
||
private final EventService eventService; | ||
private final ImageService imageService; | ||
|
||
public void uploadImages(String token, List<MultipartFile> images) { | ||
eventService.validateImageCount(token, images.size()); | ||
List<String> imageNames = imageService.uploadImages(images); | ||
eventService.saveImages(token, imageNames); | ||
} | ||
|
||
public void deleteImage(String token, Long imageId) { | ||
String imageName = eventService.deleteImage(token, imageId); | ||
imageService.deleteImage(imageName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Propagation; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import server.haengdong.application.request.EventAppRequest; | ||
import server.haengdong.application.request.EventLoginAppRequest; | ||
|
@@ -31,6 +32,8 @@ | |
@Service | ||
public class EventService { | ||
|
||
private static final int MAX_IMAGE_COUNT = 10; | ||
|
||
private final EventRepository eventRepository; | ||
private final EventTokenProvider eventTokenProvider; | ||
private final BillRepository billRepository; | ||
|
@@ -137,4 +140,14 @@ public String deleteImage(String token, Long imageId) { | |
eventImageRepository.delete(eventImage); | ||
return eventImage.getName(); | ||
} | ||
|
||
public void validateImageCount(String token, int uploadImageCount) { | ||
Event event = getEvent(token); | ||
Long imageCount = eventImageRepository.countByEvent(event); | ||
Long totalImageCount = imageCount + uploadImageCount; | ||
|
||
if (totalImageCount > MAX_IMAGE_COUNT) { | ||
throw new HaengdongException(HaengdongErrorCode.IMAGE_COUNT_INVALID, totalImageCount); | ||
} | ||
} | ||
Comment on lines
+144
to
+152
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 메서드가 왜 public이어야 하는지 모르겠습니다🙃 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 위 답변 참고해주세요. |
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
server/src/test/java/server/haengdong/application/ImageServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package server.haengdong.application; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.awaitility.Awaitility.given; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.List; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.mock.web.MockMultipartFile; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import server.haengdong.exception.HaengdongException; | ||
import software.amazon.awssdk.core.sync.RequestBody; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
import software.amazon.awssdk.services.s3.model.S3Exception; | ||
|
||
|
||
class ImageServiceTest extends ServiceTestSupport { | ||
|
||
@Autowired | ||
private ImageService imageService; | ||
|
||
@MockBean | ||
private S3Client s3Client; | ||
|
||
@DisplayName("이미지 업로드 도중 실패하면 예외가 커스텀 예외가 발생한다.") | ||
@Test | ||
void uploadImages() { | ||
MultipartFile multipartFile1 = new MockMultipartFile("file1", "test1.txt", "text/plain", "hello1".getBytes()); | ||
MultipartFile multipartFile2 = new MockMultipartFile("file2", "test2.txt", "text/plain", "hello2".getBytes()); | ||
MultipartFile multipartFile3 = new MockMultipartFile("file3", "test3.txt", "text/plain", "hello3".getBytes()); | ||
MultipartFile multipartFile4 = new MockMultipartFile("file4", "test4.txt", "text/plain", "hello4".getBytes()); | ||
MultipartFile multipartFile5 = new MockMultipartFile("file5", "test5.txt", "text/plain", "hello5".getBytes()); | ||
|
||
doThrow(new RuntimeException("S3 upload failed")) | ||
.when(s3Client).putObject(any(PutObjectRequest.class), any(RequestBody.class)); | ||
|
||
assertThatThrownBy(() -> imageService.uploadImages(List.of(multipartFile1, multipartFile2, multipartFile3, multipartFile4, multipartFile5))) | ||
.isInstanceOf(HaengdongException.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 EventImageFacadeService의 필요성을 못 느끼겠어요.
EventService가 ImageService를 가지고있고, saveImages 할 때 내부에서 validateImageCount 메서드와 같은 검증 로직을 수행해주면 되는거 아닌가요?
이렇게 하면 백호가 말씀해주신 것 처럼 별도 메서드로 분리해서 인지하기 어려워지는 문제도 해결되는 것으로 보입니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EventImageFacadeService 를 사용한 이유는 트랜잭션 분리를 명확하게 하기 위함입니다.
imageService.uploadImages(images)은 긴 시간이 소모되는 작업으로 트랜잭션 내부에서 동작하면 안됩니다. 그래서 트랜잭션을 분리해야합니다.
EventService가 ImageService를 가지고 있는 방법을 사용하게 된다면 아래와 비슷한 구조가 될 것같습니다.
이런 구조가 되면 EventService의 saveImagesAndUpload 메소드에 트랜잭션이 걸려서는 안됩니다.(imageService.uploadImages(images); 가 트랜잭션 범위 안에 있으면 안되기 때문)
그런데 validateImageCount와 saveImages는 트랜잭션이 필요하죠.
하지만 프록시로 인해 내부 메소드 호출에 트랜잭션을 적용할 수 없습니다. 셀프 참조 등의 방법이 있긴하지만 복잡도가 많이 올라갑니다.
결론은 트랜잭션 분리를 확실히 하기 위해 Facade 패턴을 도입했습니다.
제가 제시한 다른 방법 중에
1, 2 중에서는 1번 방법이 더 좋지 않을까 생각합니다. 1번은 업로드 전에 검증을 먼저 할 수 있고 업로드에 실패해도 데이터베이스에 보상을 해줄 수 있어서 리소스가 적을 것 같습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EventService가 ImageService를 가지고 사용하는 예시에서 ImageService.uploadImages를 비동기로 처리하면 EventService.saveImagesAndUpload 메서드에서 시작한 트랜잭션이 기다리지 않고 메서드를 종료할 수 있긴 합니다. 일단은 지금대로 진행 하시죠
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
맞습니다. 비동기로 처리하면 바로 종료할 수 있죠. 그런데 업로드에 실패했을 때 에러 응답을 내려줄 수 없어 더 고민을 해봐야겠네요.