Skip to content

Commit

Permalink
feat: 편지 최초 상세 조회 시 상태 DISPLAYED 업데이트 기능 #3
Browse files Browse the repository at this point in the history
  • Loading branch information
623nana committed Aug 17, 2021
1 parent 50054b1 commit b49aef7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

Expand Down Expand Up @@ -52,4 +53,10 @@ public BaseResponse<LetterResponse> findLetterByEncryptedId(
LetterResponse letter = letterService.findLetterByEncryptedId(encryptedId);
return new BaseResponse<>(200, 0, "", letter);
}

@PutMapping("/letters/{encryptedId}")
public BaseResponse<LetterResponse> updateLetterState(@PathVariable("encryptedId") String encryptedId) {
LetterResponse letter = letterService.updateLetterState(encryptedId);
return new BaseResponse<>(200, 0, "", letter);
}
}
6 changes: 6 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 @@ -69,4 +69,10 @@ private String generateEncryptedId() {
private String encodeContents(String contents) {
return encodeBase64String(contents.getBytes());
}

public void updateLetterState() {
if (this.state == State.SEND) {
this.state = State.DISPLAYED;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public interface LetterRepository extends JpaRepository<Letter, Long> {

Optional<Letter> findLetterByEncryptedId(String encryptedId);

List<Letter> findLetterByState (State state);
List<Letter> findLetterByState(State state);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,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)
return questionRepository.findQuestionsBySendOptionIdEqualsOrSendOptionIdEquals(optionId,
Constant.COMMON_SEND_OPTION_ID)
.stream()
.map(QuestionResponse::new)
.collect(Collectors.toList());
Expand All @@ -82,4 +83,14 @@ public LetterResponse findLetterByEncryptedId(String encryptedId) {

return new LetterResponse(letter, question);
}

@Transactional
public LetterResponse updateLetterState(String encryptedId) {
Letter letter = letterRepository.findLetterByEncryptedId(encryptedId)
.map(l -> {
l.updateLetterState();
return l;
}).orElseThrow(() -> new IllegalArgumentException("해당 ID의 편지가 없습니다."));
return new LetterResponse(letter);
}
}

0 comments on commit b49aef7

Please sign in to comment.