Skip to content

Commit

Permalink
[feat] 메인홈 약속 조회 관련 API 구현 (#47)
Browse files Browse the repository at this point in the history
* [feat] #40 create search the closest promise today

* [feat] #40 create search list of upcoming promises

* [feat] #40 edit today's next promise logic

* [fix] #40 edit upcoming promises logic

* [fix] #40 add missed brace in code

---------

Co-authored-by: tkdwns414 <[email protected]>
Co-authored-by: Sangjune park <[email protected]>
  • Loading branch information
3 people authored Jul 12, 2024
1 parent e3f9707 commit b4132ac
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,4 +60,17 @@ public ResponseEntity<PromiseDto> getPromise(
return ResponseEntity.ok().body(promiseService.getPromise(promiseId));
}

@GetMapping("/promises/today/next")
public ResponseEntity<MainPromiseDto> getNextPromise(
@UserId final Long userId
) {
return ResponseEntity.ok().body(promiseService.getNextPromise(userId));
}

@GetMapping("/promises/upcoming")
public ResponseEntity<MainPromisesDto> getUpcomingPromise(
@UserId final Long userId
) {
return ResponseEntity.ok().body(promiseService.getUpcomingPromises(userId));
}
}
Original file line number Diff line number Diff line change
@@ -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()
);
}
}
Original file line number Diff line number Diff line change
@@ -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<MainPromiseDto> promises
) {
public static MainPromisesDto from(List<Promise> promises) {
return new MainPromisesDto(
promises.stream()
.map(MainPromiseDto::from)
.toList()
);
}
}
Original file line number Diff line number Diff line change
@@ -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<Promise, Long> {

List<Promise> 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<Promise> 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<Promise> 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<Promise> 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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,7 +28,23 @@ public Promise findById(Long id) {
.orElseThrow(() -> new PromiseException(PromiseErrorCode.NOT_FOUND_PROMISE));
}

public List<Promise> findNextPromiseByUserId(Long userId, LocalDateTime startOfDay, LocalDateTime startOfNextDay) {
return promiseRepository.findNextPromiseByUserId(userId, startOfDay, startOfNextDay, PageRequest.of(0,1));

}

public List<Promise> findUpcomingPromisesExcludingNext(Long userId, Promise nextPromise, int limit) {
Page<Promise> promisePage = promiseRepository.findUpcomingPromisesExcludingNext(userId, nextPromise.getId(), PageRequest.of(0, limit));
return promisePage.stream().collect(Collectors.toList());
}

public List<Promise> findUpcomingPromises(Long userId, int limit) {
Page<Promise> promisePage = promiseRepository.findUpcomingPromises(userId, PageRequest.of(0, limit));
return promisePage.stream().collect(Collectors.toList());
}

public boolean existsByArrivedAtIsNull(Long promiseId) {
return promiseRepository.existsByArrivedAtIsNull(promiseId);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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> 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<Promise> 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();
Expand Down

0 comments on commit b4132ac

Please sign in to comment.