From 1374db7df7e2450723c076655be1fb9fd6aff01b Mon Sep 17 00:00:00 2001 From: Edison Zhuang Date: Thu, 9 Mar 2023 08:29:26 +0800 Subject: [PATCH] moved logic to @Service class --- cart_api/pom.xml | 1 + .../cart_api/controller/CartController.java | 62 +++++++---------- .../cart_api/exception/NotFoundException.java | 9 +++ .../edu/ntu/cart_api/service/CartService.java | 66 +++++++++++++++++++ 4 files changed, 99 insertions(+), 39 deletions(-) create mode 100644 cart_api/src/main/java/sg/edu/ntu/cart_api/exception/NotFoundException.java create mode 100644 cart_api/src/main/java/sg/edu/ntu/cart_api/service/CartService.java diff --git a/cart_api/pom.xml b/cart_api/pom.xml index 8e18b03..84f6a0e 100644 --- a/cart_api/pom.xml +++ b/cart_api/pom.xml @@ -50,6 +50,7 @@ mysql mysql-connector-java + diff --git a/cart_api/src/main/java/sg/edu/ntu/cart_api/controller/CartController.java b/cart_api/src/main/java/sg/edu/ntu/cart_api/controller/CartController.java index 41c5ff0..882af3f 100644 --- a/cart_api/src/main/java/sg/edu/ntu/cart_api/controller/CartController.java +++ b/cart_api/src/main/java/sg/edu/ntu/cart_api/controller/CartController.java @@ -17,6 +17,8 @@ import sg.edu.ntu.cart_api.entity.Product; import sg.edu.ntu.cart_api.repository.CartRepository; import sg.edu.ntu.cart_api.repository.ProductRepository; +import sg.edu.ntu.cart_api.exception.NotFoundException; +import sg.edu.ntu.cart_api.service.CartService; @RestController @RequestMapping("/carts") @@ -28,6 +30,9 @@ public class CartController { @Autowired ProductRepository productRepo; + @Autowired + CartService service; + @RequestMapping(method=RequestMethod.GET) public ResponseEntity> findAll(){ List cartItems = (List)repo.findAll(); @@ -37,30 +42,15 @@ public ResponseEntity> findAll(){ @RequestMapping(value="/add/{productId}", method=RequestMethod.POST) public ResponseEntity add(@PathVariable int productId, @RequestParam Optional quantity){ - Optional optionalCartItem = repo.findByProductId(productId); - - if(optionalCartItem.isPresent()){ - - // Product found in cart - Cart cartItem = optionalCartItem.get(); - int currentQuantity = cartItem.getQuantity(); - cartItem.setQuantity(quantity.orElseGet(() -> currentQuantity + 1)); // If quantity param not exist, just increment by 1 - cartItem = repo.save(cartItem); - return ResponseEntity.ok().build(); - }else{ - - // Product not found in cart - Optional optionalProduct = productRepo.findById(productId); - if(optionalProduct.isPresent()){ - Product product = optionalProduct.get(); - Cart cartItem = new Cart(); - cartItem.setProduct(product); - cartItem.setQuantity(quantity.orElseGet(() -> 1)); - repo.save(cartItem); - return ResponseEntity.ok().build(); - }else{ - return ResponseEntity.badRequest().build(); - } + try{ + service.add(productId, quantity); + return ResponseEntity.ok().build(); // When no exception is thrown means operation is successful. + }catch(NotFoundException nfe){ + nfe.printStackTrace(); + return ResponseEntity.notFound().build(); // Return 404 if NotFoundException is thrown. + }catch(Exception e){ + e.printStackTrace(); + return ResponseEntity.internalServerError().build(); // Return 500 if any other unexpected exception is thrown. } } @@ -68,21 +58,15 @@ public ResponseEntity add(@PathVariable int productId, @RequestParam Optional optionalCartItem = repo.findByProductId(productId); - if(optionalCartItem.isPresent()){ - - int currentQuantity = 0; - Cart cart = optionalCartItem.get(); - if(cart.getQuantity() == 1) - repo.deleteById(cart.getId()); - else{ - currentQuantity = cart.getQuantity(); - cart.setQuantity(currentQuantity - 1); - repo.save(cart); - } - return ResponseEntity.ok().build(); + try{ + service.decrement(productId); + return ResponseEntity.ok().build(); // When no exception is thrown means operation is successful. + }catch(NotFoundException nfe){ + nfe.printStackTrace(); + return ResponseEntity.notFound().build(); // Return 404 if NotFoundException is thrown. + }catch(Exception e){ + e.printStackTrace(); + return ResponseEntity.internalServerError().build(); // Return 500 if any other unexpected exception is thrown. } - - return ResponseEntity.badRequest().build(); } } diff --git a/cart_api/src/main/java/sg/edu/ntu/cart_api/exception/NotFoundException.java b/cart_api/src/main/java/sg/edu/ntu/cart_api/exception/NotFoundException.java new file mode 100644 index 0000000..3ae82ad --- /dev/null +++ b/cart_api/src/main/java/sg/edu/ntu/cart_api/exception/NotFoundException.java @@ -0,0 +1,9 @@ +package sg.edu.ntu.cart_api.exception; + +public class NotFoundException extends Exception{ + + public NotFoundException(String message){ + super(message); + } + +} diff --git a/cart_api/src/main/java/sg/edu/ntu/cart_api/service/CartService.java b/cart_api/src/main/java/sg/edu/ntu/cart_api/service/CartService.java new file mode 100644 index 0000000..fa81f22 --- /dev/null +++ b/cart_api/src/main/java/sg/edu/ntu/cart_api/service/CartService.java @@ -0,0 +1,66 @@ +package sg.edu.ntu.cart_api.service; + +import org.springframework.stereotype.Service; +import org.springframework.beans.factory.annotation.Autowired; +import java.util.Optional; + +import sg.edu.ntu.cart_api.entity.Cart; +import sg.edu.ntu.cart_api.entity.Product; +import sg.edu.ntu.cart_api.repository.CartRepository; +import sg.edu.ntu.cart_api.repository.ProductRepository; +import sg.edu.ntu.cart_api.exception.NotFoundException; + +@Service +public class CartService { + + @Autowired + CartRepository repo; + + @Autowired + ProductRepository productRepo; + + public void add(int productId, Optional quantity) throws NotFoundException { + Optional optionalCartItem = repo.findByProductId(productId); + + if(optionalCartItem.isPresent()){ + + // Product found in cart + Cart cartItem = optionalCartItem.get(); + int currentQuantity = cartItem.getQuantity(); + cartItem.setQuantity(quantity.orElseGet(() -> currentQuantity + 1)); // If quantity param not exist, just increment by 1 + cartItem = repo.save(cartItem); + + }else{ + + // Product not found in cart + Optional optionalProduct = productRepo.findById(productId); + if(optionalProduct.isPresent()){ + Product product = optionalProduct.get(); + Cart cartItem = new Cart(); + cartItem.setProduct(product); + cartItem.setQuantity(quantity.orElseGet(() -> 1)); + repo.save(cartItem); + }else{ + throw new NotFoundException("Product ID not found"); + } + } + } + + public void decrement(int productId) throws NotFoundException{ + Optional optionalCartItem = repo.findByProductId(productId); + if(optionalCartItem.isPresent()){ + + int currentQuantity = 0; + Cart cart = optionalCartItem.get(); + if(cart.getQuantity() == 1) + repo.deleteById(cart.getId()); + else{ + currentQuantity = cart.getQuantity(); + cart.setQuantity(currentQuantity - 1); + repo.save(cart); + } + } + + throw new NotFoundException("Product ID not found"); + } +}