diff --git a/src/main/java/com/github/nhatoriginal/spring/config/CartIdClassConfig.java b/src/main/java/com/github/nhatoriginal/spring/config/CartIdClassConfig.java index 3f4b3e9..5689e54 100644 --- a/src/main/java/com/github/nhatoriginal/spring/config/CartIdClassConfig.java +++ b/src/main/java/com/github/nhatoriginal/spring/config/CartIdClassConfig.java @@ -2,6 +2,7 @@ import com.github.nhatoriginal.spring.model.MenuItemOption; import com.github.nhatoriginal.spring.model.User; + import lombok.Getter; import lombok.Setter; @@ -9,7 +10,7 @@ @Getter @Setter -public class CartIdClassConfig implements Serializable { - private User user; - private MenuItemOption menuItemOption; +public class CartIdClassConfig implements Serializable { + private User user; + private MenuItemOption menuItemOption; } diff --git a/src/main/java/com/github/nhatoriginal/spring/constant/Endpoint.java b/src/main/java/com/github/nhatoriginal/spring/constant/Endpoint.java index 3c5e74e..54682ed 100644 --- a/src/main/java/com/github/nhatoriginal/spring/constant/Endpoint.java +++ b/src/main/java/com/github/nhatoriginal/spring/constant/Endpoint.java @@ -24,9 +24,10 @@ public static final class User { public static final class Cart { public static final String BASE = "/cart"; - public static final String GET_ALL = "/user/{id}"; + public static final String GET_ALL = "/user/{userId}"; public static final String CREATE = ""; - public static final String DELETE = "/user/{id}"; + public static final String DELETE = "/{userId}/{menuItemOptionId}"; + public static final String UPDATE_QUANTITY = "/{userId}/{menuItemOptionId}"; } public static final class Review { diff --git a/src/main/java/com/github/nhatoriginal/spring/controller/AddressController.java b/src/main/java/com/github/nhatoriginal/spring/controller/AddressController.java index fa59123..2f7a838 100644 --- a/src/main/java/com/github/nhatoriginal/spring/controller/AddressController.java +++ b/src/main/java/com/github/nhatoriginal/spring/controller/AddressController.java @@ -37,12 +37,12 @@ public ResponseEntity save(@Validated @RequestBody SaveAddressDto saveAd } @GetMapping(Endpoint.Address.GET_ALL) - public List findByUserId(@PathVariable("userId") UUID userId) { + public List findByUserId(@PathVariable UUID userId) { return addressService.findByUserId(userId); } @DeleteMapping(Endpoint.Address.DELETE) - public ResponseEntity delete(@PathVariable("id") UUID id) { + public ResponseEntity delete(@PathVariable UUID id) { addressService.delete(id); return ResponseEntity.ok("Deleted address successfully"); } diff --git a/src/main/java/com/github/nhatoriginal/spring/controller/CartController.java b/src/main/java/com/github/nhatoriginal/spring/controller/CartController.java index f1bb8b9..ddc7b2a 100644 --- a/src/main/java/com/github/nhatoriginal/spring/controller/CartController.java +++ b/src/main/java/com/github/nhatoriginal/spring/controller/CartController.java @@ -3,6 +3,7 @@ import com.github.nhatoriginal.spring.constant.Endpoint; import com.github.nhatoriginal.spring.dto.cartList.CartItemDTO; import com.github.nhatoriginal.spring.dto.cartList.SaveCartItemDto; +import com.github.nhatoriginal.spring.dto.cartList.UpdateCartItemQuantityDto; import com.github.nhatoriginal.spring.model.Cart; import com.github.nhatoriginal.spring.service.CartService; import org.springframework.beans.factory.annotation.Autowired; @@ -20,8 +21,8 @@ public class CartController { public CartService cartService; @GetMapping(Endpoint.Cart.GET_ALL) - public List getCartItemList(@PathVariable UUID id) { - return cartService.getCartItemList(id); + public List getCartItemList(@PathVariable UUID userId) { + return cartService.getCartItemList(userId); } @PostMapping(Endpoint.Cart.CREATE) @@ -35,8 +36,17 @@ public ResponseEntity save(@Validated @RequestBody SaveCartItemDto saveC return new ResponseEntity<>("Saved cart item successfully", HttpStatus.CREATED); } - // @DeleteMapping(Endpoint.Cart.DELETE) - // public void deleteItemCart(@PathVariable UUID){ - // - // } + @DeleteMapping(Endpoint.Cart.DELETE) + public ResponseEntity delete(@PathVariable("userId") UUID userId, + @PathVariable("menuItemOptionId") UUID menuItemOptionId) { + cartService.delete(userId, menuItemOptionId); + return new ResponseEntity<>("Deleted cart item successfully", HttpStatus.OK); + } + + @PatchMapping(Endpoint.Cart.UPDATE_QUANTITY) + public ResponseEntity updateQuantity(@PathVariable("userId") UUID userId, + @PathVariable("menuItemOptionId") UUID menuItemOptionId, @RequestBody UpdateCartItemQuantityDto body) { + cartService.updateQuantity(userId, menuItemOptionId, body); + return new ResponseEntity<>("Updated cart item quantity successfully", HttpStatus.OK); + } } diff --git a/src/main/java/com/github/nhatoriginal/spring/dto/cartList/CartDTOConverter.java b/src/main/java/com/github/nhatoriginal/spring/dto/cartList/CartDTOConverter.java index 8229bb2..bbc8371 100644 --- a/src/main/java/com/github/nhatoriginal/spring/dto/cartList/CartDTOConverter.java +++ b/src/main/java/com/github/nhatoriginal/spring/dto/cartList/CartDTOConverter.java @@ -5,14 +5,15 @@ @Component public class CartDTOConverter { - - - public CartItemDTO toCartItemDTO(Cart cart){ - CartItemDTO cartItemDTO = new CartItemDTO(); - cartItemDTO.setImageUrl(cart.getMenuItemOption().getMenuItem().getImageUrl()); - cartItemDTO.setName(cart.getMenuItemOption().getMenuItem().getName()); - cartItemDTO.setPrice(cart.getMenuItemOption().getPrice()); - cartItemDTO.setQuantity(cart.getQuantity()); - return cartItemDTO; - } + public CartItemDTO toCartItemDTO(Cart cart) { + CartItemDTO cartItemDTO = new CartItemDTO(); + cartItemDTO.setMenuItemOptionId(cart.getMenuItemOption().getId()); + cartItemDTO.setEateryId(cart.getMenuItemOption().getMenuItem().getMenu().getEatery().getId()); + cartItemDTO.setImageUrl(cart.getMenuItemOption().getMenuItem().getImageUrl()); + cartItemDTO.setName(cart.getMenuItemOption().getMenuItem().getName()); + cartItemDTO.setSize(cart.getMenuItemOption().getSize()); + cartItemDTO.setPrice(cart.getMenuItemOption().getPrice()); + cartItemDTO.setQuantity(cart.getQuantity()); + return cartItemDTO; + } } diff --git a/src/main/java/com/github/nhatoriginal/spring/dto/cartList/CartItemDTO.java b/src/main/java/com/github/nhatoriginal/spring/dto/cartList/CartItemDTO.java index 3002326..9cdb117 100644 --- a/src/main/java/com/github/nhatoriginal/spring/dto/cartList/CartItemDTO.java +++ b/src/main/java/com/github/nhatoriginal/spring/dto/cartList/CartItemDTO.java @@ -1,14 +1,20 @@ package com.github.nhatoriginal.spring.dto.cartList; +import java.util.UUID; + +import com.github.nhatoriginal.spring.model.Size; + import lombok.Getter; import lombok.Setter; @Getter @Setter - public class CartItemDTO { + private UUID menuItemOptionId; + private UUID eateryId; private String imageUrl; private String name; + private Size size; private double price; private int quantity; } diff --git a/src/main/java/com/github/nhatoriginal/spring/dto/cartList/UpdateCartItemQuantityDto.java b/src/main/java/com/github/nhatoriginal/spring/dto/cartList/UpdateCartItemQuantityDto.java new file mode 100644 index 0000000..d1c6d5a --- /dev/null +++ b/src/main/java/com/github/nhatoriginal/spring/dto/cartList/UpdateCartItemQuantityDto.java @@ -0,0 +1,12 @@ +package com.github.nhatoriginal.spring.dto.cartList; + +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; +import lombok.Data; + +@Data +public class UpdateCartItemQuantityDto { + @Max(10) + @Min(1) + private int quantity; +} diff --git a/src/main/java/com/github/nhatoriginal/spring/service/CartService.java b/src/main/java/com/github/nhatoriginal/spring/service/CartService.java index a34f281..8838c08 100644 --- a/src/main/java/com/github/nhatoriginal/spring/service/CartService.java +++ b/src/main/java/com/github/nhatoriginal/spring/service/CartService.java @@ -1,8 +1,10 @@ package com.github.nhatoriginal.spring.service; +import com.github.nhatoriginal.spring.config.CartIdClassConfig; import com.github.nhatoriginal.spring.dto.cartList.CartDTOConverter; import com.github.nhatoriginal.spring.dto.cartList.CartItemDTO; import com.github.nhatoriginal.spring.dto.cartList.SaveCartItemDto; +import com.github.nhatoriginal.spring.dto.cartList.UpdateCartItemQuantityDto; import com.github.nhatoriginal.spring.model.Cart; import com.github.nhatoriginal.spring.model.MenuItemOption; import com.github.nhatoriginal.spring.model.User; @@ -30,8 +32,8 @@ public class CartService { @Autowired private MenuItemOptionRepository menuItemOptionRepository; - public List getCartItemList(UUID id) { - User user = userRepository.findById(id).get(); + public List getCartItemList(UUID userId) { + User user = userRepository.findById(userId).get(); List cartList = cartRepository.findByUser(user); List cartItemDTOS = new ArrayList(); for (Cart cart : cartList) { @@ -55,4 +57,36 @@ public Cart save(SaveCartItemDto saveCartItemDto) { return cartRepository.save(cart); } + + public void delete(UUID userId, UUID menuItemOptionId) { + User user = userRepository.findById(userId) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found")); + + MenuItemOption menuItemOption = menuItemOptionRepository.findById(menuItemOptionId) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Menu item option not found")); + + CartIdClassConfig id = new CartIdClassConfig(); + id.setUser(user); + id.setMenuItemOption(menuItemOption); + + cartRepository.deleteById(id); + } + + public void updateQuantity(UUID userId, UUID menuItemOptionId, UpdateCartItemQuantityDto body) { + User user = userRepository.findById(userId) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found")); + + MenuItemOption menuItemOption = menuItemOptionRepository.findById(menuItemOptionId) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Menu item option not found")); + + CartIdClassConfig id = new CartIdClassConfig(); + id.setUser(user); + id.setMenuItemOption(menuItemOption); + + Cart cart = cartRepository.findById(id) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Cart item not found")); + + cart.setQuantity(body.getQuantity()); + cartRepository.save(cart); + } }