Skip to content

Commit

Permalink
refactor: 레포지토리 저장 시 여러 개 저장하는 메서드 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
swonny committed Nov 4, 2023
1 parent dbad4b3 commit 461d34d
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
Expand All @@ -26,8 +28,7 @@ public void create(final CreateReadMessageLogEvent createReadMessageLogEvent) {
final ReadMessageLog buyerReadMessageLog = new ReadMessageLog(chatRoom, buyer);
final ReadMessageLog sellerReadMessageLog = new ReadMessageLog(chatRoom, seller);

readMessageLogRepository.save(buyerReadMessageLog);
readMessageLogRepository.save(sellerReadMessageLog);
readMessageLogRepository.saveAll(List.of(buyerReadMessageLog, sellerReadMessageLog));
}

@Transactional
Expand All @@ -39,6 +40,7 @@ public void update(final UpdateReadMessageLogEvent updateReadMessageLogEvent) {
new ReadMessageLogNotFoundException(
"메시지 조회 로그가 존재하지 않습니다."
));

messageLog.updateLastReadMessage(updateReadMessageLogEvent.lastReadMessage().getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import com.ddang.ddang.chat.domain.ReadMessageLog;

import java.util.List;
import java.util.Optional;

public interface ReadMessageLogRepository {

ReadMessageLog save(final ReadMessageLog readMessageLog);

Optional<ReadMessageLog> findBy(final Long readerId, final Long chatRoomId);

List<ReadMessageLog> saveAll(List<ReadMessageLog> readMessageLogs);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
Expand All @@ -14,12 +15,12 @@ public class ReadMessageLogRepositoryImpl implements ReadMessageLogRepository {
private final JpaReadMessageLogRepository jpaReadMessageLogRepository;

@Override
public ReadMessageLog save(final ReadMessageLog readMessageLog) {
return jpaReadMessageLogRepository.save(readMessageLog);
public Optional<ReadMessageLog> findBy(final Long readerId, final Long chatRoomId) {
return jpaReadMessageLogRepository.findLastReadMessageByUserIdAndChatRoomId(readerId, chatRoomId);
}

@Override
public Optional<ReadMessageLog> findBy(final Long readerId, final Long chatRoomId) {
return jpaReadMessageLogRepository.findLastReadMessageByUserIdAndChatRoomId(readerId, chatRoomId);
public List<ReadMessageLog> saveAll(final List<ReadMessageLog> readMessageLogs) {
return jpaReadMessageLogRepository.saveAll(readMessageLogs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

@SuppressWarnings("NonAsciiCharacters")
public class LastReadMessageLogServiceFixture {
Expand Down Expand Up @@ -174,8 +175,7 @@ void fixtureSetUp(

final ReadMessageLog 메시지_로그_업데이트용_로그_판매자 = new ReadMessageLog(메시지_로그_업데이트용_채팅방, 메시지_로그_업데이트용_발신자_겸_판매자);
final ReadMessageLog 메시지_로그_업데이트용_로그_구매자 = new ReadMessageLog(메시지_로그_업데이트용_채팅방, 메시지_로그_업데이트용_입찰자);
readMessageLogRepository.save(메시지_로그_업데이트용_로그_판매자);
readMessageLogRepository.save(메시지_로그_업데이트용_로그_구매자);
readMessageLogRepository.saveAll(List.of(메시지_로그_업데이트용_로그_판매자, 메시지_로그_업데이트용_로그_구매자));

메시지_로그_업데이트용_이벤트 = new UpdateReadMessageLogEvent(메시지_로그_업데이트용_발신자_겸_판매자, 메시지_로그_업데이트용_채팅방, 메시지_로그_업데이트용_마지막_조회_메시지);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ void setUp(
@Test
void 사용자가_읽지_않은_메시지_개수를_반환한다() {
// given
readMessageLogRepository.save(new ReadMessageLog(메리_엔초_채팅방, 엔초));
readMessageLogRepository.save(new ReadMessageLog(메리_엔초_채팅방, 메리));
readMessageLogRepository.saveAll(List.of(new ReadMessageLog(메리_엔초_채팅방, 메리), new ReadMessageLog(메리_엔초_채팅방, 엔초)));

// when
messageRepository.save(메리가_엔초에게_3시에_보낸_쪽지1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;

import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -34,7 +35,7 @@ void setUp(@Autowired final JpaReadMessageLogRepository jpaReadMessageLogReposit
@Test
void 마지막_읽은_메시지를_저장한다() {
// given
final ReadMessageLog actual = readMessageLogRepository.save(다섯_번째_메시지까지_읽은_메시지_로그);
final ReadMessageLog actual = readMessageLogRepository.saveAll(List.of(다섯_번째_메시지까지_읽은_메시지_로그)).get(0);

// then
assertThat(actual.getId()).isPositive();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,6 @@ void fixtureSetUp(

final ReadMessageLog 메리_엔초_채팅방의_메리_메시지_조회_로그 = new ReadMessageLog(메리_엔초_채팅방, 메리);
메리_엔초_채팅방의_메리_메시지_조회_로그.updateLastReadMessage(다섯_번째_메시지.getId());
readMessageLogRepository.save(메리_엔초_채팅방의_메리_메시지_조회_로그);
readMessageLogRepository.saveAll(List.of(메리_엔초_채팅방의_메리_메시지_조회_로그));
}
}

0 comments on commit 461d34d

Please sign in to comment.