Skip to content

Commit 61d0e6e

Browse files
authored
refactor : 트랜잭션 범위 내 외부 서비스 호출 처리 (#102)
1 parent d7ecc3a commit 61d0e6e

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/main/java/org/ezcode/codetest/application/problem/service/ProblemService.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@
1212
import org.ezcode.codetest.domain.user.service.UserDomainService;
1313
import org.ezcode.codetest.infrastructure.s3.S3Directory;
1414
import org.ezcode.codetest.infrastructure.s3.S3Uploader;
15+
import org.ezcode.codetest.infrastructure.s3.exception.S3Exception;
16+
import org.ezcode.codetest.infrastructure.s3.exception.code.S3ExceptionCode;
1517
import org.springframework.data.domain.Page;
1618
import org.springframework.data.domain.Pageable;
1719
import org.springframework.stereotype.Service;
1820
import org.springframework.transaction.annotation.Transactional;
1921
import org.springframework.web.multipart.MultipartFile;
2022

2123
import lombok.RequiredArgsConstructor;
24+
import lombok.extern.slf4j.Slf4j;
2225

2326
@Service
2427
@RequiredArgsConstructor
28+
@Slf4j
2529
public class ProblemService {
2630

2731
private final ProblemDomainService problemDomainService;
@@ -39,8 +43,8 @@ public ProblemDetailResponse createProblem(ProblemCreateRequest requestDto, Mult
3943

4044
// 문제 이미지 있다면?
4145
if (image != null && !image.isEmpty()) {
42-
String imageUrl = s3Uploader.upload(image, S3Directory.PROBLEM.getDir());
43-
savedProblem.addImage(imageUrl);
46+
String imageUrl = uploadImageAfterTransaction(image, savedProblem.getId());
47+
updateProblemWithImage(savedProblem.getId(), imageUrl);
4448
}
4549

4650
return ProblemDetailResponse.from(savedProblem);
@@ -91,5 +95,20 @@ public void removeProblem(Long problemId) {
9195

9296
problemDomainService.removeProblem(findProblem);
9397
}
98+
99+
@Transactional
100+
public void updateProblemWithImage(Long problemId, String imageUrl) {
101+
Problem problem = problemDomainService.getProblem(problemId);
102+
problem.addImage(imageUrl);
103+
}
104+
105+
private String uploadImageAfterTransaction(MultipartFile image, Long problemId) {
106+
try {
107+
return s3Uploader.upload(image, S3Directory.PROBLEM.getDir());
108+
} catch (Exception e) {
109+
log.error("Problem {} 이미지 업로드 실패", problemId, e);
110+
throw new S3Exception(S3ExceptionCode.S3_UPLOAD_FAILED);
111+
}
112+
}
94113
}
95114

0 commit comments

Comments
 (0)