Skip to content

Commit

Permalink
fix: 발송 옵션 선택하지 못한 편지를 위한 sendOptionId 컬럼 추가 #3
Browse files Browse the repository at this point in the history
  • Loading branch information
623nana committed Aug 19, 2021
1 parent b49aef7 commit 03bb2b3
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public class LetterRequest {
private Sticker sticker;

private Long questionId;

private Long sendOptionId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.nexters.covid.letter.domain.State;
import com.nexters.covid.letter.domain.Sticker;
import com.nexters.covid.letter.domain.question.Question;
import com.nexters.covid.letter.domain.sendoption.SendOption;
import java.time.LocalDateTime;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -40,9 +41,9 @@ public LetterResponse(Letter source) {
this.contents = decodeContents(source.getContents());
}

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

public LetterResponse(Letter source, Question question) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/nexters/covid/letter/domain/Letter.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class Letter extends BaseEntity {

private Long questionId;

private Long sendOptionId;

private String encryptedId;

public Letter(LetterRequest request, User user) {
Expand All @@ -59,6 +61,7 @@ public Letter(LetterRequest request, User user) {
this.state = State.PENDING;
this.sticker = request.getSticker();
this.questionId = request.getQuestionId();
this.sendOptionId = request.getSendOptionId();
this.encryptedId = generateEncryptedId();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.nexters.covid.letter.domain.sendoption;

import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface SendOptionRepository extends JpaRepository<SendOption, Long> {

@Query("select o from SendOption o join fetch o.questions")
List<SendOption> findAllJoinFetch();

Optional<SendOption> findSendOptionById(Long sendOptionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,13 @@ public class LetterService {
public List<LetterResponse> findLettersByEmail(String email) {
return letterRepository.findLettersByEmailOrderByCreatedDateDesc(email)
.stream()
.map(l -> new LetterResponse(l, findSendOptionByQuestionId(l.getQuestionId())))
.map(l -> new LetterResponse(l, findSendOptionById(l.getSendOptionId())))
.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()
public SendOption findSendOptionById(Long sendOptionId) {
return sendOptionRepository.findSendOptionById(sendOptionId)
.orElseThrow(() -> new IllegalArgumentException("발송 옵션이 존재하지 않습니다."));
}

Expand Down
21 changes: 11 additions & 10 deletions src/main/resources/data-h2.sql
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
insert into user (email, identifier, name) values ('email', 'identifier', 'name');
insert into user (email, identifier, name) values ('[email protected]', '1', '김아무개');

insert into letter (contents, encrypted_id, letter_to, email, sticker, question_id, state, title, user_id, created_date)
values ('64K07Jqp7JqU7Jqp7Jqp7Jqp', 'ENCRYPTED', 'LETTER_TO', 'email', 'HAPPY', 1 , 'PENDING', 'TITLE', 1, '2021-08-11T12:09:59.342635');
insert into letter (contents, encrypted_id, letter_to, email, sticker, question_id, state, title, user_id, created_date)
values ('64K07Jqp7JqU7Jqp7Jqp7Jqp', 'ENCRYPTED1', 'LETTER_TO1', 'email', 'BLUE', 2, 'PENDING', 'TITLE1', 1, '2021-08-09T12:09:59.342635');
insert into letter (contents, encrypted_id, letter_to, email, sticker, question_id, state, title, user_id, created_date)
values ('64K07Jqp7JqU7Jqp7Jqp7Jqp', 'ENCRYPTED1', 'LETTER_TO1', 'email', 'BLUE', 8, 'PENDING', 'TITLE1', 1, '2021-08-04T12:09:59.342635');
insert into letter (contents, encrypted_id, letter_to, email, sticker, question_id, send_option_id, state, title, user_id, created_date)
values ('64K07Jqp7JqU7Jqp7Jqp7Jqp', 'ENCRYPTED', 'LETTER_TO', 'email', 'HAPPY', 1 , 1, 'PENDING', 'TITLE', 1, '2021-08-11T12:09:59.342635');
insert into letter (contents, encrypted_id, letter_to, email, sticker, question_id, send_option_id, state, title, user_id, created_date)
values ('64K07Jqp7JqU7Jqp7Jqp7Jqp', 'ENCRYPTED1', 'LETTER_TO1', 'email', 'BLUE', 2, 1, 'PENDING', 'TITLE1', 1, '2021-08-09T12:09:59.342635');
insert into letter (contents, encrypted_id, letter_to, email, sticker, question_id, send_option_id, state, title, user_id, created_date)
values ('64K07Jqp7JqU7Jqp7Jqp7Jqp', 'ENCRYPTED2', 'LETTER_TO1', 'email', 'BLUE', 8, 2, 'PENDING', 'TITLE1', 1, '2021-08-04T12:09:59.342635');

insert into letter (contents, encrypted_id, letter_to, email, sticker, question_id, state, title, user_id)
values ('CONTENTS2', 'ENCRYPTED2', 'LETTER_TO2', '[email protected]', 'BLUE', 2, 'PENDING', 'TITLE2', 2);
insert into letter (contents, encrypted_id, letter_to, email, sticker, question_id, state, title, user_id)
values ('CONTENTS3', 'ENCRYPTED3', 'LETTER_TO3', '[email protected]', 'BLUE', 2, 'PENDING', 'TITLE3', 2);
insert into letter (contents, encrypted_id, letter_to, email, sticker, question_id, send_option_id, state, title, user_id)
values ('CONTENTS2', 'ENCRYPTED2', 'LETTER_TO2', '[email protected]', 'BLUE', 2, 7, 'PENDING', 'TITLE2', 2);
insert into letter (contents, encrypted_id, letter_to, email, sticker, question_id, send_option_id, state, title, user_id)
values ('CONTENTS3', 'ENCRYPTED3', 'LETTER_TO3', '[email protected]', 'BLUE', 2, 7, 'PENDING', 'TITLE3', 2);

-- 발송 옵션
insert into send_option (text, covid_stat) values ('실내에서 마스크를 벗을 수 있을 때', 1000);
Expand All @@ -20,6 +20,7 @@ insert into send_option (text, covid_stat) values ('하루 확진자 수 100명
insert into send_option (text, covid_stat) values ('백신 접종율이 50% 이상일 때', 0);
insert into send_option (text, covid_stat) values ('코로나 종식 선언할 때', 0);
insert into send_option (text, covid_stat) values ('공통 질문', 0);
insert into send_option (text, covid_stat) values ('미선택', 0);

-- 1. 질문 실내에서 마스크를 벗을 수 있을 때
insert into question (text, send_option_id) values ('코로나가 끝나면 누구와 어떤 공간을 가장 가고 싶나요?', 1);
Expand Down

0 comments on commit 03bb2b3

Please sign in to comment.