From a07709061e15e1585bcdb7ff167da71856738a60 Mon Sep 17 00:00:00 2001 From: kimyechan Date: Mon, 13 May 2024 05:04:28 +0900 Subject: [PATCH] =?UTF-8?q?Refactor:=20Answer=20Dto=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/answer/AnswerApplication.java | 18 ++--- .../answer/service/AnswerService.java | 66 +++++++------------ .../infra/answer/repository/AnswerMapper.java | 1 - .../presentation/answer/AnswerController.java | 15 ++--- .../presentation/answer/api/AnswerApi.java | 6 +- .../answer/dto/AnswerCreateRequest.java | 7 +- 6 files changed, 41 insertions(+), 72 deletions(-) diff --git a/baebae-BE/src/main/java/com/web/baebaeBE/application/answer/AnswerApplication.java b/baebae-BE/src/main/java/com/web/baebaeBE/application/answer/AnswerApplication.java index ffcfb950..041bc391 100644 --- a/baebae-BE/src/main/java/com/web/baebaeBE/application/answer/AnswerApplication.java +++ b/baebae-BE/src/main/java/com/web/baebaeBE/application/answer/AnswerApplication.java @@ -30,7 +30,7 @@ public class AnswerApplication { private final MemberRepository memberRepository; private final QuestionRepository questionRepository; - public AnswerDetailResponse createAnswer(AnswerCreateRequest request, Long memberId, List imageFiles, MultipartFile audioFile) { + public AnswerDetailResponse createAnswer(AnswerCreateRequest request, Long memberId, MultipartFile imageFiles) { Member member = memberRepository.findById(memberId) .orElseThrow(() -> new BusinessException(LoginException.NOT_EXIST_MEMBER)); System.out.println(request.getQuestionId()); @@ -38,7 +38,7 @@ public AnswerDetailResponse createAnswer(AnswerCreateRequest request, Long membe .orElseThrow(() -> new BusinessException(AnswerError.NO_EXIST_QUESTION)); Answer answerEntity = answerMapper.toEntity(request, question, member); - Answer savedAnswerEntity = answerService.createAnswer(answerEntity, imageFiles, audioFile); + Answer savedAnswerEntity = answerService.createAnswer(answerEntity, imageFiles); return answerMapper.toDomain(savedAnswerEntity, "나중에 수정 요망"); } @@ -49,15 +49,15 @@ public List getAnswersByMemberId(Long memberId) { .collect(Collectors.toList()); } - /* public Page getAllAnswers(Long memberId, Pageable pageable) { + public Page getAllAnswers(Long memberId, Pageable pageable) { Page answerPage = answerService.getAllAnswers(memberId, pageable); - return answerPage.map(answerMapper::toDomain); - }*/ + return answerPage.map(answer -> answerMapper.toDomain(answer, "나중에 수정 요망")); + } - /*public AnswerDetailResponse updateAnswer(Long answerId, AnswerCreateRequest request, MultipartFile[] imageFiles, MultipartFile audioFile) { - Answer updatedAnswer = answerService.updateAnswer(answerId, request, imageFiles, audioFile); - return answerMapper.toDomain(updatedAnswer); - }*/ + public AnswerDetailResponse updateAnswer(Long answerId, AnswerCreateRequest request, MultipartFile imageFiles) { + Answer updatedAnswer = answerService.updateAnswer(answerId, request, imageFiles); + return answerMapper.toDomain(updatedAnswer, "나중에 수정 요망"); + } public void deleteAnswer(Long answerId) { answerService.deleteAnswer(answerId); diff --git a/baebae-BE/src/main/java/com/web/baebaeBE/domain23/answer/service/AnswerService.java b/baebae-BE/src/main/java/com/web/baebaeBE/domain23/answer/service/AnswerService.java index 3b2ffad4..cc202afe 100644 --- a/baebae-BE/src/main/java/com/web/baebaeBE/domain23/answer/service/AnswerService.java +++ b/baebae-BE/src/main/java/com/web/baebaeBE/domain23/answer/service/AnswerService.java @@ -28,31 +28,22 @@ public class AnswerService { private final S3ImageStorageService s3ImageStorageService; @Transactional - public Answer createAnswer(Answer answer, List imageFiles, MultipartFile audioFile) { + public Answer createAnswer(Answer answer, MultipartFile imageFile) { List imageUrls = new ArrayList<>(); String musicAudioUrl = null; Answer savedAnswer = answerRepository.save(answer); try { // 이미지 파일 업로드 - for (int i = 0; i < imageFiles.size(); i++) { - MultipartFile file = imageFiles.get(i); - if (!file.isEmpty()) { - InputStream inputStream = file.getInputStream(); - String imageUrl = s3ImageStorageService.uploadFile(answer.getMember().getId().toString(), answer.getId().toString(), "image", i, inputStream, file.getSize(), file.getContentType()); - imageUrls.add(imageUrl); - } - } - - // 오디오 파일 업로드 - if (audioFile != null && !audioFile.isEmpty()) { - InputStream inputStream = audioFile.getInputStream(); - musicAudioUrl = s3ImageStorageService.uploadFile(answer.getMember().getId().toString(), answer.getId().toString(), "audio", 0, inputStream, audioFile.getSize(), audioFile.getContentType()); + if (!imageFile.isEmpty()) { + InputStream inputStream = imageFile.getInputStream(); + String imageUrl = s3ImageStorageService.uploadFile(answer.getMember().getId().toString(), answer.getId().toString(), "image", 0, inputStream, imageFile.getSize(), imageFile.getContentType()); + imageUrls.add(imageUrl); } // 파일 URL을 Answer 엔티티에 저장 answer.setImageFiles(imageUrls); - answer.setMusicAudio(musicAudioUrl); + answer.setMusicAudio(answer.getMusicAudio()); } catch (Exception e) { // 롤백 이후 파일 삭제 로직 @@ -69,18 +60,9 @@ public Answer createAnswer(Answer answer, List imageFiles, Multip question.setAnswered(true); return savedAnswer; } - - public List getAnswersByMemberId(Long memberId) { - return answerRepository.findByMemberId(memberId); - } @Transactional - public Page getAllAnswers(Long memberId, Pageable pageable) { - return answerRepository.findAllByMemberId(memberId, pageable); - } - - @Transactional - public Answer updateAnswer(Long answerId, AnswerCreateRequest request, MultipartFile[] imageFiles, MultipartFile audioFile) { + public Answer updateAnswer(Long answerId, AnswerCreateRequest request, MultipartFile imageFile) { Answer answer = answerRepository.findByAnswerId(answerId) .orElseThrow(() -> new BusinessException(AnswerError.NO_EXIST_ANSWER)); @@ -88,27 +70,14 @@ public Answer updateAnswer(Long answerId, AnswerCreateRequest request, Multipart try { String memberId = answer.getMember().getId().toString(); String fileType = "image"; - int index = 0; // 이미지 파일 처리 - for (MultipartFile file : imageFiles) { - String fileName = "image_" + index + ".jpg"; - InputStream inputStream = file.getInputStream(); - long size = file.getSize(); - String contentType = file.getContentType(); - String url = s3ImageStorageService.uploadFile(memberId, answerId.toString(), fileType, index, inputStream, size, contentType); - urls.add(url); - index++; - } - - // 오디오 파일 처리 - if (audioFile != null && !audioFile.isEmpty()) { - InputStream inputStream = audioFile.getInputStream(); - long size = audioFile.getSize(); - String contentType = audioFile.getContentType(); - String musicAudioUrl = s3ImageStorageService.uploadFile(memberId, answerId.toString(), "audio", 0, inputStream, size, contentType); - answer.setMusicAudio(musicAudioUrl); - } + String fileName = "image_0.jpg"; + InputStream inputStream = imageFile.getInputStream(); + long size = imageFile.getSize(); + String contentType = imageFile.getContentType(); + String url = s3ImageStorageService.uploadFile(memberId, answerId.toString(), fileType, 0, inputStream, size, contentType); + urls.add(url); } catch (IOException e) { throw new BusinessException(AnswerError.IMAGE_PROCESSING_ERROR); @@ -127,6 +96,15 @@ public Answer updateAnswer(Long answerId, AnswerCreateRequest request, Multipart return answerRepository.save(answer); } + public List getAnswersByMemberId(Long memberId) { + return answerRepository.findByMemberId(memberId); + } + + @Transactional + public Page getAllAnswers(Long memberId, Pageable pageable) { + return answerRepository.findAllByMemberId(memberId, pageable); + } + @Transactional public void deleteAnswer(Long answerId) { Answer answer = answerRepository.findByAnswerId(answerId) diff --git a/baebae-BE/src/main/java/com/web/baebaeBE/infra/answer/repository/AnswerMapper.java b/baebae-BE/src/main/java/com/web/baebaeBE/infra/answer/repository/AnswerMapper.java index f0282c93..98036246 100644 --- a/baebae-BE/src/main/java/com/web/baebaeBE/infra/answer/repository/AnswerMapper.java +++ b/baebae-BE/src/main/java/com/web/baebaeBE/infra/answer/repository/AnswerMapper.java @@ -17,7 +17,6 @@ public Answer toEntity(AnswerCreateRequest request, Question question, Member me return Answer.builder() .question(question) .member(member) - .imageFiles(request.getImageUrls()) // 이미지 URL 리스트 처리 .content(request.getContent()) .linkAttachments(request.getLinkAttachments()) // 링크 첨부 리스트 처리 .musicName(request.getMusicName()) diff --git a/baebae-BE/src/main/java/com/web/baebaeBE/presentation/answer/AnswerController.java b/baebae-BE/src/main/java/com/web/baebaeBE/presentation/answer/AnswerController.java index cb280d25..918a7170 100644 --- a/baebae-BE/src/main/java/com/web/baebaeBE/presentation/answer/AnswerController.java +++ b/baebae-BE/src/main/java/com/web/baebaeBE/presentation/answer/AnswerController.java @@ -20,7 +20,7 @@ import java.util.List; -/*@RestController +@RestController @AllArgsConstructor @RequestMapping("/api/answers") public class AnswerController implements AnswerApi { @@ -28,10 +28,9 @@ public class AnswerController implements AnswerApi { @PostMapping(value = "/{memberId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ResponseEntity createAnswer(@PathVariable Long memberId, - @RequestPart(value = "imageFiles") List imageFiles, - @RequestPart(value = "audioFile") MultipartFile audioFile, + @RequestPart(value = "imageFile") MultipartFile imageFile, @RequestPart AnswerCreateRequest request) { - AnswerDetailResponse createdAnswer = answerApplication.createAnswer(request, memberId, imageFiles, audioFile); + AnswerDetailResponse createdAnswer = answerApplication.createAnswer(request, memberId, imageFile); return ResponseEntity.status(HttpStatus.CREATED).body(createdAnswer); } @@ -50,11 +49,9 @@ public ResponseEntity> getAllAnswers(@RequestParam Lo @PutMapping(value = "/{answerId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ResponseEntity updateAnswer( @PathVariable Long answerId, - @RequestPart(value = "imageFiles") List imageFiles, - @RequestPart(value = "audioFile") MultipartFile audioFile, + @RequestPart(value = "imageFile") MultipartFile imageFile, @RequestPart AnswerCreateRequest request) { - MultipartFile[] imageFilesArray = imageFiles.toArray(new MultipartFile[0]); - AnswerDetailResponse updatedAnswer = answerApplication.updateAnswer(answerId, request, imageFilesArray, audioFile); + AnswerDetailResponse updatedAnswer = answerApplication.updateAnswer(answerId, request, imageFile); return ResponseEntity.ok(updatedAnswer); } @@ -75,4 +72,4 @@ public ResponseEntity updateAnswerReactions( answerApplication.updateReactionCounts(answerId, heartCount, curiousCount, sadCount); return ResponseEntity.ok().build(); } -}*/ \ No newline at end of file +} \ No newline at end of file diff --git a/baebae-BE/src/main/java/com/web/baebaeBE/presentation/answer/api/AnswerApi.java b/baebae-BE/src/main/java/com/web/baebaeBE/presentation/answer/api/AnswerApi.java index d5b885ed..d25db9e9 100644 --- a/baebae-BE/src/main/java/com/web/baebaeBE/presentation/answer/api/AnswerApi.java +++ b/baebae-BE/src/main/java/com/web/baebaeBE/presentation/answer/api/AnswerApi.java @@ -42,8 +42,7 @@ public interface AnswerApi { @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) ResponseEntity createAnswer( @PathVariable Long memberId, - @RequestPart(value = "imageFiles") List imageFiles, - @RequestPart(value = "audioFile") MultipartFile audioFile, + @RequestPart(value = "imageFiles") MultipartFile imageFile, @RequestPart(name = "request") AnswerCreateRequest request); @Operation( @@ -96,8 +95,7 @@ ResponseEntity> getAllAnswers( @PutMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) ResponseEntity updateAnswer( @PathVariable Long answerId, - @RequestPart(value = "imageFiles") List imageFiles, - @RequestPart(value = "audioFile") MultipartFile audioFile, + @RequestPart(value = "imageFiles") MultipartFile imageFiles, @RequestPart(name = "request") AnswerCreateRequest request); @Operation( diff --git a/baebae-BE/src/main/java/com/web/baebaeBE/presentation/answer/dto/AnswerCreateRequest.java b/baebae-BE/src/main/java/com/web/baebaeBE/presentation/answer/dto/AnswerCreateRequest.java index 79ade8ad..9ec513a3 100644 --- a/baebae-BE/src/main/java/com/web/baebaeBE/presentation/answer/dto/AnswerCreateRequest.java +++ b/baebae-BE/src/main/java/com/web/baebaeBE/presentation/answer/dto/AnswerCreateRequest.java @@ -15,24 +15,21 @@ public class AnswerCreateRequest { private String musicName; private String musicSinger; private String musicAudioUrl; - private List imageUrls; // 이미지 URL 리스트 public AnswerCreateRequest(Long questionId, String content, List linkAttachments, - String musicName, String musicSinger, String musicAudioUrl, - List imageUrls) { + String musicName, String musicSinger, String musicAudioUrl) { this.questionId = questionId; this.content = content; this.linkAttachments = linkAttachments; this.musicName = musicName; this.musicSinger = musicSinger; this.musicAudioUrl = musicAudioUrl; - this.imageUrls = imageUrls; } public static AnswerCreateRequest of(Long questionId, String content, List linkAttachments, String musicName, String musicSinger, String musicAudioUrl, List imageUrls) { return new AnswerCreateRequest(questionId, content, linkAttachments, musicName, musicSinger, - musicAudioUrl, imageUrls); + musicAudioUrl); } } \ No newline at end of file