dailycodework / dream-shops

95 stars 44 forks source link

one endpoint giving me error #3

Open Ampta opened 1 month ago

Ampta commented 1 month ago

bold part is not working in my system

errors are: at com.ampta.central_shops.service.cart.CartService.initializeNewCart(CartService.java:53) ~[classes/:na] at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:580) ~[na:na] at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:355) ~[spring-aop-6.1.13.jar:6.1.13] at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:716) ~[spring-aop-6.1.13.jar:6.1.13] at com.ampta.central_shops.service.cart.CartService$$SpringCGLIB$$0.initializeNewCart() ~[classes/:na] at com.ampta.central_shops.controller.CartItemController.addItemToCart(CartItemController.java:32) ~[classes/:na] at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:580) ~[na:na]

@PostMapping("/item/add") public ResponseEntity addItemToCart(@RequestParam(required = false) Long cartId, @RequestParam Long productId, @RequestParam Integer quantity){ try {

        **if (cartId == null) {
            cartId = cartService.initializeNewCart();
        }**

        cartItemService.addItemToCart(cartId, productId, quantity);
        return ResponseEntity.ok(new ApiResponse("Add Item Success", null));
    }catch (ResourceNotFoundException e){
        return ResponseEntity.status(NOT_FOUND).body(new ApiResponse(e.getMessage(), null));
    }

}
YunussEmree commented 1 month ago

Hey, in this code there is no auto initialize cart for default users so you can set the userid manually or integrate spring security. It will be solution for your problem.