-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
99 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
cart_api/src/main/java/sg/edu/ntu/cart_api/exception/NotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
cart_api/src/main/java/sg/edu/ntu/cart_api/service/CartService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |