Skip to content

Commit

Permalink
fix: cart error-fix api-module
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeongh00 committed Jul 24, 2024
1 parent 76efcc6 commit f4712a3
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public List<CartInfo> findAllCarts(String accessToken) {
Store store = storeQueryService.findStoreById(menu.getStore().getId());

CartInfo cartInfo = new CartInfo(
cart.getId(),
store.getName(),
menu.getName(),
menu.getPrice(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.likelion.apimodule.cart.application;

import com.likelion.apimodule.cart.dto.CartSaveReq;
import com.likelion.apimodule.cart.dto.CartUpdateReq;
import com.likelion.apimodule.security.util.JwtUtil;
import com.likelion.coremodule.cart.domain.Cart;
import com.likelion.coremodule.cart.exception.CartErrorCode;
import com.likelion.coremodule.cart.exception.CartException;
import com.likelion.coremodule.cart.service.CartQueryService;
import com.likelion.coremodule.menu.domain.Menu;
import com.likelion.coremodule.menu.service.MenuQueryService;
Expand All @@ -12,6 +15,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional
Expand All @@ -22,15 +27,35 @@ public class CartSaveUseCase {
private final UserQueryService userQueryService;
private final MenuQueryService menuQueryService;

public Integer findMyCartCount(Long userId, Long menuId) {
return cartQueryService.findCartByUserIdAndMenuId(userId, menuId);
}

public void saveMyCart(String accessToken, CartSaveReq saveReq) {

String email = jwtUtil.getEmail(accessToken);
User user = userQueryService.findByEmail(email);

Menu menu = menuQueryService.findMenuById(saveReq.menuId());

final Cart cart = Cart.builder().user(user).menu(menu).quantity(saveReq.quantity()).build();
cartQueryService.saveCart(cart);
if (findMyCartCount(user.getUserId(), menu.getId()) >= 1) {
throw new CartException(CartErrorCode.NO_CART_INFO);
} else {
final Cart cart = Cart.builder().user(user).menu(menu).quantity(saveReq.quantity()).build();
cartQueryService.saveCart(cart);
}
}

public void updateMyCart(String accessToken, List<CartUpdateReq> updateReqs) {

String email = jwtUtil.getEmail(accessToken);
User user = userQueryService.findByEmail(email);

for (CartUpdateReq req : updateReqs) {

Cart cart = cartQueryService.findCartByUserIdAndCartId(user.getUserId(), req.cartId());
cart.setCartQuantity(req.quantity());
}
}

public void deleteMyCart(String accessToken, Long cartId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.likelion.apimodule.cart.dto;

public record CartInfo(
Long cartid, // cart
String storeName, // store
String menuName, // menu
Integer price, // menu
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.likelion.apimodule.cart.dto;

public record CartUpdateReq(
Long cartId,
Integer quantity
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.likelion.apimodule.cart.application.CartSaveUseCase;
import com.likelion.apimodule.cart.dto.CartInfo;
import com.likelion.apimodule.cart.dto.CartSaveReq;
import com.likelion.apimodule.cart.dto.CartUpdateReq;
import com.likelion.commonmodule.exception.common.ApplicationResponse;
import com.likelion.commonmodule.security.util.AuthConsts;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -64,6 +65,25 @@ public ApplicationResponse<String> saveMyCart(@RequestHeader(AuthConsts.ACCESS_T
return ApplicationResponse.ok("장바구니 저장 완료");
}

// 장바구니 추가
@PutMapping
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "나의 장바구니 추가 성공",
useReturnTypeSchema = true
)
}
)
@Operation(summary = "나의 장바구니 추가 API", description = "나의 장바구니 추가 API 입니다.")
public ApplicationResponse<String> updateMyCart(@RequestHeader(AuthConsts.ACCESS_TOKEN_HEADER) String accessToken,
@RequestBody List<CartUpdateReq> updateReqs) {

cartSaveUseCase.updateMyCart(accessToken, updateReqs);
return ApplicationResponse.ok("장바구니 수량 수정 완료");
}

// 장바구니 삭제
@DeleteMapping("{cartId}/delete")
@ApiResponses(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.likelion.apimodule.home.application;

public class HomeFindUseCase {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.likelion.apimodule.home.presentation;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/home")
@Validated
@Tag(name = "Home", description = "Home 관련 API")
public class HomeController {

// qr 시 시장 조회수 증가
// @GetMapping("/{marketId}/info")
// @ApiResponses(
// value = {
// @ApiResponse(
// responseCode = "200",
// description = "시장 정보 확인 성공",
// useReturnTypeSchema = true
// )
// }
// )
// @Operation(summary = "시장 정보 확인 API", description = "시장 정보 확인 API 입니다.")

// 시장 리스트 검색

// 시장 리스트 나열
}

0 comments on commit f4712a3

Please sign in to comment.