From 2b09dac234f9cabbb3da8723688a4d9a781e59c5 Mon Sep 17 00:00:00 2001 From: tkdwns414 Date: Fri, 7 Jun 2024 20:41:05 +0900 Subject: [PATCH] [fix] #98 fix api to create answer without image --- .../server/service/answer/AnswerService.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/telepigeon/server/service/answer/AnswerService.java b/src/main/java/com/telepigeon/server/service/answer/AnswerService.java index d0968a9..35e861c 100644 --- a/src/main/java/com/telepigeon/server/service/answer/AnswerService.java +++ b/src/main/java/com/telepigeon/server/service/answer/AnswerService.java @@ -23,6 +23,7 @@ import org.springframework.data.util.Pair; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.time.LocalDate; @@ -47,7 +48,7 @@ public class AnswerService { private final FcmService fcmService; private final S3Service s3Service; - private static String ANSWER_S3_UPLOAD_FOLDER = "/answer/"; + private static String ANSWER_S3_UPLOAD_FOLDER = "answer/"; @Transactional public Answer create( @@ -64,10 +65,14 @@ public Answer create( ConfidenceCreateDto.of(answerCreateDto.content()) ); Double emotion = (confidence.positive() - confidence.negative()) * 0.01; + Answer answer = answerSaver.create( - Answer.create(answerCreateDto.content(), - s3Service.uploadImage(ANSWER_S3_UPLOAD_FOLDER, answerCreateDto.image()), - emotion, question, profile) + Answer.create( + answerCreateDto.content(), + uploadImage(answerCreateDto.image()), + emotion, + question, + profile) ); profile.updateEmotion( @@ -212,4 +217,10 @@ private double CalculateEmotion( ) { return totEmotion * 0.9 + emotion * 0.1; } + + private String uploadImage(MultipartFile image) throws IOException { + if (image != null) + return s3Service.uploadImage(ANSWER_S3_UPLOAD_FOLDER, image); + return null; + } }