diff --git a/src/main/java/org/kkumulkkum/server/controller/PromiseController.java b/src/main/java/org/kkumulkkum/server/controller/PromiseController.java index b35f285..ba1c153 100644 --- a/src/main/java/org/kkumulkkum/server/controller/PromiseController.java +++ b/src/main/java/org/kkumulkkum/server/controller/PromiseController.java @@ -6,6 +6,8 @@ import org.kkumulkkum.server.annotation.IsParticipant; import org.kkumulkkum.server.annotation.UserId; import org.kkumulkkum.server.dto.promise.PromiseCreateDto; +import org.kkumulkkum.server.dto.promise.response.MainPromiseDto; +import org.kkumulkkum.server.dto.promise.response.MainPromisesDto; import org.kkumulkkum.server.dto.promise.response.PromiseDto; import org.kkumulkkum.server.dto.promise.response.PromisesDto; import org.kkumulkkum.server.service.promise.PromiseService; @@ -58,4 +60,17 @@ public ResponseEntity getPromise( return ResponseEntity.ok().body(promiseService.getPromise(promiseId)); } + @GetMapping("/promises/today/next") + public ResponseEntity getNextPromise( + @UserId final Long userId + ) { + return ResponseEntity.ok().body(promiseService.getNextPromise(userId)); + } + + @GetMapping("/promises/upcoming") + public ResponseEntity getUpcomingPromise( + @UserId final Long userId + ) { + return ResponseEntity.ok().body(promiseService.getUpcomingPromises(userId)); + } } diff --git a/src/main/java/org/kkumulkkum/server/dto/promise/response/MainPromiseDto.java b/src/main/java/org/kkumulkkum/server/dto/promise/response/MainPromiseDto.java new file mode 100644 index 0000000..1de063d --- /dev/null +++ b/src/main/java/org/kkumulkkum/server/dto/promise/response/MainPromiseDto.java @@ -0,0 +1,33 @@ +package org.kkumulkkum.server.dto.promise.response; + +import com.fasterxml.jackson.annotation.JsonFormat; +import org.kkumulkkum.server.domain.Promise; + +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; + +public record MainPromiseDto( + Long id, + String name, + String meetingName, + String dressLevel, + int dDay, + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy.MM.dd") + LocalDateTime date, + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "a h:mm", locale = "en") + LocalDateTime time, + String placeName +) { + public static MainPromiseDto from(Promise promise) { + return new MainPromiseDto( + promise.getId(), + promise.getName(), + promise.getMeeting().getName(), + promise.getDressUpLevel().getContent(), + (int) ChronoUnit.DAYS.between(LocalDateTime.now(), promise.getTime()), + promise.getTime(), + promise.getTime(), + promise.getPlaceName() + ); + } +} diff --git a/src/main/java/org/kkumulkkum/server/dto/promise/response/MainPromisesDto.java b/src/main/java/org/kkumulkkum/server/dto/promise/response/MainPromisesDto.java new file mode 100644 index 0000000..a8baa67 --- /dev/null +++ b/src/main/java/org/kkumulkkum/server/dto/promise/response/MainPromisesDto.java @@ -0,0 +1,17 @@ +package org.kkumulkkum.server.dto.promise.response; + +import org.kkumulkkum.server.domain.Promise; + +import java.util.List; + +public record MainPromisesDto( + List promises +) { + public static MainPromisesDto from(List promises) { + return new MainPromisesDto( + promises.stream() + .map(MainPromiseDto::from) + .toList() + ); + } +} diff --git a/src/main/java/org/kkumulkkum/server/repository/PromiseRepository.java b/src/main/java/org/kkumulkkum/server/repository/PromiseRepository.java index b05ec44..70c96bc 100644 --- a/src/main/java/org/kkumulkkum/server/repository/PromiseRepository.java +++ b/src/main/java/org/kkumulkkum/server/repository/PromiseRepository.java @@ -1,17 +1,51 @@ package org.kkumulkkum.server.repository; import org.kkumulkkum.server.domain.Promise; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import java.time.LocalDateTime; import java.util.List; public interface PromiseRepository extends JpaRepository { List findAllByMeetingId(Long meetingId); + @Query("SELECT p FROM Participant pt " + + "JOIN pt.member m " + + "JOIN pt.promise p " + + "WHERE m.user.id = :userId " + + "AND p.time >= :startOfDay " + + "AND p.time < :startOfNextDay " + + "AND p.time > CURRENT_TIMESTAMP " + + "AND p.isCompleted = false " + + "ORDER BY p.time ASC, p.createdAt ASC") + List findNextPromiseByUserId(Long userId, LocalDateTime startOfDay, LocalDateTime startOfNextDay, Pageable pageable); + + @Query("SELECT p FROM Participant pt " + + "JOIN pt.member m " + + "JOIN pt.promise p " + + "WHERE m.user.id = :userId " + + "AND p.time > CURRENT_TIMESTAMP " + + "AND p.isCompleted = false " + + "AND p.id <> :nextPromiseId " + + "ORDER BY p.time ASC, p.createdAt ASC") + Page findUpcomingPromisesExcludingNext(Long userId, Long nextPromiseId, Pageable pageable); + + @Query("SELECT p FROM Participant pt " + + "JOIN pt.member m " + + "JOIN pt.promise p " + + "WHERE m.user.id = :userId " + + "AND p.time > CURRENT_TIMESTAMP " + + "AND p.isCompleted = false " + + "ORDER BY p.time ASC, p.createdAt ASC") + Page findUpcomingPromises(Long userId, Pageable pageable); + @Query("SELECT CASE WHEN EXISTS " + "(SELECT p FROM Participant p WHERE p.promise.id = :promiseId AND p.arrivalAt IS NULL) " + "THEN TRUE ELSE FALSE END FROM Participant p") boolean existsByArrivedAtIsNull(Long promiseId); + } diff --git a/src/main/java/org/kkumulkkum/server/service/promise/PromiseRetriever.java b/src/main/java/org/kkumulkkum/server/service/promise/PromiseRetriever.java index 4ebd98c..cc4f344 100644 --- a/src/main/java/org/kkumulkkum/server/service/promise/PromiseRetriever.java +++ b/src/main/java/org/kkumulkkum/server/service/promise/PromiseRetriever.java @@ -5,9 +5,13 @@ import org.kkumulkkum.server.exception.PromiseException; import org.kkumulkkum.server.exception.code.PromiseErrorCode; import org.kkumulkkum.server.repository.PromiseRepository; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Component; +import java.time.LocalDateTime; import java.util.List; +import java.util.stream.Collectors; @Component @RequiredArgsConstructor @@ -24,7 +28,23 @@ public Promise findById(Long id) { .orElseThrow(() -> new PromiseException(PromiseErrorCode.NOT_FOUND_PROMISE)); } + public List findNextPromiseByUserId(Long userId, LocalDateTime startOfDay, LocalDateTime startOfNextDay) { + return promiseRepository.findNextPromiseByUserId(userId, startOfDay, startOfNextDay, PageRequest.of(0,1)); + + } + + public List findUpcomingPromisesExcludingNext(Long userId, Promise nextPromise, int limit) { + Page promisePage = promiseRepository.findUpcomingPromisesExcludingNext(userId, nextPromise.getId(), PageRequest.of(0, limit)); + return promisePage.stream().collect(Collectors.toList()); + } + + public List findUpcomingPromises(Long userId, int limit) { + Page promisePage = promiseRepository.findUpcomingPromises(userId, PageRequest.of(0, limit)); + return promisePage.stream().collect(Collectors.toList()); + } + public boolean existsByArrivedAtIsNull(Long promiseId) { return promiseRepository.existsByArrivedAtIsNull(promiseId); + } } diff --git a/src/main/java/org/kkumulkkum/server/service/promise/PromiseService.java b/src/main/java/org/kkumulkkum/server/service/promise/PromiseService.java index dc4e494..39ad26e 100644 --- a/src/main/java/org/kkumulkkum/server/service/promise/PromiseService.java +++ b/src/main/java/org/kkumulkkum/server/service/promise/PromiseService.java @@ -4,6 +4,8 @@ import lombok.RequiredArgsConstructor; import org.kkumulkkum.server.domain.*; import org.kkumulkkum.server.dto.promise.PromiseCreateDto; +import org.kkumulkkum.server.dto.promise.response.MainPromiseDto; +import org.kkumulkkum.server.dto.promise.response.MainPromisesDto; import org.kkumulkkum.server.dto.promise.response.PromiseDto; import org.kkumulkkum.server.dto.promise.response.PromisesDto; import org.kkumulkkum.server.exception.PromiseException; @@ -15,6 +17,7 @@ import org.springframework.transaction.annotation.Transactional; import java.time.Duration; +import java.time.LocalDate; import java.time.LocalDateTime; import java.util.List; @@ -91,6 +94,29 @@ public PromiseDto getPromise( return PromiseDto.from(promise); } + @Transactional(readOnly = true) + public MainPromiseDto getNextPromise(final Long userId) { + LocalDateTime startOfDay = LocalDate.now().atStartOfDay(); + LocalDateTime startOfNextDay = startOfDay.plusDays(1); + List promise = promiseRetriever.findNextPromiseByUserId(userId, startOfDay, startOfNextDay); + if (promise.isEmpty()) { + return null; + } + return MainPromiseDto.from(promise.get(0)); + } + + @Transactional(readOnly = true) + public MainPromisesDto getUpcomingPromises(final Long userId) { + LocalDateTime startOfDay = LocalDate.now().atStartOfDay(); + LocalDateTime startOfNextDay = startOfDay.plusDays(1); + List nextPromise = promiseRetriever.findNextPromiseByUserId(userId, startOfDay, startOfNextDay); + + if (!nextPromise.isEmpty()) { + return MainPromisesDto.from(promiseRetriever.findUpcomingPromisesExcludingNext(userId, nextPromise.get(0), 4)); + } + return MainPromisesDto.from(promiseRetriever.findUpcomingPromises(userId, 4)); + } + private void updateUserInfo(Participant participant, LocalDateTime promiseTime) { UserInfo userInfo = userInfoRetriever.findByParticipantId(participant.getId()); userInfo.addPromiseCount();