diff --git a/src/main/java/unithon/uniletter/event/service/EventService.java b/src/main/java/unithon/uniletter/event/service/EventService.java index 56322b9..a91fa23 100644 --- a/src/main/java/unithon/uniletter/event/service/EventService.java +++ b/src/main/java/unithon/uniletter/event/service/EventService.java @@ -12,6 +12,7 @@ import unithon.uniletter.member.repository.MemberRepository; import unithon.uniletter.message.dto.MessageResponse; import unithon.uniletter.message.repository.MessageRepository; +import unithon.uniletter.time.service.TimeGenerator; import java.time.LocalDate; import java.util.Arrays; @@ -24,6 +25,7 @@ public class EventService { private final EventRepository eventRepository; private final MessageRepository messageRepository; + private final TimeGenerator timeGenerator; private final MemberRepository memberRepository; @Transactional @@ -43,7 +45,7 @@ public List findMemberEventAfterToday(final String nickName) { if (!memberRepository.existsByNickname(nickName)) { throw new NotFoundException("닉네임에 해당하는 멤버를 찾을 수 없습니다."); } - return eventRepository.findEventsAfterToday(nickName, LocalDate.now()); + return eventRepository.findEventsAfterToday(nickName, timeGenerator.generate()); } public List findEventTypeAll() { diff --git a/src/main/java/unithon/uniletter/message/repository/MessageRepository.java b/src/main/java/unithon/uniletter/message/repository/MessageRepository.java index 4d37078..f50aa82 100644 --- a/src/main/java/unithon/uniletter/message/repository/MessageRepository.java +++ b/src/main/java/unithon/uniletter/message/repository/MessageRepository.java @@ -1,14 +1,25 @@ package unithon.uniletter.message.repository; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import unithon.uniletter.common.error.NotFoundException; import unithon.uniletter.message.Message; +import java.time.LocalDate; import java.util.List; import java.util.UUID; public interface MessageRepository extends JpaRepository { - List findByReceiverIdAndIsReadOrderBySendPlannedAtAsc(UUID receiverId, Boolean isRead); + @Query(""" + select message + from Message message + join fetch Member member + on member.id = message.receiverId + where message.isRead = false + and message.sendPlannedAt <= :today + order by message.sendPlannedAt asc + """) + List readArriveMessage(final UUID receiverId, final Boolean isRead, final LocalDate today); List findByReceiverIdOrderBySendPlannedAtDesc(UUID receiverId); diff --git a/src/main/java/unithon/uniletter/message/service/MessageService.java b/src/main/java/unithon/uniletter/message/service/MessageService.java index e9b8b71..cd941c0 100644 --- a/src/main/java/unithon/uniletter/message/service/MessageService.java +++ b/src/main/java/unithon/uniletter/message/service/MessageService.java @@ -12,6 +12,7 @@ import unithon.uniletter.message.dto.MessageRequest; import unithon.uniletter.message.dto.MessageResponse; import unithon.uniletter.message.repository.MessageRepository; +import unithon.uniletter.time.service.TimeGenerator; import java.util.List; import java.util.UUID; @@ -23,6 +24,7 @@ @Transactional public class MessageService { + private final TimeGenerator timeGenerator; private final MessageRepository messageRepository; private final MemberRepository memberRepository; private final EventRepository eventRepository; @@ -44,7 +46,7 @@ public void sendMessage(final MessageRequest request, final Member loginMember) @Transactional public List getUnreadMessages(final Member member) { - final List messages = messageRepository.findByReceiverIdAndIsReadOrderBySendPlannedAtAsc(member.getId(), false); + final List messages = messageRepository.readArriveMessage(member.getId(), false, timeGenerator.generate()); messages.forEach(Message::read); return getMessageWithEvent(messages); } diff --git a/src/main/java/unithon/uniletter/time/TimeRequest.java b/src/main/java/unithon/uniletter/time/TimeRequest.java new file mode 100644 index 0000000..8791685 --- /dev/null +++ b/src/main/java/unithon/uniletter/time/TimeRequest.java @@ -0,0 +1,6 @@ +package unithon.uniletter.time; + +import java.time.LocalDate; + +public record TimeRequest(LocalDate localDate) { +} diff --git a/src/main/java/unithon/uniletter/time/controller/TimeController.java b/src/main/java/unithon/uniletter/time/controller/TimeController.java new file mode 100644 index 0000000..e91a368 --- /dev/null +++ b/src/main/java/unithon/uniletter/time/controller/TimeController.java @@ -0,0 +1,21 @@ +package unithon.uniletter.time.controller; + +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; +import unithon.uniletter.time.TimeRequest; +import unithon.uniletter.time.service.TimeService; + +@RestController +@RequiredArgsConstructor +public class TimeController { + + private final TimeService timeService; + + @PostMapping("/time") + public ResponseEntity setServerTime(final TimeRequest timeRequest) { + timeService.setServerTime(timeRequest.localDate()); + return ResponseEntity.ok().build(); + } +} diff --git a/src/main/java/unithon/uniletter/time/service/TimeGenerator.java b/src/main/java/unithon/uniletter/time/service/TimeGenerator.java new file mode 100644 index 0000000..e7c4e8d --- /dev/null +++ b/src/main/java/unithon/uniletter/time/service/TimeGenerator.java @@ -0,0 +1,23 @@ +package unithon.uniletter.time.service; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +import java.time.LocalDate; +import java.util.Optional; + +@RequiredArgsConstructor +@Component +public class TimeGenerator { + + private LocalDate localDate; + + public LocalDate generate() { + return Optional.ofNullable(localDate) + .orElseGet(LocalDate::now); + } + + public void setLocalDate(final LocalDate localDate) { + this.localDate = localDate; + } +} diff --git a/src/main/java/unithon/uniletter/time/service/TimeService.java b/src/main/java/unithon/uniletter/time/service/TimeService.java new file mode 100644 index 0000000..9324e75 --- /dev/null +++ b/src/main/java/unithon/uniletter/time/service/TimeService.java @@ -0,0 +1,17 @@ +package unithon.uniletter.time.service; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.time.LocalDate; + +@RequiredArgsConstructor +@Service +public class TimeService { + + private final TimeGenerator timeGenerator; + + public void setServerTime(final LocalDate localDate) { + timeGenerator.setLocalDate(localDate); + } +}