Skip to content

Commit

Permalink
feat: 장바구니 추가 기능 구현 (9oormthon-univ#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
LEEJaeHyeok97 authored Nov 19, 2024
1 parent a9f6131 commit bede955
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.jangburich.domain.order.application;

import com.jangburich.domain.menu.domain.Menu;
import com.jangburich.domain.menu.domain.repository.MenuRepository;
import com.jangburich.domain.order.domain.Cart;
import com.jangburich.domain.order.domain.repository.CartRepository;
import com.jangburich.domain.order.domain.repository.OrdersRepository;
import com.jangburich.domain.order.dto.request.AddCartRequest;
import com.jangburich.domain.store.domain.Store;
import com.jangburich.domain.store.domain.repository.StoreRepository;
import com.jangburich.domain.user.domain.User;
import com.jangburich.domain.user.domain.repository.UserRepository;
import com.jangburich.global.payload.Message;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class OrderService {

private final CartRepository cartRepository;
private final UserRepository userRepository;
private final MenuRepository menuRepository;
private final StoreRepository storeRepository;

@Transactional
public Message addCart(String userProviderId, AddCartRequest addCartRequest) {
User user = userRepository.findByProviderId(userProviderId)
.orElseThrow(() -> new NullPointerException());

Menu menu = menuRepository.findById(addCartRequest.menuId())
.orElseThrow(() -> new IllegalArgumentException("등록된 메뉴를 찾을 수 없습니다."));

System.out.println("menu.getId() = " + menu.getId());
System.out.println("user.getUserId() = " + user.getUserId());

Optional<Cart> optionalCart = cartRepository.findByUserIdAndMenuId(user.getUserId(), menu.getId());

Store store = storeRepository.findById(addCartRequest.storeId())
.orElseThrow(() -> new IllegalArgumentException("유효하지 않은 가게 id 입니다."));

if (optionalCart.isPresent()) {
Cart existingCart = optionalCart.get();
existingCart.updateQuantity(addCartRequest.quantity());

return Message.builder()
.message("장바구니에 상품을 추가했습니다.")
.build();
}

Cart newCart = Cart.builder()
.quantity(addCartRequest.quantity())
.menu(menu)
.user(user)
.store(store)
.orders(null)
.build();

cartRepository.save(newCart);

return Message.builder()
.message("장바구니에 상품을 추가했습니다.")
.build();
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/jangburich/domain/order/domain/Cart.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.jangburich.domain.common.BaseEntity;
import com.jangburich.domain.menu.domain.Menu;
import com.jangburich.domain.store.domain.Store;
import com.jangburich.domain.user.domain.User;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand All @@ -10,7 +12,9 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import java.util.List;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -34,4 +38,26 @@ public class Cart extends BaseEntity {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "menu_id")
private Menu menu;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "store_id")
private Store store;


@Builder
public Cart(Integer quantity, Orders orders, Menu menu, User user, Store store) {
this.quantity = quantity;
this.orders = orders;
this.menu = menu;
this.user = user;
this.store = store;
}

public void updateQuantity(int quantity) {
this.quantity += quantity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.jangburich.domain.order.domain.repository;

import com.jangburich.domain.order.domain.Cart;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface CartRepository extends JpaRepository<Cart, Long> {
@Query("SELECT c FROM Cart c WHERE c.user.userId = :userId AND c.menu.id = :menuId")
Optional<Cart> findByUserIdAndMenuId(@Param("userId") Long userId, @Param("menuId") Long menuId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.jangburich.domain.order.domain.repository;

import com.jangburich.domain.order.domain.Orders;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface OrdersRepository extends JpaRepository<Orders, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.jangburich.domain.order.dto.request;

public record AddCartRequest(
Long storeId,
Long menuId,
int quantity
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.jangburich.domain.order.presentation;

import com.jangburich.domain.order.application.OrderService;
import com.jangburich.domain.order.dto.request.AddCartRequest;
import com.jangburich.global.payload.Message;
import com.jangburich.global.payload.ResponseCustom;
import com.jangburich.utils.parser.AuthenticationParser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Order", description = "Order API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/orders")
public class OrderController {

private final OrderService orderService;

@Operation(summary = "장바구니 담기", description = "장바구니에 물건과 수량을 담습니다.")
@PostMapping("/cart")
public ResponseCustom<Message> addCart(
Authentication authentication,
@RequestBody AddCartRequest addCartRequest
) {
return ResponseCustom.OK(orderService.addCart(AuthenticationParser.parseUserId(authentication), addCartRequest));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ public Page<SearchStoresResponse> findStoresByCategory(Long userId, Integer sear
double myCurrentLat = storeSearchCondition.lat();
double myCurrentLon = storeSearchCondition.lon();

BooleanExpression categoryCondition = isAllCategory(category);

List<SearchStoresResponse> results = queryFactory
.select(new QSearchStoresResponse(store.id, store.name, Expressions.FALSE, store.category,
Expressions.constant(1.0), Expressions.constant("open"),
store.closeTime.stringValue(), store.contactNumber, store.representativeImage))
.from(store)
.where(
store.category.eq(category),
categoryCondition,
withinSearchRadius(myCurrentLat, myCurrentLon, searchRadius, store.latitude, store.longitude)
)
.orderBy(store.id.desc())
Expand All @@ -52,7 +54,7 @@ public Page<SearchStoresResponse> findStoresByCategory(Long userId, Integer sear
.select(store.count())
.from(store)
.where(
store.category.eq(category),
categoryCondition,
withinSearchRadius(myCurrentLat, myCurrentLon, searchRadius, store.latitude, store.longitude)
);

Expand Down Expand Up @@ -93,6 +95,10 @@ public Page<SearchStoresResponse> findStores(Long userId, String keyword,
return PageableExecutionUtils.getPage(results, pageable, () -> countQuery.fetch().size());
}

private BooleanExpression isAllCategory(Category category) {
return category == Category.ALL ? Expressions.TRUE : store.category.eq(category);
}

// TO DO
private BooleanExpression isStoreCurrentlyOpen(String openTime, String closeTime) {
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");
Expand Down

0 comments on commit bede955

Please sign in to comment.