Skip to content

Commit 82a3da5

Browse files
authored
Merge pull request #195 from prgrms-web-devcourse-final-project/refactor/letter-temporary-save(WR9-112)
refactor: 답장만 임시저장 되도록 수정 (WR9-112)
2 parents b248509 + 5a1a3f0 commit 82a3da5

File tree

6 files changed

+28
-101
lines changed

6 files changed

+28
-101
lines changed

src/main/java/io/crops/warmletter/domain/letter/controller/LetterController.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ public ResponseEntity<BaseResponse<Void>> evaluateLetter(@PathVariable Long lett
8585
public ResponseEntity<BaseResponse<LetterResponse>> temporarySaveLetter(
8686
@PathVariable (name="letterId")Long letterId, @Valid @RequestBody TemporarySaveLetterRequest request) {
8787

88-
return ResponseEntity.status(HttpStatus.OK)
89-
.body(new BaseResponse<>(letterService.temporarySaveLetter(letterId, request),"임시 저장 완료 "));
88+
return ResponseEntity.ok(BaseResponse.of(letterService.temporarySaveLetter(letterId, request),"임시 저장 완료 "));
9089
}
9190

9291
/**

src/main/java/io/crops/warmletter/domain/letter/dto/request/TemporarySaveLetterRequest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class TemporarySaveLetterRequest {
1414

1515
private Long receiverId;
1616

17+
private Long matchingId;
18+
1719
private Long parentLetterId;
1820

1921
@NotBlank(message = "제목을 입력해주세요.")

src/main/java/io/crops/warmletter/domain/letter/entity/Letter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ public void updateLetterType(LetterType letterType) {
9292
}
9393

9494

95-
public void updateTemporarySave(Long receiverId, Long parentLetterId, Category category, String title, String content) {
95+
public void updateTemporarySave(Long receiverId, Long matchingId, Long parentLetterId, Category category, String title, String content) {
9696
this.receiverId = receiverId;
97+
this.matchingId = matchingId;
9798
this.parentLetterId = parentLetterId;
9899
this.category = category;
99100
this.title = title;

src/main/java/io/crops/warmletter/domain/letter/service/LetterService.java

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -188,36 +188,19 @@ public LetterResponse temporarySaveLetter(Long letterId, TemporarySaveLetterRequ
188188
Long writerId = authFacade.getCurrentUserId();
189189
String writerZipCode = authFacade.getZipCode();
190190

191-
if (letterId != null) {
192-
Letter letter = letterRepository.findByIdAndWriterId(letterId, writerId)
193-
.orElseThrow(LetterNotBelongException::new);
194-
195-
letter.updateTemporarySave(
196-
request.getReceiverId(),
197-
request.getParentLetterId(),
198-
request.getCategory(),
199-
request.getTitle(),
200-
request.getContent()
201-
);
202-
203-
return LetterResponse.fromEntity(letter, writerZipCode);
204-
}
205-
else {
206-
207-
Letter letter = Letter.builder()
208-
.writerId(1L)
209-
.letterType(LetterType.RANDOM)
210-
.category(request.getCategory())
211-
.title(request.getTitle())
212-
.content(request.getContent())
213-
.status(Status.SAVED)
214-
.fontType(request.getFontType())
215-
.paperType(request.getPaperType())
216-
.build();
217-
letterRepository.save(letter);
218-
219-
return LetterResponse.fromEntity(letter, writerZipCode);
220-
}
191+
Letter letter = letterRepository.findByIdAndWriterId(letterId, writerId)
192+
.orElseThrow(LetterNotBelongException::new);
193+
194+
letter.updateTemporarySave(
195+
request.getReceiverId(),
196+
request.getMatchingId(),
197+
request.getParentLetterId(),
198+
request.getCategory(),
199+
request.getTitle(),
200+
request.getContent()
201+
);
202+
203+
return LetterResponse.fromEntity(letter, writerZipCode);
221204
}
222205

223206
/**

src/main/resources/application.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ spring:
4141
scope: email
4242

4343
kakao:
44-
client-id: ${KAKAO_CLIENT_ID}
45-
client-secret: ${KAKAO_CLIENT_SECRET}
44+
client-id: ${PROD_KAKAO_CLIENT_ID}
45+
client-secret: ${PROD_KAKAO_CLIENT_SECRET}
4646
redirect-uri: "https://${PROD_SERVER_DOMAIN}/login/oauth2/code/kakao"
4747
authorization-grant-type: authorization_code
4848
client-authentication-method: client_secret_post

src/test/java/io/crops/warmletter/domain/letter/service/LetterServiceTest.java

Lines changed: 8 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -967,13 +967,15 @@ void temporarySaveExistingLetter_success() throws Exception {
967967
// given
968968
Long letterId = 1L;
969969
Long writerId = 1L;
970+
Long matchingId = 1L;
970971

971972
// Letter를 mock으로 생성
972973
Letter existingLetter = mock(Letter.class);
973974
// mock 객체에 필요한 getter 메서드 동작 정의
974975
when(existingLetter.getId()).thenReturn(letterId);
975976
when(existingLetter.getTitle()).thenReturn("임시 저장 제목");
976977
when(existingLetter.getContent()).thenReturn("임시 저장 내용");
978+
when(existingLetter.getMatchingId()).thenReturn(matchingId);
977979
when(existingLetter.getCategory()).thenReturn(Category.ETC);
978980
when(existingLetter.getFontType()).thenReturn(FontType.HIMCHAN);
979981
when(existingLetter.getPaperType()).thenReturn(PaperType.COMFORT);
@@ -987,6 +989,7 @@ void temporarySaveExistingLetter_success() throws Exception {
987989
Field fontField = TemporarySaveLetterRequest.class.getDeclaredField("fontType");
988990
Field paperTypeField = TemporarySaveLetterRequest.class.getDeclaredField("paperType");
989991
Field receiverIdField = TemporarySaveLetterRequest.class.getDeclaredField("receiverId");
992+
Field matchingIdField = TemporarySaveLetterRequest.class.getDeclaredField("matchingId");
990993
Field parentLetterIdField = TemporarySaveLetterRequest.class.getDeclaredField("parentLetterId");
991994

992995
titleField.setAccessible(true);
@@ -995,6 +998,7 @@ void temporarySaveExistingLetter_success() throws Exception {
995998
fontField.setAccessible(true);
996999
paperTypeField.setAccessible(true);
9971000
receiverIdField.setAccessible(true);
1001+
matchingIdField.setAccessible(true);
9981002
parentLetterIdField.setAccessible(true);
9991003

10001004
titleField.set(request, "임시 저장 제목");
@@ -1003,6 +1007,7 @@ void temporarySaveExistingLetter_success() throws Exception {
10031007
fontField.set(request, FontType.HIMCHAN);
10041008
paperTypeField.set(request, PaperType.COMFORT);
10051009
receiverIdField.set(request, null);
1010+
matchingIdField.set(request, matchingId);
10061011
parentLetterIdField.set(request, null);
10071012

10081013
// 서비스 메서드 모킹
@@ -1013,6 +1018,7 @@ void temporarySaveExistingLetter_success() throws Exception {
10131018
// void 메서드 모킹 (doNothing 사용)
10141019
doNothing().when(existingLetter).updateTemporarySave(
10151020
request.getReceiverId(),
1021+
request.getMatchingId(),
10161022
request.getParentLetterId(),
10171023
request.getCategory(),
10181024
request.getTitle(),
@@ -1028,6 +1034,7 @@ void temporarySaveExistingLetter_success() throws Exception {
10281034
() -> assertEquals(letterId, response.getLetterId()),
10291035
() -> assertEquals("임시 저장 제목", response.getTitle()),
10301036
() -> assertEquals("임시 저장 내용", response.getContent()),
1037+
() -> assertEquals(matchingId, response.getMatchingId()),
10311038
() -> assertEquals(Category.ETC, response.getCategory()),
10321039
() -> assertEquals(FontType.HIMCHAN, response.getFontType()),
10331040
() -> assertEquals(PaperType.COMFORT, response.getPaperType()),
@@ -1038,79 +1045,14 @@ void temporarySaveExistingLetter_success() throws Exception {
10381045
verify(letterRepository).findByIdAndWriterId(letterId, writerId);
10391046
verify(existingLetter).updateTemporarySave(
10401047
request.getReceiverId(),
1048+
request.getMatchingId(),
10411049
request.getParentLetterId(),
10421050
request.getCategory(),
10431051
request.getTitle(),
10441052
request.getContent()
10451053
);
10461054
}
10471055

1048-
1049-
@Test
1050-
@DisplayName("새 편지 임시 저장 성공 테스트")
1051-
void temporarySaveNewLetter_success() throws Exception {
1052-
// given
1053-
Long writerId = 1L;
1054-
Long newLetterId = 1L;
1055-
1056-
TemporarySaveLetterRequest request = new TemporarySaveLetterRequest();
1057-
Field titleField = TemporarySaveLetterRequest.class.getDeclaredField("title");
1058-
Field contentField = TemporarySaveLetterRequest.class.getDeclaredField("content");
1059-
Field categoryField = TemporarySaveLetterRequest.class.getDeclaredField("category");
1060-
Field fontField = TemporarySaveLetterRequest.class.getDeclaredField("fontType");
1061-
Field paperTypeField = TemporarySaveLetterRequest.class.getDeclaredField("paperType");
1062-
Field parentLetterIdField = TemporarySaveLetterRequest.class.getDeclaredField("parentLetterId");
1063-
Field receiverIdField = TemporarySaveLetterRequest.class.getDeclaredField("receiverId");
1064-
1065-
titleField.setAccessible(true);
1066-
contentField.setAccessible(true);
1067-
categoryField.setAccessible(true);
1068-
fontField.setAccessible(true);
1069-
paperTypeField.setAccessible(true);
1070-
parentLetterIdField.setAccessible(true);
1071-
receiverIdField.setAccessible(true);
1072-
1073-
titleField.set(request, "새 임시 저장 제목");
1074-
contentField.set(request, "새 임시 저장 내용");
1075-
categoryField.set(request, Category.ETC);
1076-
fontField.set(request, FontType.GYEONGGI);
1077-
paperTypeField.set(request, PaperType.PAPER);
1078-
parentLetterIdField.set(request, null);
1079-
receiverIdField.set(request, null);
1080-
1081-
// 서비스 메서드 모킹
1082-
when(authFacade.getCurrentUserId()).thenReturn(writerId);
1083-
when(authFacade.getZipCode()).thenReturn("12345");
1084-
1085-
// 중요: letterRepository.save() 메서드가 호출될 때 Letter 객체를 캡처하고 ID를 설정한 후 반환
1086-
doAnswer(invocation -> {
1087-
Letter letterToSave = invocation.getArgument(0);
1088-
// 저장 시점에 ID 설정
1089-
ReflectionTestUtils.setField(letterToSave, "id", newLetterId);
1090-
return letterToSave;
1091-
}).when(letterRepository).save(any(Letter.class));
1092-
1093-
// when
1094-
LetterResponse response = letterService.temporarySaveLetter(null, request);
1095-
1096-
// then
1097-
assertAll("새 편지 임시 저장 응답 검증",
1098-
() -> assertNotNull(response),
1099-
() -> assertEquals(newLetterId, response.getLetterId()),
1100-
() -> assertEquals("새 임시 저장 제목", response.getTitle()),
1101-
() -> assertEquals("새 임시 저장 내용", response.getContent()),
1102-
() -> assertEquals(Category.ETC, response.getCategory()),
1103-
() -> assertEquals(FontType.GYEONGGI, response.getFontType()),
1104-
() -> assertEquals(PaperType.PAPER, response.getPaperType()),
1105-
() -> assertEquals(Status.SAVED, response.getStatus()),
1106-
() -> assertNull(response.getParentLetterId()),
1107-
() -> assertEquals("12345", response.getZipCode())
1108-
);
1109-
1110-
verify(authFacade).getCurrentUserId();
1111-
verify(letterRepository).save(any(Letter.class));
1112-
}
1113-
11141056
@Test
11151057
@DisplayName("임시 저장 편지 삭제 성공")
11161058
void delete_temporarySaveLetter_success() throws Exception {

0 commit comments

Comments
 (0)