Skip to content

Commit

Permalink
feat: 부치지 못한 편지/전체 편지 조회 분기 처리 & SendOption 편지 테이블과 단방향 관계 설정 #3
Browse files Browse the repository at this point in the history
  • Loading branch information
623nana committed Aug 21, 2021
1 parent a13ec24 commit 2910f3d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 28 deletions.
12 changes: 8 additions & 4 deletions src/main/java/com/nexters/covid/letter/api/LetterController.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
Expand All @@ -23,8 +24,9 @@ public class LetterController {
private final LetterService letterService;

@GetMapping("/letters")
public BaseResponse<List<LetterResponse>> findLettersByEmail(Authentication authentication) {
List<LetterResponse> letters = letterService.findLettersByEmail(authentication.getName());
public BaseResponse<List<LetterResponse>> findLettersByEmail(Authentication authentication,
@RequestParam("unposted") boolean unposted) {
List<LetterResponse> letters = letterService.findLettersByEmail(authentication.getName(), unposted);
return new BaseResponse<>(200, 0, "", letters);
}

Expand Down Expand Up @@ -55,13 +57,15 @@ public BaseResponse<LetterResponse> findLetterByEncryptedId(
}

@PutMapping("/letters/{encryptedId}/state")
public BaseResponse<LetterResponse> updateLetterState(@PathVariable("encryptedId") String encryptedId) {
public BaseResponse<LetterResponse> updateLetterState(
@PathVariable("encryptedId") String encryptedId) {
LetterResponse letter = letterService.updateLetterState(encryptedId);
return new BaseResponse<>(200, 0, "", letter);
}

@PutMapping("/letters/{encryptedId}")
public BaseResponse<LetterResponse> updateLetter(@PathVariable("encryptedId") String encryptedId, @RequestBody LetterRequest letterRequest) {
public BaseResponse<LetterResponse> updateLetter(@PathVariable("encryptedId") String encryptedId,
@RequestBody LetterRequest letterRequest) {
LetterResponse letter = letterService.updateLetter(encryptedId, letterRequest);
return new BaseResponse<>(200, 0, "", letter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@ public class LetterRequest {
private Long questionId;

private Long sendOptionId;

public Long markLetterSendOptionId() {
if (sendOptionId == null) {
return 7L;
}
return sendOptionId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ public class LetterResponse {
public LetterResponse(Letter source) {
copyProperties(source, this);
this.contents = decodeContents(source.getContents());
}

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

public LetterResponse(Letter source, Question question) {
Expand Down
25 changes: 12 additions & 13 deletions src/main/java/com/nexters/covid/letter/domain/Letter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nexters.covid.base.BaseEntity;
import com.nexters.covid.letter.api.dto.LetterRequest;
import com.nexters.covid.letter.domain.sendoption.SendOption;
import com.nexters.covid.user.domain.User;
import java.util.UUID;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand Down Expand Up @@ -49,11 +52,13 @@ public class Letter extends BaseEntity {

private Long questionId;

private Long sendOptionId;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "send_option_id")
private SendOption sendOption;

private String encryptedId;

public Letter(LetterRequest request, User user) {
public Letter(LetterRequest request, User user, SendOption sendOption) {
this.user = user;
this.letterTo = request.getEmail();
this.title = request.getTitle();
Expand All @@ -62,7 +67,7 @@ public Letter(LetterRequest request, User user) {
this.state = State.PENDING;
this.sticker = request.getSticker();
this.questionId = request.getQuestionId();
this.sendOptionId = markLetterSendOptionId(request.getSendOptionId());
this.sendOption = sendOption;
this.encryptedId = generateEncryptedId();
}

Expand All @@ -84,17 +89,11 @@ public String decodeContents() {
return new String(decodeBase64(this.contents));
}

public Long markLetterSendOptionId(Long sendOptionId) {
if (sendOptionId == null) {
return 7L;
}
return sendOptionId;
public void updateLetterSendOption(SendOption sendOption) {
this.sendOption = sendOption;
}

public void updateLetterSendOption(Long sendOptionId) {
if (this.sendOptionId != 7) {
throw new RuntimeException("발송 옵션이 이미 설정되어 있습니다.");
}
this.sendOptionId = sendOptionId;
public boolean unpostedSendOption() {
return sendOption.isUnpostedLetter();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@

import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.EntityGraph.EntityGraphType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface LetterRepository extends JpaRepository<Letter, Long> {

List<Letter> findLettersByEmailOrderByCreatedDateDesc(String email);
@EntityGraph(attributePaths = "sendOption", type = EntityGraphType.FETCH)
List<Letter> findLettersByEmailOrderByCreatedDateDesc(@Param("email") String email);

@EntityGraph(attributePaths = "sendOption", type = EntityGraphType.FETCH)
Optional<Letter> findLetterByEncryptedId(String encryptedId);

List<Letter> findLetterByState(State state);

List<Letter> findLettersBySendOptionIdAndState(Long sendOptionId, State state);
@Query("select l from Letter l where l.sendOption.id = :sendOptionId and l.state = :state")
List<Letter> findLettersBySendOptionIdAndState(@Param("sendOptionId") Long sendOptionId, @Param("state") State state);

@Modifying
@Query("UPDATE Letter l set l.state = 'SEND' WHERE l.encryptedId = :encryptedId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ public class SendOption {

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

public boolean isUnpostedLetter() {
return id == 7L;
}
}
16 changes: 12 additions & 4 deletions src/main/java/com/nexters/covid/letter/service/LetterService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@ public class LetterService {
private final UserRepository userRepository;

@Transactional(readOnly = true)
public List<LetterResponse> findLettersByEmail(String email) {
public List<LetterResponse> findLettersByEmail(String email, boolean unposted) {
if (unposted) {
return letterRepository.findLettersByEmailOrderByCreatedDateDesc(email)
.stream()
.filter(Letter::unpostedSendOption)
.map(LetterResponse::new)
.collect(Collectors.toList());
}
return letterRepository.findLettersByEmailOrderByCreatedDateDesc(email)
.stream()
.map(l -> new LetterResponse(l, findSendOptionById(l.getSendOptionId())))
.map(LetterResponse::new)
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -63,8 +70,9 @@ public List<QuestionResponse> findQuestionsByOptionId(Long optionId) {
public LetterResponse saveLetter(LetterRequest letterRequest) {
User user = userRepository.findUserByEmail(letterRequest.getEmail())
.orElseThrow(() -> new RuntimeException("사용자가 없습니다."));
SendOption option = findSendOptionById(letterRequest.markLetterSendOptionId());

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

return new LetterResponse(letter);
}
Expand Down Expand Up @@ -93,7 +101,7 @@ public LetterResponse updateLetterState(String encryptedId) {
public LetterResponse updateLetter(String encryptedId, LetterRequest letterRequest) {
Letter letter = letterRepository.findLetterByEncryptedId(encryptedId)
.map(l -> {
l.updateLetterSendOption(letterRequest.getSendOptionId());
l.updateLetterSendOption(findSendOptionById(letterRequest.getSendOptionId()));
return l;
}).orElseThrow(() -> new IllegalArgumentException("해당 ID의 편지가 없습니다."));

Expand Down

0 comments on commit 2910f3d

Please sign in to comment.