diff --git a/src/main/java/com/somemore/note/domain/Note.java b/src/main/java/com/somemore/note/domain/Note.java index a57bcdaab..d92d562c9 100644 --- a/src/main/java/com/somemore/note/domain/Note.java +++ b/src/main/java/com/somemore/note/domain/Note.java @@ -53,4 +53,8 @@ public static Note create(UUID senderId, UUID receiverId, String title, String c .build(); } + public void markAsRead() { + this.isRead = true; + } + } diff --git a/src/main/java/com/somemore/note/repository/NoteRepository.java b/src/main/java/com/somemore/note/repository/NoteRepository.java index 31a8a5abf..67d76fa64 100644 --- a/src/main/java/com/somemore/note/repository/NoteRepository.java +++ b/src/main/java/com/somemore/note/repository/NoteRepository.java @@ -13,6 +13,7 @@ public interface NoteRepository { Note save(Note note); + Optional findById(Long noteId); Page findNotesByReceiverIsCenter(UUID centerId, Pageable pageable); Page findNotesByReceiverIsVolunteer(UUID volunteerId, Pageable pageable); Optional findNoteDetailViewReceiverIsCenter(Long noteId); diff --git a/src/main/java/com/somemore/note/repository/NoteRepositoryImpl.java b/src/main/java/com/somemore/note/repository/NoteRepositoryImpl.java index de9d74085..d224168bd 100644 --- a/src/main/java/com/somemore/note/repository/NoteRepositoryImpl.java +++ b/src/main/java/com/somemore/note/repository/NoteRepositoryImpl.java @@ -42,6 +42,11 @@ public Note save(Note note) { return noteJpaRepository.save(note); } + @Override + public Optional findById(Long noteId) { + return noteJpaRepository.findById(noteId); + } + @Override public Page findNotesByReceiverIsCenter(UUID centerId, Pageable pageable) { diff --git a/src/main/java/com/somemore/note/service/NoteMarkAsReadService.java b/src/main/java/com/somemore/note/service/NoteMarkAsReadService.java new file mode 100644 index 000000000..6557ae0bf --- /dev/null +++ b/src/main/java/com/somemore/note/service/NoteMarkAsReadService.java @@ -0,0 +1,30 @@ +package com.somemore.note.service; + +import com.somemore.global.exception.NoSuchElementException; +import com.somemore.note.domain.Note; +import com.somemore.note.repository.NoteRepository; +import com.somemore.note.usecase.NoteMarkAsReadUseCase; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_NOTE; + +@RequiredArgsConstructor +@Service +@Transactional +public class NoteMarkAsReadService implements NoteMarkAsReadUseCase { + + private final NoteRepository noteRepository; + + @Override + public void noteMarkAsRead(Long noteId) { + Note note = getNote(noteId); + note.markAsRead(); + } + + private Note getNote(Long noteId) { + return noteRepository.findById(noteId) + .orElseThrow(() -> new NoSuchElementException(NOT_EXISTS_NOTE)); + } +} diff --git a/src/main/java/com/somemore/note/service/NoteQueryService.java b/src/main/java/com/somemore/note/service/NoteQueryService.java index 65b10894a..33ce8ec34 100644 --- a/src/main/java/com/somemore/note/service/NoteQueryService.java +++ b/src/main/java/com/somemore/note/service/NoteQueryService.java @@ -6,6 +6,7 @@ import com.somemore.note.repository.mapper.NoteDetailViewForVolunteer; import com.somemore.note.repository.mapper.NoteReceiverViewForCenter; import com.somemore.note.repository.mapper.NoteReceiverViewForVolunteer; +import com.somemore.note.usecase.NoteMarkAsReadUseCase; import com.somemore.note.usecase.NoteQueryUseCase; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; @@ -22,6 +23,7 @@ @Transactional(readOnly = true) public class NoteQueryService implements NoteQueryUseCase { + private final NoteMarkAsReadUseCase noteMarkAsReadUseCase; private final NoteRepository noteRepository; @Override @@ -36,12 +38,16 @@ public Page getNotesForVolunteer(UUID volunteerId, @Override public NoteDetailViewForCenter getNoteDetailForCenter(Long noteId) { + noteMarkAsReadUseCase.noteMarkAsRead(noteId); + return noteRepository.findNoteDetailViewReceiverIsCenter(noteId) .orElseThrow(() -> new NoSuchElementException(NOT_EXISTS_NOTE)); } @Override public NoteDetailViewForVolunteer getNoteDetailForVolunteer(Long noteId) { + noteMarkAsReadUseCase.noteMarkAsRead(noteId); + return noteRepository.findNoteDetailViewReceiverIsVolunteer(noteId) .orElseThrow(() -> new NoSuchElementException(NOT_EXISTS_NOTE)); } diff --git a/src/main/java/com/somemore/note/usecase/NoteMarkAsReadUseCase.java b/src/main/java/com/somemore/note/usecase/NoteMarkAsReadUseCase.java new file mode 100644 index 000000000..c49e4866d --- /dev/null +++ b/src/main/java/com/somemore/note/usecase/NoteMarkAsReadUseCase.java @@ -0,0 +1,5 @@ +package com.somemore.note.usecase; + +public interface NoteMarkAsReadUseCase { + void noteMarkAsRead(Long noteId); +} diff --git a/src/test/java/com/somemore/note/service/NoteMarkAsReadServiceTest.java b/src/test/java/com/somemore/note/service/NoteMarkAsReadServiceTest.java new file mode 100644 index 000000000..6c91ed88d --- /dev/null +++ b/src/test/java/com/somemore/note/service/NoteMarkAsReadServiceTest.java @@ -0,0 +1,52 @@ +package com.somemore.note.service; + +import com.somemore.IntegrationTestSupport; +import com.somemore.global.exception.NoSuchElementException; +import com.somemore.note.domain.Note; +import com.somemore.note.repository.NoteRepository; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.UUID; + +import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_NOTE; +import static org.junit.jupiter.api.Assertions.*; + +class NoteMarkAsReadServiceTest extends IntegrationTestSupport { + + @Autowired + private NoteMarkAsReadService noteMarkAsReadService; + + @Autowired + private NoteRepository noteRepository; + + @DisplayName("쪽지를 읽음 처리할 수 있다.") + @Test + void noteMarkAsRead() { + // given + Note note = Note.create(UUID.randomUUID(), UUID.randomUUID(), "제목", "내용"); + noteRepository.save(note); + + // when + noteMarkAsReadService.noteMarkAsRead(note.getId()); + + // then + Note updatedNote = noteRepository.findById(note.getId()) + .orElseThrow(() -> new NoSuchElementException(NOT_EXISTS_NOTE)); + + assertTrue(updatedNote.getIsRead(), "노트가 읽음 상태로 변경되어야 한다."); + } + + @DisplayName("존재하지 않는 쪽지에 대한 업데이트 요청은 예외를 반환한다.") + @Test + void noteMarkAsRead_throwsException_WhenNoteNotFound() { + // given + Long nonExistentNoteId = 999L; + + // when & then + assertThrows(NoSuchElementException.class, + () -> noteMarkAsReadService.noteMarkAsRead(nonExistentNoteId), + "존재하지 않는 노트에 대해 예외가 발생해야 한다."); + } +}