diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/event/api/EventController.java b/src/main/java/org/ktc2/cokaen/wouldyouin/event/api/EventController.java index d5c312d1..66deac28 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/event/api/EventController.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/event/api/EventController.java @@ -64,6 +64,16 @@ public ResponseEntity> getEventsByHostId( hostId, PageRequest.of(page, size), lastId)); } + @GetMapping + public ResponseEntity> getEvents( + @RequestParam(defaultValue = ParamDefaults.PAGE) Integer page, + @RequestParam(defaultValue = ParamDefaults.PAGE_SIZE) Integer size, + @RequestParam(defaultValue = ParamDefaults.LAST_ID) Long lastId + ) { + return ApiResponse.ok(eventService.getAllByCreatedDateDesc( + PageRequest.of(page, size), lastId)); + } + @GetMapping("/{eventId}") public ResponseEntity> getEventByEventId( @PathVariable("eventId") Long eventId) { diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/event/api/dto/relationResonse/ReservationEventResponse.java b/src/main/java/org/ktc2/cokaen/wouldyouin/event/api/dto/relationResonse/ReservationEventResponse.java index 98e76401..a74729ac 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/event/api/dto/relationResonse/ReservationEventResponse.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/event/api/dto/relationResonse/ReservationEventResponse.java @@ -1,5 +1,6 @@ package org.ktc2.cokaen.wouldyouin.event.api.dto.relationResonse; +import java.time.LocalDateTime; import lombok.Builder; import lombok.Getter; import org.ktc2.cokaen.wouldyouin._common.vo.Location; @@ -13,6 +14,7 @@ public class ReservationEventResponse { private String title; private Integer price; private Location location; + private LocalDateTime startTime; private String thumbnailUrl; public static ReservationEventResponse from(Event event) { @@ -21,6 +23,7 @@ public static ReservationEventResponse from(Event event) { .title(event.getTitle()) .price(event.getPrice()) .location(event.getLocation()) + .startTime(event.getStartTime()) .thumbnailUrl(event.getThumbnailUrl()) .build(); } diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/event/application/EventService.java b/src/main/java/org/ktc2/cokaen/wouldyouin/event/application/EventService.java index ff6c9f44..746b6694 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/event/application/EventService.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/event/application/EventService.java @@ -70,6 +70,14 @@ public EventSliceResponse getAllByHostIdOrderByCreatedDateDesc(Long hostId, Page return EventSliceResponse.from(responses, events.getSize(), newLastId); } + @Transactional(readOnly = true) + public EventSliceResponse getAllByCreatedDateDesc(Pageable pageable, Long beforeLastId) { + Slice events = eventRepository.findAllByEventIdDesc(beforeLastId, pageable); + Long newLastId = getLastId(events, beforeLastId); + List responses = events.stream().map(this::getEventResponse).toList(); + return EventSliceResponse.from(responses, events.getSize(), newLastId); + } + @Transactional public EventResponse create(MemberIdentifier identifier, EventCreateRequest eventCreateRequest) { Host host = hostService.getByIdOrThrow(identifier.id()); diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/event/persist/EventRepository.java b/src/main/java/org/ktc2/cokaen/wouldyouin/event/persist/EventRepository.java index efa63ff6..e8671f6c 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/event/persist/EventRepository.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/event/persist/EventRepository.java @@ -13,10 +13,15 @@ public interface EventRepository extends JpaRepository { @Query("SELECT E FROM Event E JOIN FETCH E.host " + "WHERE E.host.Id = :hostId " - + "AND E.host.Id > :lastId " + + "AND E.id > :lastId " + "ORDER BY E.id DESC") Slice findAllByHostIdOrderByEventIdDesc(Long hostId, Long lastId, Pageable pageable); + @Query("SELECT E FROM Event E JOIN FETCH E.host " + + "WHERE E.id > :lastId " + + "ORDER BY E.id DESC") + Slice findAllByEventIdDesc(Long lastId, Pageable pageable); + @Query("SELECT E FROM Event E JOIN FETCH E.host " + "WHERE (E.location.latitude between :startLatitude AND :endLatitude) " + "AND (E.location.longitude between :startLongitude AND :endLongitude) " diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/like/api/LikeController.java b/src/main/java/org/ktc2/cokaen/wouldyouin/like/api/LikeController.java index 0629424c..1f1712a6 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/like/api/LikeController.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/like/api/LikeController.java @@ -6,12 +6,11 @@ import org.ktc2.cokaen.wouldyouin._common.api.ParamDefaults; import org.ktc2.cokaen.wouldyouin.auth.Authorize; import org.ktc2.cokaen.wouldyouin.auth.MemberIdentifier; -import org.ktc2.cokaen.wouldyouin.like.application.LikeServiceFactory; -import org.ktc2.cokaen.wouldyouin.like.api.dto.LikeResponse; +import org.ktc2.cokaen.wouldyouin.like.api.dto.LikeSliceResponse; import org.ktc2.cokaen.wouldyouin.like.api.dto.LikeToggleResponse; +import org.ktc2.cokaen.wouldyouin.like.application.LikeServiceFactory; import org.ktc2.cokaen.wouldyouin.member.persist.MemberType; import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Slice; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -28,7 +27,7 @@ public class LikeController { private final LikeServiceFactory likeServiceFactory; @GetMapping - public ResponseEntity>> getLikes( + public ResponseEntity> getLikes( @Authorize(MemberType.normal) MemberIdentifier identifier, @RequestParam("type") MemberType memberType, @RequestParam(defaultValue = ParamDefaults.PAGE) Integer page, diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/like/api/dto/LikeSliceResponse.java b/src/main/java/org/ktc2/cokaen/wouldyouin/like/api/dto/LikeSliceResponse.java new file mode 100644 index 00000000..efb11c79 --- /dev/null +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/like/api/dto/LikeSliceResponse.java @@ -0,0 +1,24 @@ +package org.ktc2.cokaen.wouldyouin.like.api.dto; + +import java.util.List; +import lombok.Builder; +import lombok.Getter; +import org.ktc2.cokaen.wouldyouin._common.api.SliceInfo; + +@Getter +@Builder +public class LikeSliceResponse { + + private List likes; + private SliceInfo sliceInfo; + + public static LikeSliceResponse from(List likes, int size, Long lastId) { + return LikeSliceResponse.builder() + .likes(likes) + .sliceInfo(SliceInfo.builder() + .sliceSize(size) + .lastId(lastId) + .build()) + .build(); + } +} diff --git a/src/main/java/org/ktc2/cokaen/wouldyouin/like/application/LikeService.java b/src/main/java/org/ktc2/cokaen/wouldyouin/like/application/LikeService.java index 8a888bad..0d8dfbb9 100644 --- a/src/main/java/org/ktc2/cokaen/wouldyouin/like/application/LikeService.java +++ b/src/main/java/org/ktc2/cokaen/wouldyouin/like/application/LikeService.java @@ -1,7 +1,9 @@ package org.ktc2.cokaen.wouldyouin.like.application; +import java.util.List; import lombok.RequiredArgsConstructor; import org.ktc2.cokaen.wouldyouin.like.api.dto.LikeResponse; +import org.ktc2.cokaen.wouldyouin.like.api.dto.LikeSliceResponse; import org.ktc2.cokaen.wouldyouin.like.api.dto.LikeToggleResponse; import org.ktc2.cokaen.wouldyouin.like.persist.Like; import org.ktc2.cokaen.wouldyouin.like.persist.LikeRepository; @@ -29,10 +31,13 @@ public abstract class LikeService getLikes(Long memberId, Pageable pageable, Long lastId) { - return getLikeRepository().findAllByMember( - memberService.getByIdOrThrow(memberId), lastId, pageable) - .map(like -> LikeResponse.from(like.getLikeableMember())); + public LikeSliceResponse getLikes(Long memberId, Pageable pageable, Long beforeLastId) { + Slice likes = getLikeRepository().findAllByMember( + memberService.getByIdOrThrow(memberId), beforeLastId, pageable); + Long newLastId = getLastId(likes, beforeLastId); + List responses = likes.stream() + .map(like -> LikeResponse.from(like.getLikeableMember())).toList(); + return LikeSliceResponse.from(responses, likes.getSize(), newLastId); } @Transactional @@ -57,4 +62,11 @@ protected LikeableMember getLikeableMemberByIdOrThrow(Long likeableMemberId) { return likeableMemberGetterFactory.get(getTargetLikeableMemberType()) .getByIdOrThrow(likeableMemberId); } + + private Long getLastId(Slice likes, Long oldLastId) { + if (likes.hasContent()) { + return likes.getContent().getLast().getId(); + } + return oldLastId; + } }