Skip to content

Commit

Permalink
moved logic to @service class
Browse files Browse the repository at this point in the history
  • Loading branch information
edisonzsq committed Mar 9, 2023
1 parent 1a2eaa1 commit 1374db7
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 39 deletions.
1 change: 1 addition & 0 deletions cart_api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -28,6 +30,9 @@ public class CartController {
@Autowired
ProductRepository productRepo;

@Autowired
CartService service;

@RequestMapping(method=RequestMethod.GET)
public ResponseEntity<List<Cart>> findAll(){
List<Cart> cartItems = (List<Cart>)repo.findAll();
Expand All @@ -37,52 +42,31 @@ public ResponseEntity<List<Cart>> findAll(){

@RequestMapping(value="/add/{productId}", method=RequestMethod.POST)
public ResponseEntity add(@PathVariable int productId, @RequestParam Optional<Integer> quantity){
Optional<Cart> 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<Product> 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.
}

}

@RequestMapping(value="/decrement/{productId}", method=RequestMethod.POST)
public ResponseEntity decrement(@PathVariable int productId){

Optional<Cart> 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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package sg.edu.ntu.cart_api.exception;

public class NotFoundException extends Exception{

public NotFoundException(String message){
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -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<Integer> quantity) throws NotFoundException {
Optional<Cart> 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<Product> 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<Cart> 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");
}
}

0 comments on commit 1374db7

Please sign in to comment.