From ec238724faed9e8ad8ea3ad867a8095742704829 Mon Sep 17 00:00:00 2001 From: chaewonkim Date: Fri, 12 Jul 2024 16:49:28 +0900 Subject: [PATCH 1/5] [feat] #40 create search the closest promise today --- .../server/controller/PromiseController.java | 7 ++++ .../dto/promise/response/MainPromiseDto.java | 33 +++++++++++++++++++ .../server/repository/PromiseRepository.java | 16 +++++++++ .../service/promise/PromiseRetriever.java | 11 +++++++ .../service/promise/PromiseService.java | 11 ++++++- 5 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/kkumulkkum/server/dto/promise/response/MainPromiseDto.java diff --git a/src/main/java/org/kkumulkkum/server/controller/PromiseController.java b/src/main/java/org/kkumulkkum/server/controller/PromiseController.java index c0be657..889b99c 100644 --- a/src/main/java/org/kkumulkkum/server/controller/PromiseController.java +++ b/src/main/java/org/kkumulkkum/server/controller/PromiseController.java @@ -5,6 +5,7 @@ import org.kkumulkkum.server.annotation.IsMember; 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.PromiseDto; import org.kkumulkkum.server.dto.promise.response.PromisesDto; import org.kkumulkkum.server.service.promise.PromiseService; @@ -57,4 +58,10 @@ 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)); + } } 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/repository/PromiseRepository.java b/src/main/java/org/kkumulkkum/server/repository/PromiseRepository.java index 8ec5b4a..3c00290 100644 --- a/src/main/java/org/kkumulkkum/server/repository/PromiseRepository.java +++ b/src/main/java/org/kkumulkkum/server/repository/PromiseRepository.java @@ -1,11 +1,27 @@ 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") + Page findNextPromiseByUserId(Long userId, LocalDateTime startOfDay, LocalDateTime startOfNextDay, Pageable pageable); + } 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 ec9ae7c..c559897 100644 --- a/src/main/java/org/kkumulkkum/server/service/promise/PromiseRetriever.java +++ b/src/main/java/org/kkumulkkum/server/service/promise/PromiseRetriever.java @@ -5,8 +5,11 @@ 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; @Component @@ -23,4 +26,12 @@ public Promise findById(Long id) { return promiseRepository.findById(id) .orElseThrow(() -> new PromiseException(PromiseErrorCode.NOT_FOUND_PROMISE)); } + + public Promise findNextPromiseByUserId(Long userId, LocalDateTime startOfDay, LocalDateTime startOfNextDay) { + Page promisePage = promiseRepository.findNextPromiseByUserId(userId, startOfDay, startOfNextDay, PageRequest.of(0,1)); + + return promisePage.stream() + .findFirst() + .orElseThrow(() -> new PromiseException(PromiseErrorCode.NOT_FOUND_PROMISE)); + } } 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 84cea51..ae640aa 100644 --- a/src/main/java/org/kkumulkkum/server/service/promise/PromiseService.java +++ b/src/main/java/org/kkumulkkum/server/service/promise/PromiseService.java @@ -7,13 +7,15 @@ import org.kkumulkkum.server.domain.Participant; import org.kkumulkkum.server.domain.Promise; import org.kkumulkkum.server.dto.promise.PromiseCreateDto; +import org.kkumulkkum.server.dto.promise.response.MainPromiseDto; import org.kkumulkkum.server.dto.promise.response.PromiseDto; import org.kkumulkkum.server.dto.promise.response.PromisesDto; import org.kkumulkkum.server.service.participant.ParticipantSaver; -import org.kkumulkkum.server.service.member.MemberRetreiver; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.List; @Service @@ -77,4 +79,11 @@ public PromiseDto getPromise( Promise promise = promiseRetriever.findById(promiseId); return PromiseDto.from(promise); } + + @Transactional(readOnly = true) + public MainPromiseDto getNextPromise(final Long userId) { + LocalDateTime startOfDay = LocalDate.now().atStartOfDay(); + LocalDateTime startOfNextDay = startOfDay.plusDays(1); + return MainPromiseDto.from(promiseRetriever.findNextPromiseByUserId(userId, startOfDay, startOfNextDay)); + } } From 9829c13ad4d7edabf90de4080ea22282c1f0b3b9 Mon Sep 17 00:00:00 2001 From: chaewonkim Date: Fri, 12 Jul 2024 16:53:09 +0900 Subject: [PATCH 2/5] [feat] #40 create search list of upcoming promises --- .../server/controller/PromiseController.java | 8 ++++++++ .../dto/promise/response/MainPromisesDto.java | 17 +++++++++++++++++ .../server/repository/PromiseRepository.java | 18 ++++++++++++++++++ .../service/promise/PromiseRetriever.java | 11 +++++++++++ .../server/service/promise/PromiseService.java | 15 +++++++++++++++ 5 files changed, 69 insertions(+) create mode 100644 src/main/java/org/kkumulkkum/server/dto/promise/response/MainPromisesDto.java diff --git a/src/main/java/org/kkumulkkum/server/controller/PromiseController.java b/src/main/java/org/kkumulkkum/server/controller/PromiseController.java index 889b99c..a88e64f 100644 --- a/src/main/java/org/kkumulkkum/server/controller/PromiseController.java +++ b/src/main/java/org/kkumulkkum/server/controller/PromiseController.java @@ -6,6 +6,7 @@ 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; @@ -64,4 +65,11 @@ public ResponseEntity getNextPromise( ) { 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/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 3c00290..4c84ac3 100644 --- a/src/main/java/org/kkumulkkum/server/repository/PromiseRepository.java +++ b/src/main/java/org/kkumulkkum/server/repository/PromiseRepository.java @@ -24,4 +24,22 @@ public interface PromiseRepository extends JpaRepository { "ORDER BY p.time ASC, p.createdAt ASC") Page 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); } 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 c559897..fc587b3 100644 --- a/src/main/java/org/kkumulkkum/server/service/promise/PromiseRetriever.java +++ b/src/main/java/org/kkumulkkum/server/service/promise/PromiseRetriever.java @@ -11,6 +11,7 @@ import java.time.LocalDateTime; import java.util.List; +import java.util.stream.Collectors; @Component @RequiredArgsConstructor @@ -34,4 +35,14 @@ public Promise findNextPromiseByUserId(Long userId, LocalDateTime startOfDay, Lo .findFirst() .orElseThrow(() -> new PromiseException(PromiseErrorCode.NOT_FOUND_PROMISE)); } + + 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()); + } } 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 ae640aa..c0189a4 100644 --- a/src/main/java/org/kkumulkkum/server/service/promise/PromiseService.java +++ b/src/main/java/org/kkumulkkum/server/service/promise/PromiseService.java @@ -8,8 +8,10 @@ import org.kkumulkkum.server.domain.Promise; 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; import org.kkumulkkum.server.service.participant.ParticipantSaver; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -86,4 +88,17 @@ public MainPromiseDto getNextPromise(final Long userId) { LocalDateTime startOfNextDay = startOfDay.plusDays(1); return MainPromiseDto.from(promiseRetriever.findNextPromiseByUserId(userId, startOfDay, startOfNextDay)); } + + @Transactional(readOnly = true) + public MainPromisesDto getUpcomingPromises(final Long userId) { + LocalDateTime startOfDay = LocalDate.now().atStartOfDay(); + LocalDateTime startOfNextDay = startOfDay.plusDays(1); + try { + Promise nextPromise = promiseRetriever.findNextPromiseByUserId(userId, startOfDay, startOfNextDay); + return MainPromisesDto.from(promiseRetriever.findUpcomingPromisesExcludingNext(userId, nextPromise, 4)); + } catch (PromiseException e) { + // 오늘의 약속이 없는 경우 + return MainPromisesDto.from(promiseRetriever.findUpcomingPromises(userId, 4)); + } + } } From c15c2f1ae536835e0e722baa36722a4fc1b65330 Mon Sep 17 00:00:00 2001 From: tkdwns414 Date: Fri, 12 Jul 2024 22:33:22 +0900 Subject: [PATCH 3/5] [feat] #40 edit today's next promise logic --- .../server/repository/PromiseRepository.java | 2 +- .../service/promise/PromiseRetriever.java | 7 ++----- .../server/service/promise/PromiseService.java | 17 +++++++++-------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/kkumulkkum/server/repository/PromiseRepository.java b/src/main/java/org/kkumulkkum/server/repository/PromiseRepository.java index 4c84ac3..51a8256 100644 --- a/src/main/java/org/kkumulkkum/server/repository/PromiseRepository.java +++ b/src/main/java/org/kkumulkkum/server/repository/PromiseRepository.java @@ -22,7 +22,7 @@ public interface PromiseRepository extends JpaRepository { "AND p.time > CURRENT_TIMESTAMP " + "AND p.isCompleted = false " + "ORDER BY p.time ASC, p.createdAt ASC") - Page findNextPromiseByUserId(Long userId, LocalDateTime startOfDay, LocalDateTime startOfNextDay, Pageable pageable); + List findNextPromiseByUserId(Long userId, LocalDateTime startOfDay, LocalDateTime startOfNextDay, Pageable pageable); @Query("SELECT p FROM Participant pt " + "JOIN pt.member m " + 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 fc587b3..744669d 100644 --- a/src/main/java/org/kkumulkkum/server/service/promise/PromiseRetriever.java +++ b/src/main/java/org/kkumulkkum/server/service/promise/PromiseRetriever.java @@ -28,12 +28,9 @@ public Promise findById(Long id) { .orElseThrow(() -> new PromiseException(PromiseErrorCode.NOT_FOUND_PROMISE)); } - public Promise findNextPromiseByUserId(Long userId, LocalDateTime startOfDay, LocalDateTime startOfNextDay) { - Page promisePage = promiseRepository.findNextPromiseByUserId(userId, startOfDay, startOfNextDay, PageRequest.of(0,1)); + public List findNextPromiseByUserId(Long userId, LocalDateTime startOfDay, LocalDateTime startOfNextDay) { + return promiseRepository.findNextPromiseByUserId(userId, startOfDay, startOfNextDay, PageRequest.of(0,1)); - return promisePage.stream() - .findFirst() - .orElseThrow(() -> new PromiseException(PromiseErrorCode.NOT_FOUND_PROMISE)); } public List findUpcomingPromisesExcludingNext(Long userId, Promise nextPromise, int limit) { 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 c0189a4..bce3dd7 100644 --- a/src/main/java/org/kkumulkkum/server/service/promise/PromiseService.java +++ b/src/main/java/org/kkumulkkum/server/service/promise/PromiseService.java @@ -12,7 +12,9 @@ import org.kkumulkkum.server.dto.promise.response.PromiseDto; import org.kkumulkkum.server.dto.promise.response.PromisesDto; import org.kkumulkkum.server.exception.PromiseException; +import org.kkumulkkum.server.exception.code.PromiseErrorCode; import org.kkumulkkum.server.service.participant.ParticipantSaver; +import org.springframework.data.domain.Page; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -86,19 +88,18 @@ public PromiseDto getPromise( public MainPromiseDto getNextPromise(final Long userId) { LocalDateTime startOfDay = LocalDate.now().atStartOfDay(); LocalDateTime startOfNextDay = startOfDay.plusDays(1); - return MainPromiseDto.from(promiseRetriever.findNextPromiseByUserId(userId, startOfDay, startOfNextDay)); + 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); - try { - Promise nextPromise = promiseRetriever.findNextPromiseByUserId(userId, startOfDay, startOfNextDay); - return MainPromisesDto.from(promiseRetriever.findUpcomingPromisesExcludingNext(userId, nextPromise, 4)); - } catch (PromiseException e) { - // 오늘의 약속이 없는 경우 - return MainPromisesDto.from(promiseRetriever.findUpcomingPromises(userId, 4)); - } + Promise nextPromise = promiseRetriever.findNextPromiseByUserId(userId, startOfDay, startOfNextDay).get(0); + return MainPromisesDto.from(promiseRetriever.findUpcomingPromisesExcludingNext(userId, nextPromise,4)); } } From 2c5fa4612da59170d7aece2a822d260ddbdef0c8 Mon Sep 17 00:00:00 2001 From: chaewonkim Date: Fri, 12 Jul 2024 22:47:12 +0900 Subject: [PATCH 4/5] [fix] #40 edit upcoming promises logic --- .../kkumulkkum/server/service/promise/PromiseService.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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 bce3dd7..8511845 100644 --- a/src/main/java/org/kkumulkkum/server/service/promise/PromiseService.java +++ b/src/main/java/org/kkumulkkum/server/service/promise/PromiseService.java @@ -99,7 +99,11 @@ public MainPromiseDto getNextPromise(final Long userId) { public MainPromisesDto getUpcomingPromises(final Long userId) { LocalDateTime startOfDay = LocalDate.now().atStartOfDay(); LocalDateTime startOfNextDay = startOfDay.plusDays(1); - Promise nextPromise = promiseRetriever.findNextPromiseByUserId(userId, startOfDay, startOfNextDay).get(0); - return MainPromisesDto.from(promiseRetriever.findUpcomingPromisesExcludingNext(userId, nextPromise,4)); + 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)); } } From d6a4ac3a69160dc8d9007b5374d3ab334057ef16 Mon Sep 17 00:00:00 2001 From: tkdwns414 Date: Fri, 12 Jul 2024 23:06:17 +0900 Subject: [PATCH 5/5] [fix] #40 add missed brace in code --- .../org/kkumulkkum/server/service/promise/PromiseRetriever.java | 1 + 1 file changed, 1 insertion(+) 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 3b0c99c..cc4f344 100644 --- a/src/main/java/org/kkumulkkum/server/service/promise/PromiseRetriever.java +++ b/src/main/java/org/kkumulkkum/server/service/promise/PromiseRetriever.java @@ -41,6 +41,7 @@ public List findUpcomingPromisesExcludingNext(Long userId, Promise next 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);