Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/com/somemore/note/domain/Note.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ public static Note create(UUID senderId, UUID receiverId, String title, String c
.build();
}

public void markAsRead() {
this.isRead = true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

public interface NoteRepository {
Note save(Note note);
Optional<Note> findById(Long noteId);
Page<NoteReceiverViewForCenter> findNotesByReceiverIsCenter(UUID centerId, Pageable pageable);
Page<NoteReceiverViewForVolunteer> findNotesByReceiverIsVolunteer(UUID volunteerId, Pageable pageable);
Optional<NoteDetailViewForCenter> findNoteDetailViewReceiverIsCenter(Long noteId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public Note save(Note note) {
return noteJpaRepository.save(note);
}

@Override
public Optional<Note> findById(Long noteId) {
return noteJpaRepository.findById(noteId);
}

@Override
public Page<NoteReceiverViewForCenter> findNotesByReceiverIsCenter(UUID centerId, Pageable pageable) {

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/somemore/note/service/NoteMarkAsReadService.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/somemore/note/service/NoteQueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +23,7 @@
@Transactional(readOnly = true)
public class NoteQueryService implements NoteQueryUseCase {

private final NoteMarkAsReadUseCase noteMarkAsReadUseCase;
private final NoteRepository noteRepository;

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

@Override
public NoteDetailViewForCenter getNoteDetailForCenter(Long noteId) {
noteMarkAsReadUseCase.noteMarkAsRead(noteId);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거 일단은 QueryService 라 transactional read true 일텐데
레포지토리로 .save 해주면 좋을것 같아요.

return noteRepository.findNoteDetailViewReceiverIsCenter(noteId)
.orElseThrow(() -> new NoSuchElementException(NOT_EXISTS_NOTE));
}

@Override
public NoteDetailViewForVolunteer getNoteDetailForVolunteer(Long noteId) {
noteMarkAsReadUseCase.noteMarkAsRead(noteId);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다 큰 실수를 할 뻔했습니다
책임의 분리가 확실하지 않다고 생각해서 읽음처리에 대한 유스케이스와 구현체를 따로 구현하도록 리팩토링 했습니다

return noteRepository.findNoteDetailViewReceiverIsVolunteer(noteId)
.orElseThrow(() -> new NoSuchElementException(NOT_EXISTS_NOTE));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.somemore.note.usecase;

public interface NoteMarkAsReadUseCase {
void noteMarkAsRead(Long noteId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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
Long noteId = 1L;

Note note = Note.create(UUID.randomUUID(), UUID.randomUUID(), "제목", "내용");
noteRepository.save(note);

// when
noteMarkAsReadService.noteMarkAsRead(noteId);

// then
Note updatedNote = noteRepository.findById(noteId)
.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),
"존재하지 않는 노트에 대해 예외가 발생해야 한다.");
}
}
Loading