Skip to content

Commit

Permalink
refactor: #697 질문과 답변 조회 시 삭제된 답변은 보이지 않도록 수정 (#698)
Browse files Browse the repository at this point in the history
* test: 질문이 삭제된 경우 조회되지 않음을 나타내는 테스트 추가

* refactor: 답변이 삭제된 경우 null이 반환되도록 수정
  • Loading branch information
JJ503 authored Oct 15, 2023
1 parent 7507116 commit d8096cf
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static ReadQnaResponse from(final ReadQnaDto readQnaDto) {
}

private static ReadAnswerResponse processReadAnswerResponse(final ReadAnswerDto readAnswerDto) {
if (readAnswerDto == null) {
if (readAnswerDto.isDeleted()) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ public record ReadAnswerDto(
Long id,
ReadUserInQnaDto writerDto,
String content,
LocalDateTime createdTime
LocalDateTime createdTime,
boolean isDeleted
) {

public static ReadAnswerDto from(final Answer answer, final User writer) {
return new ReadAnswerDto(
answer.getId(),
ReadUserInQnaDto.from(writer),
answer.getContent(),
answer.getCreatedTime()
answer.getCreatedTime(),
answer.isDeleted()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public record ReadQuestionDto(
ReadUserInQnaDto readUserInQnaDto,
String content,
LocalDateTime createdTime,
boolean isDeleted,
boolean isQuestioner
) {

Expand All @@ -21,6 +22,7 @@ public static ReadQuestionDto of(final Question question, final User user) {
ReadUserInQnaDto.from(question.getWriter()),
question.getContent(),
question.getCreatedTime(),
question.isDeleted(),
isWriter(question, user)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public class AuctionQuestionControllerFixture extends CommonControllerSliceTest
"12346",
false
);
protected ReadQuestionDto 질문_정보_dto1 = new ReadQuestionDto(1L, 질문자_정보_dto, "질문1", LocalDateTime.now(), false);
protected ReadQuestionDto 질문_정보_dto2 = new ReadQuestionDto(2L, 질문자_정보_dto, "질문2", LocalDateTime.now(), false);
protected ReadAnswerDto 답변_정보_dto1 = new ReadAnswerDto(1L, 판매자_정보_dto, "답변1", LocalDateTime.now());
protected ReadAnswerDto 답변_정보_dto2 = new ReadAnswerDto(2L, 판매자_정보_dto, "답변1", LocalDateTime.now());
protected ReadQuestionDto 질문_정보_dto1 = new ReadQuestionDto(1L, 질문자_정보_dto, "질문1", LocalDateTime.now(), false, false);
protected ReadQuestionDto 질문_정보_dto2 = new ReadQuestionDto(2L, 질문자_정보_dto, "질문2", LocalDateTime.now(), false, false);
protected ReadAnswerDto 답변_정보_dto1 = new ReadAnswerDto(1L, 판매자_정보_dto, "답변1", LocalDateTime.now(), false);
protected ReadAnswerDto 답변_정보_dto2 = new ReadAnswerDto(2L, 판매자_정보_dto, "답변1", LocalDateTime.now(), false);
private ReadQnaDto 질문과_답변_정보_dto1 = new ReadQnaDto(질문_정보_dto1, 답변_정보_dto1);
private ReadQnaDto 질문과_답변_정보_dto2 = new ReadQnaDto(질문_정보_dto2, 답변_정보_dto2);
protected ReadQnasDto 질문과_답변_정보들_dto =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ void setUp() {
final ReadUserInQnaDto 판매자_정보_dto = ReadUserInQnaDto.from(판매자);
final ReadUserInQnaDto 질문자_정보_dto = ReadUserInQnaDto.from(질문자);
final ReadUserInQnaDto 두번째_질문자_정보_dto = ReadUserInQnaDto.from(두번째_질문을_작성한_사용자);
질문_정보_dto1 = new ReadQuestionDto(질문.getId(), 질문자_정보_dto, 질문.getContent(), 질문.getCreatedTime(), false);
질문_정보_dto2 = new ReadQuestionDto(질문2.getId(), 두번째_질문자_정보_dto, 질문2.getContent(), 질문2.getCreatedTime(), true);
질문_정보_dto3 = new ReadQuestionDto(질문3.getId(), 질문자_정보_dto, 질문3.getContent(), 질문3.getCreatedTime(), false);
답변_정보_dto1 = new ReadAnswerDto(답변1.getId(), 판매자_정보_dto, 답변1.getContent(), 답변1.getCreatedTime());
답변_정보_dto2 = new ReadAnswerDto(답변2.getId(), 판매자_정보_dto, 답변2.getContent(), 답변2.getCreatedTime());
질문_정보_dto1 = new ReadQuestionDto(질문.getId(), 질문자_정보_dto, 질문.getContent(), 질문.getCreatedTime(), false, false);
질문_정보_dto2 = new ReadQuestionDto(질문2.getId(), 두번째_질문자_정보_dto, 질문2.getContent(), 질문2.getCreatedTime(), false, true);
질문_정보_dto3 = new ReadQuestionDto(질문3.getId(), 질문자_정보_dto, 질문3.getContent(), 질문3.getCreatedTime(), false, false);
답변_정보_dto1 = new ReadAnswerDto(답변1.getId(), 판매자_정보_dto, 답변1.getContent(), 답변1.getCreatedTime(), false);
답변_정보_dto2 = new ReadAnswerDto(답변2.getId(), 판매자_정보_dto, 답변2.getContent(), 답변2.getCreatedTime(), false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,19 @@ class JpaQuestionRepositoryTest extends JpaQuestionRepositoryFixture {
softAssertions.assertThat(actual.get(2).getAnswer()).isNull();
});
}

@Test
void 경매_아이디를_통해_질문과_답변_조회시_질문이_삭제된_경우_답변이_있더라도_함꼐_조회되지_않는다() {
// when
final List<Question> actual = questionRepository.findAllByAuctionId(질문이_3개_답변이_2개중_첫번째_질문이_삭제된_경매.getId());

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(actual).hasSize(2);
softAssertions.assertThat(actual.get(0)).isEqualTo(삭제되지_않은_질문2);
softAssertions.assertThat(actual.get(0).getAnswer()).isEqualTo(삭제되지_않은_질문의_답변2);
softAssertions.assertThat(actual.get(1)).isEqualTo(답변이_없는_질문3);
softAssertions.assertThat(actual.get(1).getAnswer()).isNull();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,21 @@ public class JpaQuestionRepositoryFixture {

protected Auction 경매;
protected Auction 질문이_3개_답변이_2개인_경매;
protected Auction 질문이_3개_답변이_2개중_첫번째_질문이_삭제된_경매;
protected Auction 질문이_3개_답변이_2개중_첫번째_답변이_삭제된_경매;
protected User 질문자;
protected String 질문_내용 = "궁금한 점이 있어요.";
protected Question 질문1;
protected Question 질문2;
protected Question 질문3;
protected Question 삭제된_질문1;
protected Question 삭제되지_않은_질문2;
protected Question 답변이_없는_질문3;
protected Question 삭제된_질문;
protected Answer 답변1;
protected Answer 답변2;
protected Answer 삭제된_질문의_답변1;
protected Answer 삭제되지_않은_질문의_답변2;

@BeforeEach
void setUp() {
Expand All @@ -74,6 +81,22 @@ void setUp() {
.startPrice(new Price(1_000))
.closingTime(LocalDateTime.now())
.build();
질문이_3개_답변이_2개중_첫번째_질문이_삭제된_경매 = Auction.builder()
.seller(판매자)
.title("경매 상품")
.description("이것은 경매 상품입니다.")
.bidUnit(new BidUnit(1_000))
.startPrice(new Price(1_000))
.closingTime(LocalDateTime.now())
.build();
질문이_3개_답변이_2개중_첫번째_답변이_삭제된_경매 = Auction.builder()
.seller(판매자)
.title("경매 상품")
.description("이것은 경매 상품입니다.")
.bidUnit(new BidUnit(1_000))
.startPrice(new Price(1_000))
.closingTime(LocalDateTime.now())
.build();
질문자 = User.builder()
.name("질문자")
.profileImage(프로필_이미지)
Expand All @@ -89,13 +112,22 @@ void setUp() {
질문1.addAnswer(답변1);
질문2.addAnswer(답변2);

삭제된_질문1 = new Question(질문이_3개_답변이_2개중_첫번째_질문이_삭제된_경매, 질문자, "질문1");
삭제되지_않은_질문2 = new Question(질문이_3개_답변이_2개중_첫번째_질문이_삭제된_경매, 질문자, "질문2");
답변이_없는_질문3 = new Question(질문이_3개_답변이_2개중_첫번째_질문이_삭제된_경매, 질문자, "질문3");
삭제된_질문의_답변1 = new Answer("삭제된 질문의 답변");
삭제되지_않은_질문의_답변2 = new Answer("삭제되지 않은 질문의 답변2");
삭제된_질문1.addAnswer(삭제된_질문의_답변1);
삭제된_질문1.delete();
삭제되지_않은_질문2.addAnswer(삭제되지_않은_질문의_답변2);

삭제된_질문 = new Question(경매, 질문자, "질문3");
삭제된_질문.delete();

userRepository.saveAll(List.of(판매자, 질문자));
auctionRepository.saveAll(List.of(경매, 질문이_3개_답변이_2개인_경매));
questionRepository.saveAll(List.of(질문1, 질문2, 질문3, 삭제된_질문));
answerRepository.saveAll(List.of(답변1, 답변2));
auctionRepository.saveAll(List.of(경매, 질문이_3개_답변이_2개인_경매, 질문이_3개_답변이_2개중_첫번째_질문이_삭제된_경매, 질문이_3개_답변이_2개중_첫번째_답변이_삭제된_경매));
questionRepository.saveAll(List.of(질문1, 질문2, 질문3, 삭제된_질문, 삭제된_질문1, 삭제되지_않은_질문2, 답변이_없는_질문3));
answerRepository.saveAll(List.of(답변1, 답변2, 삭제된_질문의_답변1, 삭제되지_않은_질문의_답변2));

em.flush();
em.clear();
Expand Down

0 comments on commit d8096cf

Please sign in to comment.