Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature, test: 상세 조회- 보유수량 및 roomCode, 장바구니 및 예약 API 수정 #112

Merged
merged 5 commits into from
Dec 12, 2023
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 @@ -5,16 +5,16 @@
import jakarta.validation.constraints.Pattern;
import lombok.Builder;

public record CartCreateRequest(@NotNull Long roomId,
public record CartCreateRequest(@NotNull Long roomCode,

@Pattern(regexp = DateTimeUtil.LOCAL_DATE_REGEX_PATTERN, message = "잘못된 시간 형식입니다. (올바른 예시: 2023-10-25)") String startDate,
@Pattern(regexp = DateTimeUtil.LOCAL_DATE_REGEX_PATTERN, message = "잘못된 시간 형식입니다. (올바른 예시: 2023-10-25)") String endDate,
@NotNull
Long price) {

@Builder
public CartCreateRequest(Long roomId, String startDate, String endDate, Long price) {
this.roomId = roomId;
public CartCreateRequest(Long roomCode, String startDate, String endDate, Long price) {
this.roomCode = roomCode;
this.startDate = startDate;
this.endDate = endDate;
this.price = price;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import lombok.Builder;

public record CartDeleteResponse(Long cartId, Long roomId, String startDate, String endDate) {
public record CartDeleteResponse(Long cartId, Long roomCode, String startDate, String endDate) {

@Builder
public CartDeleteResponse(Long cartId, Long roomId, String startDate, String endDate) {
public CartDeleteResponse(Long cartId, Long roomCode, String startDate, String endDate) {
this.cartId = cartId;
this.roomId = roomId;
this.roomCode = roomCode;
this.startDate = startDate;
this.endDate = endDate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class CartResponse {
private final Long productId;
private final String productName;
private final String image;
private final Long roomId;
private final Long roomCode;
private final String roomName;
private final Long price;
private final String description;
Expand All @@ -21,21 +21,17 @@ public class CartResponse {
private final String endDate;
private final String checkIn;
private final String checkOut;
private Boolean reserved = false;

public void setReserved(){
reserved = true;
}

@Builder
public CartResponse(Long cartId, Long productId, String productName, String image, Long roomId,
public CartResponse(Long cartId, Long productId, String productName, String image,
Long roomCode,
String roomName, Long price, String description, Long standard, Long capacity,
String startDate, String endDate, String checkIn, String checkOut, Boolean reserved) {
String startDate, String endDate, String checkIn, String checkOut) {
this.cartId = cartId;
this.productId = productId;
this.productName = productName;
this.image = image;
this.roomId = roomId;
this.roomCode = roomCode;
this.roomName = roomName;
this.price = price;
this.description = description;
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/com/fc/shimpyo_be/domain/cart/entity/Cart.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.fc.shimpyo_be.domain.cart.entity;

import com.fc.shimpyo_be.domain.member.entity.Member;
import com.fc.shimpyo_be.domain.room.entity.Room;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand Down Expand Up @@ -30,10 +29,9 @@ public class Cart {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id", nullable = false)
private Member member;
@Comment(value = "객실 식별자")
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "room_id", nullable = false)
private Room room;
@Comment(value = "객실 코드")
@Column(nullable = false)
private Long roomCode;
@Comment(value = "총 이용 금액")
@Column(nullable = false)
private Long price;
Expand All @@ -46,8 +44,8 @@ public class Cart {
private LocalDate endDate;

@Builder
public Cart(Room room, Member member, Long price, LocalDate startDate, LocalDate endDate) {
this.room = room;
public Cart(Long roomCode, Member member, Long price, LocalDate startDate, LocalDate endDate) {
this.roomCode = roomCode;
this.member = member;
this.price = price;
this.startDate = startDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
public interface CartRepository extends JpaRepository<Cart, Long> {
Optional<List<Cart>> findByMemberId(Long memberId);

Long countByRoomCode(Long roomCode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
import com.fc.shimpyo_be.domain.member.entity.Member;
import com.fc.shimpyo_be.domain.member.exception.MemberNotFoundException;
import com.fc.shimpyo_be.domain.member.repository.MemberRepository;
import com.fc.shimpyo_be.domain.product.exception.RoomNotFoundException;
import com.fc.shimpyo_be.domain.product.exception.RoomNotReserveException;
import com.fc.shimpyo_be.domain.product.service.ProductService;
import com.fc.shimpyo_be.domain.room.entity.Room;
import com.fc.shimpyo_be.domain.room.repository.RoomRepository;
import com.fc.shimpyo_be.global.util.SecurityUtil;
import jakarta.validation.Valid;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -39,14 +39,13 @@ public class CartService {
private final ProductService productService;

public List<CartResponse> getCarts() {
List<CartResponse> cartResponses= cartRepository.findByMemberId(securityUtil.getCurrentMemberId()).orElseThrow()
.stream().map(CartMapper::toCartResponse).toList();
cartResponses.stream().filter(
cartResponse -> !productService.isAvailableForReservation(cartResponse.getRoomId(),
cartResponse.getStartDate(), cartResponse.getEndDate())).forEach(
CartResponse::setReserved);

return cartResponses;
List<Cart> carts = cartRepository.findByMemberId(
securityUtil.getCurrentMemberId()).orElseThrow();

return carts.stream().map(this::getCartResponse).filter(
cartResponse ->
productService.countAvailableForReservationUsingRoomCode(cartResponse.getRoomCode(),
cartResponse.getStartDate(), cartResponse.getEndDate()) > 0).toList();
}

@Transactional
Expand All @@ -55,19 +54,23 @@ public CartResponse addCart(@Valid @RequestBody CartCreateRequest cartCreateRequ
Member member = memberRepository.findById(securityUtil.getCurrentMemberId())
.orElseThrow(MemberNotFoundException::new);

if (!productService.isAvailableForReservation(cartCreateRequest.roomId(),
cartCreateRequest.startDate(), cartCreateRequest.endDate())) {
Long countAvailableForReservation = productService.countAvailableForReservationUsingRoomCode(
cartCreateRequest.roomCode(),
cartCreateRequest.startDate(),
cartCreateRequest.endDate());

if (countAvailableForReservation <= 0
|| cartRepository.countByRoomCode(cartCreateRequest.roomCode()) + 1
> countAvailableForReservation) {
throw new RoomNotReserveException();
}

Room room = roomRepository.findById(cartCreateRequest.roomId())
.orElseThrow(RoomNotFoundException::new);
Cart createdCart = cartRepository.save(CartMapper.toCart(cartCreateRequest, room, member));
return CartMapper.toCartResponse(createdCart);
Cart createdCart = cartRepository.save(CartMapper.toCart(cartCreateRequest, member));
return getCartResponse(createdCart);
}

@Transactional
public CartDeleteResponse deleteCart(Long cartId) {
public CartDeleteResponse deleteCart(final Long cartId) {
Cart cart = cartRepository.findById(cartId).orElseThrow(CartNotFoundException::new);

if (!cart.getMember().getId().equals(securityUtil.getCurrentMemberId())) {
Expand All @@ -78,5 +81,12 @@ public CartDeleteResponse deleteCart(Long cartId) {
return CartMapper.toCartDeleteResponse(cart);
}

private CartResponse getCartResponse(final Cart cart) {
List<Room> rooms = Optional.of(roomRepository.findByCode(cart.getRoomCode()))
.orElseThrow();

return CartMapper.toCartResponse(cart, rooms.get(0));
}


}
11 changes: 5 additions & 6 deletions src/main/java/com/fc/shimpyo_be/domain/cart/util/CartMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@

public class CartMapper {

public static CartResponse toCartResponse(Cart cart) {
Room room = cart.getRoom();
public static CartResponse toCartResponse(Cart cart, Room room) {
Product product = room.getProduct();

return CartResponse.builder().cartId(cart.getId()).productId(product.getId())
.productName(product.getName()).image(product.getThumbnail()).roomId(room.getId())
.productName(product.getName()).image(product.getThumbnail()).roomCode(room.getCode())
.roomName(room.getName()).price(cart.getPrice()).description(room.getDescription())
.standard((long) room.getStandard()).capacity((long) room.getCapacity())
.startDate(DateTimeUtil.toString(cart.getStartDate()))
Expand All @@ -25,14 +24,14 @@ public static CartResponse toCartResponse(Cart cart) {
}

public static CartDeleteResponse toCartDeleteResponse(Cart cart) {
return CartDeleteResponse.builder().cartId(cart.getId()).roomId(cart.getRoom().getId())
return CartDeleteResponse.builder().cartId(cart.getId()).roomCode(cart.getRoomCode())
.startDate(DateTimeUtil.toString(cart.getStartDate()))
.endDate(DateTimeUtil.toString(cart.getEndDate())).build();
}

public static Cart toCart(CartCreateRequest cartCreateRequest, Room room, Member member) {
public static Cart toCart(CartCreateRequest cartCreateRequest, Member member) {
return Cart.builder()
.room(room)
.roomCode(cartCreateRequest.roomCode())
.member(member)
.price(cartCreateRequest.price())
.startDate(DateTimeUtil.toLocalDate(cartCreateRequest.startDate()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public Page<Product> findAllBySearchKeywordRequest(SearchKeywordRequest searchKe
.where(buildSearchConditions(searchKeywordRequest));

List<Product> content = query.fetch();
return PageableExecutionUtils.getPage(content, pageable, () -> countQuery.fetch().size());

return PageableExecutionUtils.getPage(content, pageable, () -> countQuery.fetchCount());
}

private BooleanExpression buildSearchConditions(SearchKeywordRequest searchKeywordRequest) {
Expand All @@ -66,7 +67,7 @@ private BooleanExpression buildSearchConditions(SearchKeywordRequest searchKeywo

if (searchKeywordRequest.address() != null) {
expressions.add(
address1.detailAddress.containsIgnoreCase(searchKeywordRequest.address()));
address1.address.containsIgnoreCase(searchKeywordRequest.address()));
}

if (searchKeywordRequest.category() != null && !searchKeywordRequest.category().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import com.fc.shimpyo_be.domain.product.repository.ProductCustomRepositoryImpl;
import com.fc.shimpyo_be.domain.product.repository.ProductRepository;
import com.fc.shimpyo_be.domain.product.util.ProductMapper;
import com.fc.shimpyo_be.domain.room.dto.response.RoomResponse;
import com.fc.shimpyo_be.domain.room.entity.Room;
import com.fc.shimpyo_be.domain.room.repository.RoomRepository;
import com.fc.shimpyo_be.global.util.DateTimeUtil;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -52,13 +54,26 @@ public ProductDetailsResponse getProductDetails(final Long productId, final Stri
.orElseThrow(ProductNotFoundException::new);
ProductDetailsResponse productDetailsResponse = ProductMapper.toProductDetailsResponse(
product);
productDetailsResponse.rooms().stream().filter(
roomResponse -> !isAvailableForReservation(roomResponse.getRoomId(), startDate,
endDate))
.forEach(RoomResponse::setReserved);
productDetailsResponse.rooms().forEach(
roomResponse -> roomResponse.setRemaining(
countAvailableForReservationUsingRoomCode(roomResponse.getRoomCode(), startDate,
endDate)));
return productDetailsResponse;
}

public long countAvailableForReservationUsingRoomCode(final Long roomCode, final String startDate,
final String endDate) {
AtomicLong remaining = new AtomicLong();
List<Room> rooms = Optional.of(roomRepository.findByCode(roomCode)).orElseThrow();
rooms.forEach(room -> {
if (isAvailableForReservation(room.getId(), startDate, endDate)) {
remaining.getAndIncrement();
}
});

return remaining.get();
}

public boolean isAvailableForReservation(final Long roomId, final String startDate,
final String endDate) {
ValueOperations<String, Object> values = restTemplate.opsForValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static ProductDetailsResponse toProductDetailsResponse(Product product) {
.productOptionResponse(toProductOptionResponse(product.getProductOption()))
.favorites(false)
.images(images)
.rooms(product.getRooms().stream().map(RoomMapper::toRoomResponse).toList())
.rooms(product.getRooms().stream().map(RoomMapper::toRoomResponse).distinct().toList())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import lombok.Builder;
import lombok.Getter;

@Getter
public class RoomResponse {

private final Long roomId;
private final Long roomCode;
private final String roomName;
private final Long price;
private final String description;
Expand All @@ -18,17 +19,18 @@ public class RoomResponse {
private final String checkOut;
private final RoomOptionResponse roomOptionResponse;
private final List<String> roomImages;
private Boolean reserved;
private Long remaining;

@Builder
public RoomResponse(Long roomId, String roomName, Long price, String description, Long standard,
Long capacity, String checkIn, String checkOut, Boolean reserved,
public RoomResponse(Long roomCode, String roomName, Long price, String description,
Long standard,
Long capacity, String checkIn, String checkOut, Long remaining,
RoomOptionResponse roomOptionResponse,
List<String> roomImages) {
this.roomId = roomId;
this.roomCode = roomCode;
this.roomName = roomName;
this.price = price;
this.reserved = reserved;
this.remaining = remaining;
this.description = description;
this.standard = standard;
this.capacity = capacity;
Expand All @@ -42,8 +44,26 @@ public RoomResponse(Long roomId, String roomName, Long price, String description
}
}

public void setReserved() {
reserved = true;

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
RoomResponse otherRoomResponse = (RoomResponse) obj;
return Objects.equals(roomCode, otherRoomResponse.roomCode);
}

@Override
public int hashCode() {
return Objects.hash(roomCode);
}

public void setRemaining(long remaining) {
this.remaining = remaining;
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.fc.shimpyo_be.domain.room.repository;

import com.fc.shimpyo_be.domain.room.entity.Room;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface RoomRepository extends JpaRepository<Room, Long> {

List<Room> findByCode(long code);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ public static RoomResponse toRoomResponse(Room room) {
price = price == 0 ? 100000 : price;

return RoomResponse.builder()
.roomId(room.getId())
.roomCode(room.getCode())
.roomName(room.getName())
.price(price)
.standard((long) (room.getStandard()))
.capacity((long) room.getCapacity())
.description(room.getDescription())
.reserved(false)
.checkIn(room.getCheckIn().toString())
.checkOut(room.getCheckOut().toString())
.roomOptionResponse(toRoomOptionResponse(room.getRoomOption()))
.roomImages(room.getRoomImages().stream().map(RoomImage::getPhotoUrl).toList())
.remaining(0L)
.build();
}

Expand Down
Loading