Skip to content

Commit fdd7420

Browse files
authored
Refactor/201 쪽지 상세조회시 읽음처리 기능 추가 (#202)
* refactor: 쪽지 상세조회시 읽음처리 기능 추가 - Note 엔티티에 isRead 업데이트 기능 구현 - 기존 쪽지 상세조회 로직에 isRead 업데이트 기능 추가 * refactor: 읽음처리 메서드의 책임 분리 - 읽음 처리를 위한 유스케이스 추가 - 읽음 처리 유스케이스 구현체 추가 - 테스트 코드 검증및 구현 * fix: 쪽지 읽음처리 테스트 코드 수정 - noteId를 고정값이 아닌 생성된 쪽지의 id를 사용하도록 수정
1 parent b23e334 commit fdd7420

File tree

7 files changed

+103
-0
lines changed

7 files changed

+103
-0
lines changed

src/main/java/com/somemore/note/domain/Note.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,8 @@ public static Note create(UUID senderId, UUID receiverId, String title, String c
5353
.build();
5454
}
5555

56+
public void markAsRead() {
57+
this.isRead = true;
58+
}
59+
5660
}

src/main/java/com/somemore/note/repository/NoteRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
public interface NoteRepository {
1515
Note save(Note note);
16+
Optional<Note> findById(Long noteId);
1617
Page<NoteReceiverViewForCenter> findNotesByReceiverIsCenter(UUID centerId, Pageable pageable);
1718
Page<NoteReceiverViewForVolunteer> findNotesByReceiverIsVolunteer(UUID volunteerId, Pageable pageable);
1819
Optional<NoteDetailViewForCenter> findNoteDetailViewReceiverIsCenter(Long noteId);

src/main/java/com/somemore/note/repository/NoteRepositoryImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public Note save(Note note) {
4242
return noteJpaRepository.save(note);
4343
}
4444

45+
@Override
46+
public Optional<Note> findById(Long noteId) {
47+
return noteJpaRepository.findById(noteId);
48+
}
49+
4550
@Override
4651
public Page<NoteReceiverViewForCenter> findNotesByReceiverIsCenter(UUID centerId, Pageable pageable) {
4752

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.somemore.note.service;
2+
3+
import com.somemore.global.exception.NoSuchElementException;
4+
import com.somemore.note.domain.Note;
5+
import com.somemore.note.repository.NoteRepository;
6+
import com.somemore.note.usecase.NoteMarkAsReadUseCase;
7+
import lombok.RequiredArgsConstructor;
8+
import org.springframework.stereotype.Service;
9+
import org.springframework.transaction.annotation.Transactional;
10+
11+
import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_NOTE;
12+
13+
@RequiredArgsConstructor
14+
@Service
15+
@Transactional
16+
public class NoteMarkAsReadService implements NoteMarkAsReadUseCase {
17+
18+
private final NoteRepository noteRepository;
19+
20+
@Override
21+
public void noteMarkAsRead(Long noteId) {
22+
Note note = getNote(noteId);
23+
note.markAsRead();
24+
}
25+
26+
private Note getNote(Long noteId) {
27+
return noteRepository.findById(noteId)
28+
.orElseThrow(() -> new NoSuchElementException(NOT_EXISTS_NOTE));
29+
}
30+
}

src/main/java/com/somemore/note/service/NoteQueryService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.somemore.note.repository.mapper.NoteDetailViewForVolunteer;
77
import com.somemore.note.repository.mapper.NoteReceiverViewForCenter;
88
import com.somemore.note.repository.mapper.NoteReceiverViewForVolunteer;
9+
import com.somemore.note.usecase.NoteMarkAsReadUseCase;
910
import com.somemore.note.usecase.NoteQueryUseCase;
1011
import lombok.RequiredArgsConstructor;
1112
import org.springframework.data.domain.Page;
@@ -22,6 +23,7 @@
2223
@Transactional(readOnly = true)
2324
public class NoteQueryService implements NoteQueryUseCase {
2425

26+
private final NoteMarkAsReadUseCase noteMarkAsReadUseCase;
2527
private final NoteRepository noteRepository;
2628

2729
@Override
@@ -36,12 +38,16 @@ public Page<NoteReceiverViewForVolunteer> getNotesForVolunteer(UUID volunteerId,
3638

3739
@Override
3840
public NoteDetailViewForCenter getNoteDetailForCenter(Long noteId) {
41+
noteMarkAsReadUseCase.noteMarkAsRead(noteId);
42+
3943
return noteRepository.findNoteDetailViewReceiverIsCenter(noteId)
4044
.orElseThrow(() -> new NoSuchElementException(NOT_EXISTS_NOTE));
4145
}
4246

4347
@Override
4448
public NoteDetailViewForVolunteer getNoteDetailForVolunteer(Long noteId) {
49+
noteMarkAsReadUseCase.noteMarkAsRead(noteId);
50+
4551
return noteRepository.findNoteDetailViewReceiverIsVolunteer(noteId)
4652
.orElseThrow(() -> new NoSuchElementException(NOT_EXISTS_NOTE));
4753
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.somemore.note.usecase;
2+
3+
public interface NoteMarkAsReadUseCase {
4+
void noteMarkAsRead(Long noteId);
5+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.somemore.note.service;
2+
3+
import com.somemore.IntegrationTestSupport;
4+
import com.somemore.global.exception.NoSuchElementException;
5+
import com.somemore.note.domain.Note;
6+
import com.somemore.note.repository.NoteRepository;
7+
import org.junit.jupiter.api.DisplayName;
8+
import org.junit.jupiter.api.Test;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
11+
import java.util.UUID;
12+
13+
import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_NOTE;
14+
import static org.junit.jupiter.api.Assertions.*;
15+
16+
class NoteMarkAsReadServiceTest extends IntegrationTestSupport {
17+
18+
@Autowired
19+
private NoteMarkAsReadService noteMarkAsReadService;
20+
21+
@Autowired
22+
private NoteRepository noteRepository;
23+
24+
@DisplayName("쪽지를 읽음 처리할 수 있다.")
25+
@Test
26+
void noteMarkAsRead() {
27+
// given
28+
Note note = Note.create(UUID.randomUUID(), UUID.randomUUID(), "제목", "내용");
29+
noteRepository.save(note);
30+
31+
// when
32+
noteMarkAsReadService.noteMarkAsRead(note.getId());
33+
34+
// then
35+
Note updatedNote = noteRepository.findById(note.getId())
36+
.orElseThrow(() -> new NoSuchElementException(NOT_EXISTS_NOTE));
37+
38+
assertTrue(updatedNote.getIsRead(), "노트가 읽음 상태로 변경되어야 한다.");
39+
}
40+
41+
@DisplayName("존재하지 않는 쪽지에 대한 업데이트 요청은 예외를 반환한다.")
42+
@Test
43+
void noteMarkAsRead_throwsException_WhenNoteNotFound() {
44+
// given
45+
Long nonExistentNoteId = 999L;
46+
47+
// when & then
48+
assertThrows(NoSuchElementException.class,
49+
() -> noteMarkAsReadService.noteMarkAsRead(nonExistentNoteId),
50+
"존재하지 않는 노트에 대해 예외가 발생해야 한다.");
51+
}
52+
}

0 commit comments

Comments
 (0)