Skip to content

Commit

Permalink
Refactor: Answer Dto 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
tioon committed May 12, 2024
1 parent ebb7e47 commit a077090
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public class AnswerApplication {
private final MemberRepository memberRepository;
private final QuestionRepository questionRepository;

public AnswerDetailResponse createAnswer(AnswerCreateRequest request, Long memberId, List<MultipartFile> 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());
Question question = questionRepository.findById(request.getQuestionId())
.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, "나중에 수정 요망");
}

Expand All @@ -49,15 +49,15 @@ public List<AnswerResponse> getAnswersByMemberId(Long memberId) {
.collect(Collectors.toList());
}

/* public Page<AnswerDetailResponse> getAllAnswers(Long memberId, Pageable pageable) {
public Page<AnswerDetailResponse> getAllAnswers(Long memberId, Pageable pageable) {
Page<Answer> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,22 @@ public class AnswerService {
private final S3ImageStorageService s3ImageStorageService;

@Transactional
public Answer createAnswer(Answer answer, List<MultipartFile> imageFiles, MultipartFile audioFile) {
public Answer createAnswer(Answer answer, MultipartFile imageFile) {
List<String> 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) {
// 롤백 이후 파일 삭제 로직
Expand All @@ -69,46 +60,24 @@ public Answer createAnswer(Answer answer, List<MultipartFile> imageFiles, Multip
question.setAnswered(true);
return savedAnswer;
}

public List<Answer> getAnswersByMemberId(Long memberId) {
return answerRepository.findByMemberId(memberId);
}

@Transactional
public Page<Answer> 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));

List<String> urls = new ArrayList<>();
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);
Expand All @@ -127,6 +96,15 @@ public Answer updateAnswer(Long answerId, AnswerCreateRequest request, Multipart
return answerRepository.save(answer);
}

public List<Answer> getAnswersByMemberId(Long memberId) {
return answerRepository.findByMemberId(memberId);
}

@Transactional
public Page<Answer> getAllAnswers(Long memberId, Pageable pageable) {
return answerRepository.findAllByMemberId(memberId, pageable);
}

@Transactional
public void deleteAnswer(Long answerId) {
Answer answer = answerRepository.findByAnswerId(answerId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@

import java.util.List;

/*@RestController
@RestController
@AllArgsConstructor
@RequestMapping("/api/answers")
public class AnswerController implements AnswerApi {
private final AnswerApplication answerApplication;

@PostMapping(value = "/{memberId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<AnswerDetailResponse> createAnswer(@PathVariable Long memberId,
@RequestPart(value = "imageFiles") List<MultipartFile> 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);
}

Expand All @@ -50,11 +49,9 @@ public ResponseEntity<List<AnswerDetailResponse>> getAllAnswers(@RequestParam Lo
@PutMapping(value = "/{answerId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<AnswerDetailResponse> updateAnswer(
@PathVariable Long answerId,
@RequestPart(value = "imageFiles") List<MultipartFile> 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);
}

Expand All @@ -75,4 +72,4 @@ public ResponseEntity<Void> updateAnswerReactions(
answerApplication.updateReactionCounts(answerId, heartCount, curiousCount, sadCount);
return ResponseEntity.ok().build();
}
}*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public interface AnswerApi {
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
ResponseEntity<AnswerDetailResponse> createAnswer(
@PathVariable Long memberId,
@RequestPart(value = "imageFiles") List<MultipartFile> imageFiles,
@RequestPart(value = "audioFile") MultipartFile audioFile,
@RequestPart(value = "imageFiles") MultipartFile imageFile,
@RequestPart(name = "request") AnswerCreateRequest request);

@Operation(
Expand Down Expand Up @@ -96,8 +95,7 @@ ResponseEntity<List<AnswerDetailResponse>> getAllAnswers(
@PutMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
ResponseEntity<AnswerDetailResponse> updateAnswer(
@PathVariable Long answerId,
@RequestPart(value = "imageFiles") List<MultipartFile> imageFiles,
@RequestPart(value = "audioFile") MultipartFile audioFile,
@RequestPart(value = "imageFiles") MultipartFile imageFiles,
@RequestPart(name = "request") AnswerCreateRequest request);

@Operation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,21 @@ public class AnswerCreateRequest {
private String musicName;
private String musicSinger;
private String musicAudioUrl;
private List<String> imageUrls; // 이미지 URL 리스트

public AnswerCreateRequest(Long questionId, String content, List<String> linkAttachments,
String musicName, String musicSinger, String musicAudioUrl,
List<String> 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<String> linkAttachments,
String musicName, String musicSinger, String musicAudioUrl,
List<String> imageUrls) {
return new AnswerCreateRequest(questionId, content, linkAttachments, musicName, musicSinger,
musicAudioUrl, imageUrls);
musicAudioUrl);
}
}

0 comments on commit a077090

Please sign in to comment.