Skip to content

Commit

Permalink
[Weekly/11/Test/event] LikeController 반환값 수정, Event Controller에 모든 이벤…
Browse files Browse the repository at this point in the history
…트 반환 api 추가 (#123)

* [Weekly/10/Test/CurationController] curationControllerUnitTest 2 (#90)

* feat: ReservationEventResponse에 startTime 반환 추가

* feat: Like Controller반환값 LikeSliceResponse으로 변경

* feat: 생성 순서대로 Event 반환하는 api 추가

---------

Co-authored-by: 조홍식 <[email protected]>
  • Loading branch information
ariimo and Daolove0323 authored Nov 13, 2024
1 parent 2b5e639 commit 2e09f7d
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ public ResponseEntity<ApiResponseBody<EventSliceResponse>> getEventsByHostId(
hostId, PageRequest.of(page, size), lastId));
}

@GetMapping
public ResponseEntity<ApiResponseBody<EventSliceResponse>> 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<ApiResponseBody<EventResponse>> getEventByEventId(
@PathVariable("eventId") Long eventId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Event> events = eventRepository.findAllByEventIdDesc(beforeLastId, pageable);
Long newLastId = getLastId(events, beforeLastId);
List<EventResponse> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ public interface EventRepository extends JpaRepository<Event, Long> {

@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<Event> 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<Event> 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) "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +27,7 @@ public class LikeController {
private final LikeServiceFactory likeServiceFactory;

@GetMapping
public ResponseEntity<ApiResponseBody<Slice<LikeResponse>>> getLikes(
public ResponseEntity<ApiResponseBody<LikeSliceResponse>> getLikes(
@Authorize(MemberType.normal) MemberIdentifier identifier,
@RequestParam("type") MemberType memberType,
@RequestParam(defaultValue = ParamDefaults.PAGE) Integer page,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<LikeResponse> likes;
private SliceInfo sliceInfo;

public static LikeSliceResponse from(List<LikeResponse> likes, int size, Long lastId) {
return LikeSliceResponse.builder()
.likes(likes)
.sliceInfo(SliceInfo.builder()
.sliceSize(size)
.lastId(lastId)
.build())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -29,10 +31,13 @@ public abstract class LikeService<LikeType extends Like<? extends LikeableMember
public abstract MemberType getTargetLikeableMemberType();

@Transactional(readOnly = true)
public Slice<LikeResponse> 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<LikeType> likes = getLikeRepository().findAllByMember(
memberService.getByIdOrThrow(memberId), beforeLastId, pageable);
Long newLastId = getLastId(likes, beforeLastId);
List<LikeResponse> responses = likes.stream()
.map(like -> LikeResponse.from(like.getLikeableMember())).toList();
return LikeSliceResponse.from(responses, likes.getSize(), newLastId);
}

@Transactional
Expand All @@ -57,4 +62,11 @@ protected LikeableMember getLikeableMemberByIdOrThrow(Long likeableMemberId) {
return likeableMemberGetterFactory.get(getTargetLikeableMemberType())
.getByIdOrThrow(likeableMemberId);
}

private Long getLastId(Slice<LikeType> likes, Long oldLastId) {
if (likes.hasContent()) {
return likes.getContent().getLast().getId();
}
return oldLastId;
}
}

0 comments on commit 2e09f7d

Please sign in to comment.