-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refactor] PhotoService 인터페이스의 구현체를 Facade 객체로 두어 이메일 전송 로직을 트랜잭션에서 제외
- Loading branch information
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
src/main/java/gdsc/cau/puangbe/photo/service/PhotoServiceFacadeImpl.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,73 @@ | ||
package gdsc.cau.puangbe.photo.service; | ||
|
||
import gdsc.cau.puangbe.common.exception.BaseException; | ||
import gdsc.cau.puangbe.common.util.ConstantUtil; | ||
import gdsc.cau.puangbe.common.util.ResponseCode; | ||
import gdsc.cau.puangbe.photo.dto.request.EmailInfoDto; | ||
import gdsc.cau.puangbe.photo.entity.PhotoResult; | ||
import jakarta.mail.internet.MimeMessage; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.mail.javamail.MimeMessageHelper; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.thymeleaf.TemplateEngine; | ||
import org.thymeleaf.context.Context; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class PhotoServiceFacadeImpl implements PhotoService{ | ||
private final PhotoServiceImpl photoServiceImpl; | ||
private final JavaMailSender mailSender; | ||
private final TemplateEngine templateEngine; | ||
|
||
private final String EMAIL_LINK = "https://www.google.com/"; // TODO : 프론트 분들 링크 관련 답변 오면 프레임 페이지 링크 관련 수정 | ||
|
||
// 완성된 요청 id 및 imageUrl을 받아 저장 | ||
@Override | ||
public void uploadPhoto(Long photoRequestId, String imageUrl) { | ||
sendEmail(photoServiceImpl.uploadPhoto(photoRequestId, imageUrl)); | ||
} | ||
|
||
// 특정 요청의 imageUrl 조회 | ||
@Override | ||
@Transactional(readOnly = true) | ||
public String getPhotoUrl(Long photoRequestId) { | ||
PhotoResult photoResult = photoServiceImpl.getPhotoResult(photoRequestId); | ||
if(photoResult.getImageUrl() == null){ | ||
throw new BaseException(ResponseCode.IMAGE_ON_PROCESS); | ||
} | ||
|
||
log.info("결과 이미지 URL 조회 완료: {}", photoResult.getImageUrl()); | ||
return photoResult.getImageUrl(); | ||
} | ||
|
||
// 유저에게 이메일 전송 | ||
private void sendEmail(EmailInfoDto emailInfoDto) { | ||
// 이메일 템플릿 내용 설정 | ||
Context context = new Context(); | ||
context.setVariable("userName", emailInfoDto.getName()); | ||
context.setVariable("photoUrl", emailInfoDto.getPhotoUrl()); | ||
context.setVariable("framePageUrl", EMAIL_LINK); | ||
String body = templateEngine.process("email-template", context); | ||
|
||
try { | ||
// 메일 정보 설정 | ||
MimeMessage mimeMessage = mailSender.createMimeMessage(); | ||
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, "UTF-8"); | ||
messageHelper.setFrom(ConstantUtil.GDSC_CAU_EMAIL); | ||
messageHelper.setTo(emailInfoDto.getEmail()); | ||
messageHelper.setSubject("[푸앙이 사진관] AI 프로필 사진 생성 완료"); | ||
messageHelper.setText(body, true); | ||
|
||
// 메일 전송 | ||
mailSender.send(mimeMessage); | ||
log.info("이메일 전송 완료: {}", emailInfoDto.getEmail()); | ||
} catch (Exception e){ | ||
e.printStackTrace(); | ||
throw new BaseException(ResponseCode.EMAIL_SEND_ERROR); | ||
} | ||
} | ||
} |