Skip to content

Commit 75c63d5

Browse files
authored
Merge pull request #202 from prgrms-web-devcourse-final-project/fix/temporary-letter(WR9-120)
Fix/temporary letter(wr9 120)
2 parents 54139bd + 75e9104 commit 75c63d5

File tree

6 files changed

+269
-166
lines changed

6 files changed

+269
-166
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,19 @@ public ResponseEntity<BaseResponse<Void>> evaluateLetter(@PathVariable Long lett
8181
/**
8282
* 임시저장 편지 생성
8383
*/
84-
@PostMapping("/letters/{letterId}/temporary-save")
84+
@PostMapping("/letters/temporary-save")
8585
public ResponseEntity<BaseResponse<LetterResponse>> temporarySaveLetter(
86-
@PathVariable (name="letterId")Long letterId, @Valid @RequestBody TemporarySaveLetterRequest request) {
86+
@Valid @RequestBody TemporarySaveLetterRequest request) {
8787

88-
return ResponseEntity.ok(BaseResponse.of(letterService.temporarySaveLetter(letterId, request),"임시 저장 완료 "));
88+
return ResponseEntity.ok(BaseResponse.of(letterService.temporarySaveLetter(request),"임시 저장 완료 "));
8989
}
9090

9191
/**
9292
* 임시저장 편지 삭제
9393
*/
9494
@DeleteMapping("/letters/{letterId}/temporary-save")
9595
public ResponseEntity<BaseResponse<Map<String,Long>>> deleteTemporarySaveLetter(@PathVariable Long letterId) {
96-
return ResponseEntity.status(HttpStatus.OK)
97-
.body(new BaseResponse<>(letterService.deleteTemporarySaveLetter(letterId),"임시 저장 편지 삭제 완료"));
96+
return ResponseEntity.ok(BaseResponse.of(letterService.deleteTemporarySaveLetter(letterId),"임시 저장 편지 삭제 완료"));
9897
}
9998

10099
/**

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@
44
import io.crops.warmletter.domain.letter.enums.FontType;
55
import io.crops.warmletter.domain.letter.enums.PaperType;
66
import jakarta.validation.constraints.NotBlank;
7+
import jakarta.validation.constraints.NotNull;
78
import lombok.*;
89

910
@Getter
10-
@Setter
1111
@NoArgsConstructor
1212
@AllArgsConstructor
1313
public class TemporarySaveLetterRequest {
1414

15+
private Long letterId;
16+
17+
@NotNull(message = "받는 사람을 설정해주세요.")
1518
private Long receiverId;
1619

20+
@NotNull(message = "매칭 번호를 설정해주세요.")
1721
private Long matchingId;
1822

23+
@NotNull(message = "상위 편지를 설정해주세요.")
1924
private Long parentLetterId;
2025

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

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,20 @@ public void updateLetterType(LetterType letterType) {
9191
this.letterType = letterType;
9292
}
9393

94-
95-
public void updateTemporarySave(Long receiverId, Long matchingId, Long parentLetterId, Category category, String title, String content) {
96-
this.receiverId = receiverId;
97-
this.matchingId = matchingId;
98-
this.parentLetterId = parentLetterId;
99-
this.category = category;
94+
public void updateTitle(String title) {
10095
this.title = title;
96+
}
97+
98+
public void updateContent(String content) {
10199
this.content = content;
102-
this.status = Status.SAVED;
100+
}
101+
102+
public void updatePaperType(PaperType paperType) {
103+
this.paperType = paperType;
104+
}
105+
106+
public void updateFontType(FontType fontType) {
107+
this.fontType = fontType;
103108
}
104109

105110
public void updateMatchingId(Long matchingId) {

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,36 @@ public void evaluateLetter(Long letterId, EvaluateLetterRequest request) {
184184
}
185185

186186
@Transactional
187-
public LetterResponse temporarySaveLetter(Long letterId, TemporarySaveLetterRequest request) {
187+
public LetterResponse temporarySaveLetter(TemporarySaveLetterRequest request) {
188188
Long writerId = authFacade.getCurrentUserId();
189189
String writerZipCode = authFacade.getZipCode();
190190

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-
);
191+
Letter letter;
192+
193+
if (request.getLetterId() == null) {
194+
letter = Letter.builder()
195+
.writerId(writerId)
196+
.receiverId(request.getReceiverId())
197+
.matchingId(request.getMatchingId())
198+
.parentLetterId(request.getParentLetterId())
199+
.title(request.getTitle())
200+
.content(request.getContent())
201+
.category(request.getCategory())
202+
.paperType(request.getPaperType())
203+
.fontType(request.getFontType())
204+
.status(Status.SAVED)
205+
.build();
206+
207+
letterRepository.save(letter);
208+
} else {
209+
letter = letterRepository.findByIdAndWriterId(request.getLetterId(), writerId)
210+
.orElseThrow(LetterNotBelongException::new);
211+
212+
letter.updateTitle(request.getTitle());
213+
letter.updateContent(request.getContent());
214+
letter.updatePaperType(request.getPaperType());
215+
letter.updateFontType(request.getFontType());
216+
}
202217

203218
return LetterResponse.fromEntity(letter, writerZipCode);
204219
}

src/test/java/io/crops/warmletter/domain/letter/controller/LettersControllerUnitTest.java

Lines changed: 52 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -214,95 +214,85 @@ void evaluateLetter_Success() throws Exception {
214214
);
215215
}
216216
@Test
217-
@DisplayName("POST /api/letters/{letterId}/temporary-save - 편지 임시 저장 성공 테스트")
217+
@DisplayName("POST /api/letters/temporary-save - 편지 임시 저장 성공 테스트")
218218
void temporarySaveLetter_success() throws Exception {
219219
// given
220-
Long letterId = 1L;
220+
Long writerId = 2L;
221+
222+
Long receiverId = 1L;
223+
Long matchingId = 1L;
224+
Long parentLetterId = 1L;
225+
String title = "임시 저장 제목(처음)";
226+
String content = "임시 저장 내용(처음)";
227+
Category category = Category.CONSULT;
228+
PaperType paperType = PaperType.BASIC;
229+
FontType fontType = FontType.DEFAULT;
230+
231+
TemporarySaveLetterRequest request = new TemporarySaveLetterRequest(
232+
null, receiverId, matchingId, parentLetterId, title, content, category, paperType, fontType
233+
);
221234

222-
TemporarySaveLetterRequest request = new TemporarySaveLetterRequest();
223-
request.setTitle("임시 저장 제목");
224-
request.setContent("임시 저장할 내용입니다.");
225-
request.setCategory(Category.ETC);
226-
request.setPaperType(PaperType.PAPER);
227-
request.setFontType(FontType.GYEONGGI);
235+
Long letterId = 10L;
236+
String zipCode = "11111";
228237

229238
LetterResponse expectedResponse = LetterResponse.builder()
230239
.letterId(letterId)
231-
.writerId(1L)
232-
.title("임시 저장 제목")
233-
.content("임시 저장할 내용입니다.")
234-
.category(Category.ETC)
235-
.paperType(PaperType.PAPER)
236-
.fontType(FontType.GYEONGGI)
240+
.writerId(writerId)
241+
.receiverId(receiverId)
242+
.parentLetterId(parentLetterId)
243+
.zipCode(zipCode)
244+
.title(title)
245+
.content(content)
246+
.category(category)
247+
.paperType(paperType)
248+
.fontType(fontType)
249+
.status(Status.SAVED)
250+
.matchingId(matchingId)
237251
.build();
238252

239-
when(letterService.temporarySaveLetter(eq(letterId), any(TemporarySaveLetterRequest.class)))
253+
when(letterService.temporarySaveLetter(any(TemporarySaveLetterRequest.class)))
240254
.thenReturn(expectedResponse);
241255

242256
// when & then
243-
mockMvc.perform(post("/api/letters/{letterId}/temporary-save", letterId)
257+
mockMvc.perform(post("/api/letters/temporary-save")
244258
.contentType(MediaType.APPLICATION_JSON)
245259
.content(objectMapper.writeValueAsString(request)))
246260
.andExpect(status().isOk())
247261
.andExpect(jsonPath("$.message").value("임시 저장 완료 "))
248-
.andExpect(jsonPath("$.data.letterId").value(letterId))
249-
.andExpect(jsonPath("$.data.title").value("임시 저장 제목"))
250-
.andExpect(jsonPath("$.data.content").value("임시 저장할 내용입니다."))
251-
.andExpect(jsonPath("$.data.category").value("ETC"))
252-
.andExpect(jsonPath("$.data.paperType").value("PAPER"))
253-
.andExpect(jsonPath("$.data.fontType").value("GYEONGGI"))
254-
.andDo(print());
255-
256-
verify(letterService).temporarySaveLetter(eq(letterId), any(TemporarySaveLetterRequest.class));
257-
}
258-
259-
260-
261-
@Test
262-
@DisplayName("POST /api/letters/{letterId}/temporary-save - 존재하지 않는 편지 임시 저장 실패 테스트")
263-
void temporarySaveLetter_fail_letterNotFound() throws Exception {
264-
// given
265-
Long nonExistentLetterId = 999L;
266-
267-
TemporarySaveLetterRequest request = new TemporarySaveLetterRequest();
268-
request.setTitle("임시 저장 제목");
269-
request.setContent("임시 저장할 내용입니다.");
270-
271-
doThrow(new LetterNotFoundException())
272-
.when(letterService)
273-
.temporarySaveLetter(eq(nonExistentLetterId), any(TemporarySaveLetterRequest.class));
274-
275-
// when & then
276-
mockMvc.perform(post("/api/letters/{letterId}/temporary-save", nonExistentLetterId)
277-
.contentType(MediaType.APPLICATION_JSON)
278-
.content(objectMapper.writeValueAsString(request)))
279-
.andExpect(status().isNotFound())
280-
.andExpect(jsonPath("$.code").value("LET-001"))
281-
.andExpect(jsonPath("$.message").value("해당 편지를 찾을 수 없습니다."))
262+
.andExpect(jsonPath("$.data.title").value(expectedResponse.getTitle()))
263+
.andExpect(jsonPath("$.data.content").value(expectedResponse.getContent()))
264+
.andExpect(jsonPath("$.data.category").value(expectedResponse.getCategory().toString()))
265+
.andExpect(jsonPath("$.data.paperType").value(expectedResponse.getPaperType().toString()))
266+
.andExpect(jsonPath("$.data.fontType").value(expectedResponse.getFontType().toString()))
282267
.andDo(print());
283268

284-
verify(letterService).temporarySaveLetter(
285-
eq(nonExistentLetterId),
286-
any(TemporarySaveLetterRequest.class)
287-
);
269+
verify(letterService).temporarySaveLetter(any(TemporarySaveLetterRequest.class));
288270
}
289271

290272
@Test
291-
@DisplayName("POST /api/letters/{letterId}/temporary-save - 권한 없는 편지 임시 저장 실패 테스트")
273+
@DisplayName("POST /api/letters/temporary-save - 권한 없는 편지 임시 저장 실패 테스트")
292274
void temporarySaveLetter_fail_notBelongLetter() throws Exception {
293275
// given
294-
Long unauthorizedLetterId = 888L;
295-
296-
TemporarySaveLetterRequest request = new TemporarySaveLetterRequest();
297-
request.setTitle("임시 저장 제목");
298-
request.setContent("임시 저장할 내용입니다.");
276+
Long invalidLetterId = 999L;
277+
Long receiverId = 1L;
278+
Long matchingId = 1L;
279+
Long parentLetterId = 1L;
280+
String title = "임시 저장 제목(처음)";
281+
String content = "임시 저장 내용(처음)";
282+
Category category = Category.CONSULT;
283+
PaperType paperType = PaperType.BASIC;
284+
FontType fontType = FontType.DEFAULT;
285+
286+
TemporarySaveLetterRequest request = new TemporarySaveLetterRequest(
287+
invalidLetterId, receiverId, matchingId, parentLetterId, title, content, category, paperType, fontType
288+
);
299289

300290
doThrow(new LetterNotBelongException())
301291
.when(letterService)
302-
.temporarySaveLetter(eq(unauthorizedLetterId), any(TemporarySaveLetterRequest.class));
292+
.temporarySaveLetter(any(TemporarySaveLetterRequest.class));
303293

304294
// when & then
305-
mockMvc.perform(post("/api/letters/{letterId}/temporary-save", unauthorizedLetterId)
295+
mockMvc.perform(post("/api/letters/temporary-save")
306296
.contentType(MediaType.APPLICATION_JSON)
307297
.content(objectMapper.writeValueAsString(request)))
308298
.andExpect(status().isForbidden())
@@ -311,7 +301,6 @@ void temporarySaveLetter_fail_notBelongLetter() throws Exception {
311301
.andDo(print());
312302

313303
verify(letterService).temporarySaveLetter(
314-
eq(unauthorizedLetterId),
315304
any(TemporarySaveLetterRequest.class)
316305
);
317306
}

0 commit comments

Comments
 (0)