diff --git a/src/main/java/org/ezcode/codetest/application/problem/service/ProblemService.java b/src/main/java/org/ezcode/codetest/application/problem/service/ProblemService.java index afabe5f6..20481e19 100644 --- a/src/main/java/org/ezcode/codetest/application/problem/service/ProblemService.java +++ b/src/main/java/org/ezcode/codetest/application/problem/service/ProblemService.java @@ -12,6 +12,8 @@ import org.ezcode.codetest.domain.user.service.UserDomainService; import org.ezcode.codetest.infrastructure.s3.S3Directory; import org.ezcode.codetest.infrastructure.s3.S3Uploader; +import org.ezcode.codetest.infrastructure.s3.exception.S3Exception; +import org.ezcode.codetest.infrastructure.s3.exception.code.S3ExceptionCode; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @@ -19,9 +21,11 @@ import org.springframework.web.multipart.MultipartFile; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; @Service @RequiredArgsConstructor +@Slf4j public class ProblemService { private final ProblemDomainService problemDomainService; @@ -39,8 +43,8 @@ public ProblemDetailResponse createProblem(ProblemCreateRequest requestDto, Mult // 문제 이미지 있다면? if (image != null && !image.isEmpty()) { - String imageUrl = s3Uploader.upload(image, S3Directory.PROBLEM.getDir()); - savedProblem.addImage(imageUrl); + String imageUrl = uploadImageAfterTransaction(image, savedProblem.getId()); + updateProblemWithImage(savedProblem.getId(), imageUrl); } return ProblemDetailResponse.from(savedProblem); @@ -91,5 +95,20 @@ public void removeProblem(Long problemId) { problemDomainService.removeProblem(findProblem); } + + @Transactional + public void updateProblemWithImage(Long problemId, String imageUrl) { + Problem problem = problemDomainService.getProblem(problemId); + problem.addImage(imageUrl); + } + + private String uploadImageAfterTransaction(MultipartFile image, Long problemId) { + try { + return s3Uploader.upload(image, S3Directory.PROBLEM.getDir()); + } catch (Exception e) { + log.error("Problem {} 이미지 업로드 실패", problemId, e); + throw new S3Exception(S3ExceptionCode.S3_UPLOAD_FAILED); + } + } }