Skip to content

Commit

Permalink
fix: Question, Answer api 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jihyo-j committed May 21, 2024
1 parent 3478100 commit 499a822
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ public ResponseEntity<Page<AnswerDetailResponse>> getAllAnswers(
return ResponseEntity.ok(answerService.getAllAnswers(memberId, categoryId, pageable));
}

@PutMapping(value = "/{answerId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<AnswerDetailResponse> updateAnswer(@PathVariable Long answerId,
@RequestPart(value = "imageFile", required = false) MultipartFile imageFile,
@RequestPart AnswerCreateRequest request) {
AnswerDetailResponse updatedAnswer = answerService.updateAnswer(answerId, request, imageFile);
return ResponseEntity.ok(updatedAnswer);
}

@DeleteMapping("/{answerId}")
public ResponseEntity<Void> deleteAnswer(@PathVariable Long answerId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,6 @@ ResponseEntity<Page<AnswerDetailResponse>> getAllAnswers(
@PathVariable Long memberId,
@RequestParam(required = false) Long category,
Pageable pageable);
@Operation(
summary = "답변 수정",
description = "기존 답변을 수정합니다.",
security = @SecurityRequirement(name = "bearerAuth")
)
@Parameter(
in = ParameterIn.HEADER,
name = "Authorization", required = true,
schema = @Schema(type = "string"),
description = "Bearer [Access 토큰]")
@ApiResponse(responseCode = "201", description = "답변 수정 성공",
content = @Content(mediaType = "application/json",
schema = @Schema(implementation = AnswerDetailResponse.class)))
@PutMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
ResponseEntity<AnswerDetailResponse> updateAnswer(
@PathVariable Long answerId,
@RequestPart(value = "imageFile", required = false) MultipartFile imageFile,
@RequestPart AnswerCreateRequest request);

@Operation(
summary = "답변 삭제",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
@NoArgsConstructor
public class AnswerCreateRequest {
private Long questionId;
private String nickname;
private Boolean profileOnOff;
private String content;
private String linkAttachments;
Expand All @@ -20,12 +19,11 @@ public class AnswerCreateRequest {



public AnswerCreateRequest(Long questionId, String content,String nickname,
public AnswerCreateRequest(Long questionId, String content,
Boolean profileOnOff, String linkAttachments,
String musicName, String musicSinger, String musicAudioUrl, String imageUrl, boolean updateImage) {
this.questionId = questionId;
this.content = content;
this.nickname = nickname;
this.profileOnOff = profileOnOff;
this.linkAttachments = linkAttachments;
this.musicName = musicName;
Expand All @@ -36,10 +34,10 @@ public AnswerCreateRequest(Long questionId, String content,String nickname,

}

public static AnswerCreateRequest of(Long questionId, String content, String nickname,
public static AnswerCreateRequest of(Long questionId, String content,
Boolean profileOnOff, String linkAttachments,
String musicName, String musicSinger, String musicAudioUrl, String imageUrl, boolean updateImage) {
return new AnswerCreateRequest(questionId, content, nickname, profileOnOff, linkAttachments,
return new AnswerCreateRequest(questionId, content, profileOnOff, linkAttachments,
musicName, musicSinger, musicAudioUrl, imageUrl, updateImage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ public class AnswerDetailResponse {
private String questionContent;
private Long memberId;
private String content;
private String senderNickname;
private String nickname;
private String memberNickname;
private Boolean profileOnOff;
private String linkAttachments;
private String musicName;
Expand All @@ -25,7 +24,7 @@ public class AnswerDetailResponse {


public AnswerDetailResponse(Long answerId, Long questionId, String questionContent, Long memberId,
String content, String senderNickname, String nickname, Boolean profileOnOff,
String content, String memberNickname, Boolean profileOnOff,
String linkAttachments, String musicName, String musicSinger, String musicAudioUrl,
String imageUrl, LocalDateTime createdDate) {

Expand All @@ -35,8 +34,7 @@ public AnswerDetailResponse(Long answerId, Long questionId, String questionConte
this.questionContent = questionContent;
this.memberId = memberId;
this.content = content;
this.senderNickname = senderNickname;
this.nickname = nickname;
this.memberNickname = memberNickname;
this.profileOnOff = profileOnOff;
this.linkAttachments = linkAttachments;
this.musicName = musicName;
Expand All @@ -47,11 +45,11 @@ public AnswerDetailResponse(Long answerId, Long questionId, String questionConte
}

public static AnswerDetailResponse of(Long answerId, Long questionId, String questionContent, Long memberId,
String content, String memberNickname, String nickname, Boolean profileOnOff,
String content, String memberNickname, Boolean profileOnOff,
String linkAttachments, String musicName, String musicSinger, String musicAudioUrl,
String imageUrl, LocalDateTime createdDate) {
return new AnswerDetailResponse(answerId, questionId, questionContent, memberId, content, memberNickname,
nickname, profileOnOff, linkAttachments, musicName, musicSinger, musicAudioUrl, imageUrl, createdDate);
profileOnOff, linkAttachments, musicName, musicSinger, musicAudioUrl, imageUrl, createdDate);


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public class Answer {
@OnDelete(action = OnDeleteAction.CASCADE)
private Member member;

@Column(name = "nickname")
private String nickname;

@Column(name = "image_file")
private String imageFile; // 이미지 파일 경로를 저장하는 리스트
Expand Down Expand Up @@ -67,10 +65,10 @@ public class Answer {
@Column(name = "profile_on_off", nullable = false)
private boolean profileOnOff;

public static Answer of(Long id, Question question, Member member, String nickname, String content,
public static Answer of(Long id, Question question, Member member, String content,
String imageFile, Music music, String linkAttachments, String imageUrl, LocalDateTime createdDate,
ReactionCount reactionCount, boolean profileOnOff) {
return new Answer(id, question, member, nickname, imageFile, content, music, linkAttachments, imageUrl, createdDate, null, reactionCount, profileOnOff);
return new Answer(id, question, member, imageFile, content, music, linkAttachments, imageUrl, createdDate, null, reactionCount, profileOnOff);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public Answer toEntity(AnswerCreateRequest request, Question question, Member me
Answer answer = Answer.builder()
.question(question)
.member(member)
.nickname(request.getNickname())
.content(request.getContent())
.linkAttachments(request.getLinkAttachments())
.profileOnOff(request.getProfileOnOff())
Expand All @@ -53,8 +52,7 @@ public AnswerDetailResponse toDomain(Answer answer) {
question.getContent(),
member.getId(),
answer.getContent(),
question.getSender().getNickname(),
answer.getNickname(),
member.getNickname(),
answer.isProfileOnOff(),
answer.getLinkAttachments(),
music != null ? music.getMusicName() : null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.web.baebaeBE.domain.answer.service;

import com.web.baebaeBE.domain.answer.dto.AnswerDetailResponse;
import com.web.baebaeBE.domain.answer.dto.AnswerResponse;
import com.web.baebaeBE.domain.answer.exception.AnswerError;
import com.web.baebaeBE.domain.answer.repository.AnswerMapper;
import com.web.baebaeBE.domain.categorized.answer.entity.CategorizedAnswer;
Expand Down Expand Up @@ -33,9 +32,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
Expand Down Expand Up @@ -100,13 +97,6 @@ public AnswerDetailResponse createAnswer(AnswerCreateRequest request, Long membe
return answerMapper.toDomain(savedAnswer);
}

@Transactional
public List<AnswerResponse> getAnswersByMemberId(Long memberId) {
List<Answer> answers = answerRepository.findByMemberId(memberId);
return answers.stream()
.map(answerMapper::toResponse)
.collect(Collectors.toList());
}

@Transactional
public Page<AnswerDetailResponse> getAllAnswers(Long memberId, Long categoryId, Pageable pageable) {
Expand All @@ -120,36 +110,6 @@ public Page<AnswerDetailResponse> getAllAnswers(Long memberId, Long categoryId,
}
}

@Transactional
public AnswerDetailResponse updateAnswer(Long answerId, AnswerCreateRequest request, MultipartFile imageFile) {
Answer answer = answerRepository.findByAnswerId(answerId)
.orElseThrow(() -> new BusinessException(AnswerError.NO_EXIST_ANSWER));
answer.setContent(request.getContent());
answer.setLinkAttachments(request.getLinkAttachments());

// Music 엔티티 업데이트
answer.getMusic().setMusicName(request.getMusicName());
answer.getMusic().setMusicSinger(request.getMusicSinger());
answer.getMusic().setMusicAudioUrl(request.getMusicAudioUrl());

if (request.isUpdateImage() && imageFile != null && !imageFile.isEmpty()) {
try (InputStream inputStream = imageFile.getInputStream()) {
String imageUrl = s3ImageStorageService.uploadFile(answer.getMember().getId().toString(), answerId.toString(), "image", 0, inputStream, imageFile.getSize(), imageFile.getContentType());
answer.setImageFile(imageUrl);
} catch (IOException e) {
throw new BusinessException(AnswerError.IMAGE_PROCESSING_ERROR);
}
}

// 질문의 isAnswered 상태를 true로 업데이트
Question question = answer.getQuestion();
question.setAnswered(true);

questionRepository.save(question);
answer = answerRepository.save(answer);
return answerMapper.toDomain(answer);
}

@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 @@ -3,6 +3,7 @@
import com.web.baebaeBE.domain.question.controller.api.QuestionApi;
import com.web.baebaeBE.domain.question.dto.QuestionCreateRequest;
import com.web.baebaeBE.domain.question.dto.QuestionDetailResponse;
import com.web.baebaeBE.domain.question.dto.QuestionUpdateRequest;
import com.web.baebaeBE.domain.question.service.QuestionService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -51,8 +52,8 @@ public ResponseEntity<List<QuestionDetailResponse>> getUnansweredQuestions(

@PutMapping("/{questionId}")
public ResponseEntity<Void> updateQuestion(
@PathVariable Long questionId, @RequestParam String content) {
questionService.updateQuestion(questionId, content);
@PathVariable Long questionId, @RequestBody QuestionUpdateRequest request) {
questionService.updateQuestion(questionId, request);
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.web.baebaeBE.domain.question.dto.QuestionCreateRequest;
import com.web.baebaeBE.domain.question.dto.QuestionDetailResponse;
import com.web.baebaeBE.domain.question.dto.QuestionUpdateRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
Expand Down Expand Up @@ -110,8 +111,7 @@ ResponseEntity<List<QuestionDetailResponse>> getUnansweredQuestions(
"}")))
@PutMapping("/{questionId}")
ResponseEntity<Void> updateQuestion(
@PathVariable Long questionId,
@RequestParam String content);
@PathVariable Long questionId, @RequestBody QuestionUpdateRequest request);

@Operation(
summary = "질문 삭제",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.web.baebaeBE.domain.question.dto;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDateTime;
@Getter
@Setter
@NoArgsConstructor
public class QuestionUpdateRequest {
private String content;
private String nickname;

public QuestionUpdateRequest(String content,String nickname) {
this.content = content;
this.nickname = nickname;
}
public static QuestionUpdateRequest of( String content, String nickname) {
return new QuestionUpdateRequest(content, nickname);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class Question {

public void updateContent(String content) {
this.content = content;

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.web.baebaeBE.domain.member.repository.MemberRepository;
import com.web.baebaeBE.domain.question.dto.QuestionCreateRequest;
import com.web.baebaeBE.domain.question.dto.QuestionDetailResponse;
import com.web.baebaeBE.domain.question.dto.QuestionUpdateRequest;
import com.web.baebaeBE.domain.question.exception.QuestionError;
import com.web.baebaeBE.domain.question.repository.QuestionMapper;
import com.web.baebaeBE.global.error.exception.BusinessException;
Expand Down Expand Up @@ -49,14 +50,13 @@ public Page<QuestionDetailResponse> getQuestionsByMemberId(Long memberId, Pageab
}

@Transactional
public QuestionDetailResponse updateQuestion(Long questionId, String content) {
public QuestionDetailResponse updateQuestion(Long questionId, QuestionUpdateRequest request) {
Question question = questionRepository.findById(questionId)
.orElseThrow(() -> new BusinessException(QuestionError.NO_EXIST_QUESTION));
question.updateContent(content);
question.updateContent(question.getContent());
question.setNickname(request.getNickname()); // 닉네임 업데이트
Question updatedQuestion = questionRepository.save(question);

//firebaseNotificationService.notifyNewQuestion(updatedQuestion.getSender(), updatedQuestion); // 파이어베이스 메세지 송신

return questionMapper.toDomain(updatedQuestion);
}

Expand Down

0 comments on commit 499a822

Please sign in to comment.