Skip to content

Commit

Permalink
merge: 시간 생성 모킹
Browse files Browse the repository at this point in the history
Feature/#86 시연을 위한 시간 모킹
  • Loading branch information
hong-sile authored Apr 7, 2024
2 parents 7bbdbe2 + 850501e commit 1c7a417
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +25,7 @@ public class EventService {

private final EventRepository eventRepository;
private final MessageRepository messageRepository;
private final TimeGenerator timeGenerator;
private final MemberRepository memberRepository;

@Transactional
Expand All @@ -43,7 +45,7 @@ public List<Event> 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<EventType> findEventTypeAll() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Message, UUID> {
List<Message> 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<Message> readArriveMessage(final UUID receiverId, final Boolean isRead, final LocalDate today);

List<Message> findByReceiverIdOrderBySendPlannedAtDesc(UUID receiverId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,6 +24,7 @@
@Transactional
public class MessageService {

private final TimeGenerator timeGenerator;
private final MessageRepository messageRepository;
private final MemberRepository memberRepository;
private final EventRepository eventRepository;
Expand All @@ -44,7 +46,7 @@ public void sendMessage(final MessageRequest request, final Member loginMember)

@Transactional
public List<MessageResponse> getUnreadMessages(final Member member) {
final List<Message> messages = messageRepository.findByReceiverIdAndIsReadOrderBySendPlannedAtAsc(member.getId(), false);
final List<Message> messages = messageRepository.readArriveMessage(member.getId(), false, timeGenerator.generate());
messages.forEach(Message::read);
return getMessageWithEvent(messages);
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/unithon/uniletter/time/TimeRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package unithon.uniletter.time;

import java.time.LocalDate;

public record TimeRequest(LocalDate localDate) {
}
Original file line number Diff line number Diff line change
@@ -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<Void> setServerTime(final TimeRequest timeRequest) {
timeService.setServerTime(timeRequest.localDate());
return ResponseEntity.ok().build();
}
}
23 changes: 23 additions & 0 deletions src/main/java/unithon/uniletter/time/service/TimeGenerator.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
17 changes: 17 additions & 0 deletions src/main/java/unithon/uniletter/time/service/TimeService.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 1c7a417

Please sign in to comment.