Skip to content

Commit

Permalink
feat: 작성한 편지 조회 기능 & 편지 작성 기능 #3
Browse files Browse the repository at this point in the history
  • Loading branch information
623nana committed Aug 6, 2021
1 parent 30c0846 commit b238686
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void configure(WebSecurity web) throws Exception {
, "/configuration/security"
, "/webjars/**"
, "/login"
, "/letters/{\\d+}"
, "/redirect");
web.ignoring().requestMatchers(PathRequest.toStaticResources().atCommonLocations());
web.ignoring().requestMatchers(PathRequest.toH2Console());
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/com/nexters/covid/letter/api/LetterController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.nexters.covid.letter.api;

import com.nexters.covid.base.BaseResponse;
import com.nexters.covid.letter.api.dto.LetterRequest;
import com.nexters.covid.letter.api.dto.LetterResponse;
import com.nexters.covid.letter.api.dto.OptionResponse;
import com.nexters.covid.letter.api.dto.QuestionResponse;
Expand All @@ -10,6 +11,7 @@
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
Expand All @@ -31,8 +33,22 @@ public BaseResponse<List<OptionResponse>> findAllOptions() {
}

@GetMapping("/letters/options/{optionId}/questions")
public BaseResponse<List<QuestionResponse>> findQuestionsByOptionId(@PathVariable("optionId") Long optionId) {
public BaseResponse<List<QuestionResponse>> findQuestionsByOptionId(
@PathVariable("optionId") Long optionId) {
List<QuestionResponse> questions = letterService.findQuestionsByOptionId(optionId);
return new BaseResponse<>(200, 0, "", questions);
}

@PostMapping("/letters")
public BaseResponse<LetterResponse> saveLetter(LetterRequest letterRequest) {
LetterResponse letter = letterService.saveLetter(letterRequest);
return new BaseResponse<>(200, 0, "", letter);
}

@GetMapping("/letters/{encryptedId}")
public BaseResponse<LetterResponse> findLetterByEncryptedId(
@PathVariable("encryptedId") String encryptedId) {
LetterResponse letter = letterService.findLetterByEncryptedId(encryptedId);
return new BaseResponse<>(200, 0, "", letter);
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/nexters/covid/letter/api/dto/LetterRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.nexters.covid.letter.api.dto;

import com.nexters.covid.letter.domain.Sticker;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class LetterRequest {

private String title;

private String contents;

private String email;

private Sticker sticker;

private Long questionId;
}
20 changes: 18 additions & 2 deletions src/main/java/com/nexters/covid/letter/domain/Letter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nexters.covid.base.BaseEntity;
import com.nexters.covid.letter.api.dto.LetterRequest;
import com.nexters.covid.user.domain.User;
import java.util.UUID;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
Expand Down Expand Up @@ -42,9 +44,23 @@ public class Letter extends BaseEntity {
@Enumerated(EnumType.STRING)
private Sticker sticker;

private String answer;

private Long questionId;

private String encryptedId;

public Letter(LetterRequest request, User user) {
this.user = user;
this.letterTo = request.getEmail();
this.title = request.getTitle();
this.contents = request.getContents();
this.email = user.getEmail();
this.state = State.PENDING;
this.sticker = request.getSticker();
this.questionId = request.getQuestionId();
this.encryptedId = generateEncryptedId();
}

private String generateEncryptedId() {
return UUID.randomUUID().toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.nexters.covid.letter.domain;

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

public interface LetterRepository extends JpaRepository<Letter, Long> {

List<Letter> findLettersByEmail(String email);
List<Letter> findLettersByEmailOrderByCreatedDateDesc(String email);

Optional<Letter> findLetterByEncryptedId(String encryptedId);
}
31 changes: 28 additions & 3 deletions src/main/java/com/nexters/covid/letter/service/LetterService.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.nexters.covid.letter.service;

import com.nexters.covid.letter.api.dto.LetterRequest;
import com.nexters.covid.letter.api.dto.LetterResponse;
import com.nexters.covid.letter.api.dto.OptionResponse;
import com.nexters.covid.letter.api.dto.QuestionResponse;
import com.nexters.covid.letter.domain.Letter;
import com.nexters.covid.letter.domain.LetterRepository;
import com.nexters.covid.letter.domain.QuestionRepository;
import com.nexters.covid.letter.domain.SendOptionRepository;
import com.nexters.covid.user.domain.User;
import com.nexters.covid.user.domain.UserRepository;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
Expand All @@ -16,13 +20,16 @@
@Service
public class LetterService {

private static final Long COMMON_SEND_OPTION_ID = 3L;

private final LetterRepository letterRepository;
private final SendOptionRepository sendOptionRepository;
private final QuestionRepository questionRepository;
private final UserRepository userRepository;

@Transactional(readOnly = true)
public List<LetterResponse> findLettersByEmail(String email) {
return letterRepository.findLettersByEmail(email)
return letterRepository.findLettersByEmailOrderByCreatedDateDesc(email)
.stream()
.map(LetterResponse::new)
.collect(Collectors.toList());
Expand All @@ -37,10 +44,28 @@ public List<OptionResponse> findAllOptions() {
}

@Transactional(readOnly = true)
public List<QuestionResponse> findQuestionsByOptionId(Long questionId) {
return questionRepository.findQuestionsBySendOptionIdEqualsOrSendOptionIdEquals(questionId, 3L)
public List<QuestionResponse> findQuestionsByOptionId(Long optionId) {
return questionRepository
.findQuestionsBySendOptionIdEqualsOrSendOptionIdEquals(optionId, COMMON_SEND_OPTION_ID)
.stream()
.map(QuestionResponse::new)
.collect(Collectors.toList());
}

@Transactional
public LetterResponse saveLetter(LetterRequest letterRequest) {
User user = userRepository.findUserByEmail(letterRequest.getEmail())
.orElseThrow(() -> new RuntimeException("사용자가 없습니다."));

Letter letter = letterRepository.save(new Letter(letterRequest, user));

return new LetterResponse(letter);
}

@Transactional(readOnly = true)
public LetterResponse findLetterByEncryptedId(String encryptedId) {
Letter letter = letterRepository.findLetterByEncryptedId(encryptedId)
.orElseThrow(() -> new IllegalArgumentException("해당 ID의 편지가 없습니다."));
return new LetterResponse(letter);
}
}
8 changes: 4 additions & 4 deletions src/main/resources/data-h2.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
insert into user (email, identifier, name) values ('email', 'identifier', 'name');

insert into letter (answer, contents, encrypted_id, letter_to, email, sticker, question_id, state, title, user_id)
values ('ANSWER', 'CONTENTS', 'ENCRYPTED', 'LETTER_TO', 'email', 'HAPPY', 1 , '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', 'BLUE', 2, 'PENDING', 'TITLE1', 1);
insert into letter (contents, encrypted_id, letter_to, email, sticker, question_id, state, title, user_id)
values ('CONTENTS', 'ENCRYPTED', 'LETTER_TO', 'email', 'HAPPY', 1 , 'PENDING', 'TITLE', 1);
insert into letter (contents, encrypted_id, letter_to, email, sticker, question_id, state, title, user_id)
values ('CONTENTS1', 'ENCRYPTED1', 'LETTER_TO1', 'email', 'BLUE', 2, 'PENDING', 'TITLE1', 1);

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

0 comments on commit b238686

Please sign in to comment.