Skip to content

Commit

Permalink
fix: 작성 편지 리스트 조회시 발송 옵션 텍스트도 같이 보이도록 수정 #3
Browse files Browse the repository at this point in the history
  • Loading branch information
623nana committed Aug 11, 2021
1 parent e3df571 commit 24032ed
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ public class LetterResponse {

private String encryptedId;

private String sendOptionText;

private LocalDateTime createdDate;

public LetterResponse(Letter source) {
copyProperties(source, this);
this.contents = decodeContents(source.getContents());
}

public LetterResponse(Letter source, String sendOptionText) {
this(source);
this.sendOptionText = sendOptionText;
}

private String decodeContents(String contents) {
return new String(decodeBase64(contents));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ public class Question {
private SendOption sendOption;

private String text;

public boolean isMatch(Long questionId) {
return id.equals(questionId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ public class SendOption {
@OneToMany(mappedBy = "sendOption", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private final List<Question> questions = new ArrayList<>();

public List<Question> questions() {
return questions;
public boolean isMatchQuestion(Long questionId) {
for (Question question : questions) {
if (question.isMatch(questionId)) {
return true;
}
}
return false;
}
}
17 changes: 15 additions & 2 deletions src/main/java/com/nexters/covid/letter/service/LetterService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.nexters.covid.letter.domain.Letter;
import com.nexters.covid.letter.domain.LetterRepository;
import com.nexters.covid.letter.domain.question.QuestionRepository;
import com.nexters.covid.letter.domain.sendoption.SendOption;
import com.nexters.covid.letter.domain.sendoption.SendOptionRepository;
import com.nexters.covid.user.domain.User;
import com.nexters.covid.user.domain.UserRepository;
Expand All @@ -30,10 +31,21 @@ public class LetterService {
public List<LetterResponse> findLettersByEmail(String email) {
return letterRepository.findLettersByEmailOrderByCreatedDateDesc(email)
.stream()
.map(LetterResponse::new)
.map(l -> new LetterResponse(l, findSendOptionByQuestionId(l.getQuestionId())))
.collect(Collectors.toList());
}

@Transactional(readOnly = true)
public String findSendOptionByQuestionId(Long questionId) {
List<SendOption> options = sendOptionRepository.findAllJoinFetch();

return options.stream()
.filter(o -> o.isMatchQuestion(questionId))
.map(SendOption::getText)
.findAny()
.orElseThrow(() -> new IllegalArgumentException("발송 옵션이 존재하지 않습니다."));
}

@Transactional(readOnly = true)
public List<OptionResponse> findAllOptions() {
return sendOptionRepository.findAll()
Expand All @@ -45,7 +57,8 @@ public List<OptionResponse> findAllOptions() {
@Transactional(readOnly = true)
public List<QuestionResponse> findQuestionsByOptionId(Long optionId) {
return questionRepository
.findQuestionsBySendOptionIdEqualsOrSendOptionIdEquals(optionId, Constant.COMMON_SEND_OPTION_ID)
.findQuestionsBySendOptionIdEqualsOrSendOptionIdEquals(optionId,
Constant.COMMON_SEND_OPTION_ID)
.stream()
.map(QuestionResponse::new)
.collect(Collectors.toList());
Expand Down

0 comments on commit 24032ed

Please sign in to comment.