Skip to content

Commit

Permalink
feat: 발송 옵션 조회 기능 #3
Browse files Browse the repository at this point in the history
  • Loading branch information
623nana committed Jul 27, 2021
1 parent f848320 commit 9be6a20
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 12 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/nexters/covid/letter/api/LetterController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
package com.nexters.covid.letter.api;

import com.nexters.covid.base.BaseResponse;
import com.nexters.covid.letter.api.dto.OptionResponse;
import com.nexters.covid.letter.service.LetterService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
public class LetterController {

private final LetterService letterService;

@GetMapping("/letter/option")
public BaseResponse<List<OptionResponse>> options() {
List<OptionResponse> options = letterService.options();
return new BaseResponse<>(200, 0, "", options);
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/nexters/covid/letter/api/dto/OptionResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.nexters.covid.letter.api.dto;

import static org.springframework.beans.BeanUtils.copyProperties;

import com.nexters.covid.letter.domain.Question;
import com.nexters.covid.letter.domain.SendOption;
import java.util.List;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class OptionResponse {

private String text;
private Long covidStat;
private List<Question> questions;

public OptionResponse(SendOption source) {
copyProperties(source, this);
this.questions = source.questions();
}
}
4 changes: 0 additions & 4 deletions src/main/java/com/nexters/covid/letter/domain/Letter.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ public class Letter extends BaseEntity {
@Enumerated(EnumType.STRING)
private Sticker sticker;

@Column(name = "option")
@Enumerated(EnumType.STRING)
private SendOption option;

private String answer;

private String questionId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface LetterRepository extends JpaRepository<Letter, Long> {

}
28 changes: 28 additions & 0 deletions src/main/java/com/nexters/covid/letter/domain/Question.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.nexters.covid.letter.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@NoArgsConstructor
public class Question {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne
@JsonIgnore
@JoinColumn(name = "send_option_id")
private SendOption sendOption;

private String text;
}
36 changes: 33 additions & 3 deletions src/main/java/com/nexters/covid/letter/domain/SendOption.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
package com.nexters.covid.letter.domain;

public enum SendOption {
// TODO: 회의 해서 정의 필요
A, B, C
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@Table(name = "send_option")
@NoArgsConstructor
public class SendOption {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String text;

private Long covidStat;

@OneToMany(mappedBy = "sendOption", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private final List<Question> questions = new ArrayList<>();

public List<Question> questions() {
return questions;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.nexters.covid.letter.domain;

import java.util.List;
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();
}
22 changes: 22 additions & 0 deletions src/main/java/com/nexters/covid/letter/service/LetterService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.nexters.covid.letter.service;

import com.nexters.covid.letter.api.dto.OptionResponse;
import com.nexters.covid.letter.domain.SendOptionRepository;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
public class LetterService {

private final SendOptionRepository sendOptionRepository;

public List<OptionResponse> options() {
return sendOptionRepository.findAllJoinFetch()
.stream()
.map(OptionResponse::new)
.collect(Collectors.toList());
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ spring:
enabled: true
jpa:
defer-datasource-initialization: true
show-sql: true
18 changes: 14 additions & 4 deletions src/main/resources/data-h2.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
insert into user (email, identifier, name) values ('email', 'identifier', 'name');

insert into letter (answer, contents, encrypted_id, letter_to, email, option, sticker, question_id, state, title, user_id)
values ('ANSWER', 'CONTENTS', 'ENCRYPTED', 'LETTER_TO', 'email', 'A', 'B', 'HAPPY', 'PENDING', 'TITLE', 1);
insert into letter (answer, contents, encrypted_id, letter_to, email, option, sticker, question_id, state, title, user_id)
values ('ANSWER1', 'CONTENTS1', 'ENCRYPTED1', 'LETTER_TO1', 'email', 'A', 'B', 'BLUE', 'PENDING', 'TITLE1', 1);
insert into letter (answer, contents, encrypted_id, letter_to, email, sticker, question_id, state, title, user_id)
values ('ANSWER', 'CONTENTS', 'ENCRYPTED', 'LETTER_TO', 'email', 'A', 'HAPPY', 'PENDING', 'TITLE', 1);
insert into letter (answer, contents, encrypted_id, letter_to, email, sticker, question_id, state, title, user_id)
values ('ANSWER1', 'CONTENTS1', 'ENCRYPTED1', 'LETTER_TO1', 'email', 'A', 'BLUE', 'PENDING', 'TITLE1', 1);

insert into send_option (text, covid_stat) values ('테스트', 1000);
insert into send_option (text, covid_stat) values ('테스트2', 2000);

insert into question (text, send_option_id) values ('테스트1에 1', 1);
insert into question (text, send_option_id) values ('테스트1에 2', 1);
insert into question (text, send_option_id) values ('테스트1에 3', 1);

insert into question (text, send_option_id) values ('테스트2에 1', 2);
insert into question (text, send_option_id) values ('테스트2에 2', 2);

0 comments on commit 9be6a20

Please sign in to comment.