Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import com.rentify.rentify_api.image.entity.Image;
import com.rentify.rentify_api.post.entity.Post;
import com.rentify.rentify_api.post.entity.PostStatus;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import com.rentify.rentify_api.rental.entity.Rental;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -32,26 +36,53 @@ public class PostDetailResponse {
private List<String> imageUrls;
private LocalDateTime createAt;
private LocalDateTime updateAt;
private List<RentalPeriod> rentalPeriods;

public static PostDetailResponse from(Post post) {
public static PostDetailResponse from(Post post, List<Rental> rentals) {
return PostDetailResponse.builder()
.postId(post.getId())
.userId(post.getUser().getId())
.categoryName(post.getCategory().getName())
.userName(post.getUser().getName())
.title(post.getTitle())
.description(post.getDescription())
.pricePerDay(post.getPricePerDay())
.maxRentalDays(post.getMaxRentalDays())
.isParcel(post.getIsParcel())
.isMeetup(post.getIsMeetup())
.status(post.getStatus())
.imageUrls(post.getImages()
.stream()
.map(Image::getUrl)
.collect(Collectors.toList()))
.createAt(post.getCreateAt())
.updateAt(post.getUpdateAt())
.build();
.postId(post.getId())
.userId(post.getUser().getId())
.categoryName(post.getCategory().getName())
.userName(post.getUser().getName())
.title(post.getTitle())
.description(post.getDescription())
.pricePerDay(post.getPricePerDay())
.maxRentalDays(post.getMaxRentalDays())
.isParcel(post.getIsParcel())
.isMeetup(post.getIsMeetup())
.status(post.getStatus())
.imageUrls(post.getImages()
.stream()
.map(Image::getUrl)
.collect(Collectors.toList()))
.createAt(post.getCreateAt())
.updateAt(post.getUpdateAt())
.rentalPeriods(rentals.stream()
.map(RentalPeriod::from)
.collect(Collectors.toList()))
.build();
}

// 기존 from 메서드 오버로드 (rental이 없는 경우 빈 리스트 사용)
public static PostDetailResponse from(Post post) {
return from(post, List.of());
}

@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public static class RentalPeriod {
private Long rentalId;
private LocalDate startDate;
private LocalDate endDate;

public static RentalPeriod from(Rental rental) {
return RentalPeriod.builder()
.rentalId(rental.getId())
.startDate(rental.getStartDate())
.endDate(rental.getEndDate())
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
import com.rentify.rentify_api.post.exception.PostNotFoundException;
import com.rentify.rentify_api.post.repository.PostHistoryRepository;
import com.rentify.rentify_api.post.repository.PostRepository;
import com.rentify.rentify_api.rental.entity.Rental;
import com.rentify.rentify_api.rental.repository.RentalRepository;
import com.rentify.rentify_api.user.entity.User;
import com.rentify.rentify_api.user.exception.UserNotFoundException;
import com.rentify.rentify_api.user.repository.UserRepository;

import java.time.LocalDate;
import java.util.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -42,6 +47,7 @@ public class PostService {
private static final Set<String> ALLOWED_SORT_FILTERS = Set.of(
"createAt", "pricePerDay", "title", "id"
);
private final RentalRepository rentalRepository;

@Transactional(readOnly = true)
public Page<PostDetailResponse> getPosts(
Expand Down Expand Up @@ -74,7 +80,11 @@ public PostDetailResponse getPost(Long postId) {
Post post = postRepository.findById(postId)
.orElseThrow(PostNotFoundException::new);

return PostDetailResponse.from(post);
// 현재 게시글에 적용되어있는 rental 조회
List<Rental> rentals = rentalRepository
.findFutureRentalsByPostId(postId, LocalDate.now());

return PostDetailResponse.from(post, rentals);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,37 @@ List<Rental> findOverlappingRentals(
countQuery = "SELECT count(r) FROM Rental r JOIN r.post po WHERE r.user.id = :userId OR po.user.id = :userId"
)
Page<Object[]> findByUserIdOrPostOwnerId(@Param("userId") Long userId, Pageable pageable);

/**
* 특정 Post의 현재 또는 미래 rental 데이터를 조회합니다.
* 종료일이 오늘 이상인 rentals만 반환합니다.
*/
@Query("SELECT r FROM Rental r " +
"WHERE r.post.id = :postId " +
"AND r.endDate >= :today " +
"AND r.status IN ('REQUESTED', 'CONFIRMED') " +
"ORDER BY r.startDate ASC")
List<Rental> findFutureRentalsByPostId(
@Param("postId") Long postId,
@Param("today") LocalDate today
);

/**
* 특정 Post의 모든 rental 데이터를 조회합니다.
*/
List<Rental> findByPostId(Long postId);

/**
* 특정 기간 동안의 rental 데이터를 조회합니다.
* (예약이 겹치는 날짜 확인 등에 사용)
*/
@Query("SELECT r FROM Rental r " +
"WHERE r.post.id = :postId " +
"AND r.startDate <= :endDate " +
"AND r.endDate >= :startDate")
List<Rental> findOverlappingRentals(
@Param("postId") Long postId,
@Param("startDate") LocalDate startDate,
@Param("endDate") LocalDate endDate
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
import com.rentify.rentify_api.post.entity.Post;
import com.rentify.rentify_api.post.repository.PostHistoryRepository;
import com.rentify.rentify_api.post.repository.PostRepository;
import com.rentify.rentify_api.rental.repository.RentalRepository;
import com.rentify.rentify_api.user.entity.User;
import com.rentify.rentify_api.user.exception.UserNotFoundException;
import com.rentify.rentify_api.user.repository.UserRepository;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.time.LocalDate;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -47,6 +49,8 @@ class PostServiceTest {
@Mock
private UserRepository userRepository;
@Mock
private RentalRepository rentalRepository;
@Mock
private CategoryRepository categoryRepository;
@Mock
private ImageService imageService;
Expand Down Expand Up @@ -179,7 +183,8 @@ void get_post_success() {
.build();

given(postRepository.findById(postId)).willReturn(Optional.of(mockPost));

given(rentalRepository.findFutureRentalsByPostId(postId, LocalDate.now()))
.willReturn(List.of());
// when
PostDetailResponse response = postService.getPost(postId);

Expand All @@ -194,6 +199,7 @@ void get_post_success() {
"http://test.com/1.jpg",
"http://test.com/2.jpg"
);
assertThat(response.getRentalPeriods()).isNotNull();

// verify
verify(postRepository, times(1)).findById(any());
Expand Down