Class ShoppingCartService

java.lang.Object
com.goldenleaf.shop.service.ShoppingCartService

@Service public class ShoppingCartService extends Object
Service class for managing ShoppingCart entities.

Provides business logic for retrieving, adding, updating, and deleting shopping carts. Supports operations by Customer and cart ID. Acts as an intermediary between controllers and the ShoppingCartRepository.

See Also:
  • Constructor Details

  • Method Details

    • getAllShoppingCarts

      public List<ShoppingCart> getAllShoppingCarts()
      Retrieves all shopping carts from the database.
      Returns:
      a List of all ShoppingCart entities
      See Also:
      • ListCrudRepository.findAll()
    • getShoppingCartById

      public ShoppingCart getShoppingCartById(Long id)
      Retrieves a ShoppingCart by its unique ID.
      Parameters:
      id - the ID of the shopping cart
      Returns:
      the ShoppingCart with the specified ID
      Throws:
      RuntimeException - if no shopping cart with the given ID exists
      See Also:
      • CrudRepository.findById(Object)
    • getShoppingCartByCustomer

      public ShoppingCart getShoppingCartByCustomer(Customer customer)
      Retrieves a ShoppingCart for a specific Customer.
      Parameters:
      customer - the customer whose shopping cart is retrieved
      Returns:
      the ShoppingCart for the given customer
      Throws:
      RuntimeException - if no shopping cart exists for the given customer
      See Also:
    • addShoppingCart

      public void addShoppingCart(ShoppingCart cart)
      Adds a new ShoppingCart to the database.
      Parameters:
      cart - the shopping cart to add
      See Also:
      • CrudRepository.save(Object)
    • removeShoppingCart

      public void removeShoppingCart(ShoppingCart cart)
      Removes an existing ShoppingCart from the database.

      If the shopping cart exists (by ID), it will be deleted; otherwise, nothing happens.

      Parameters:
      cart - the shopping cart to remove
      See Also:
      • CrudRepository.delete(Object)
      • CrudRepository.existsById(Object)
    • removeShoppingCartById

      public void removeShoppingCartById(Long id)
      Removes a ShoppingCart by its ID.
      Parameters:
      id - the ID of the shopping cart to delete
      See Also:
      • CrudRepository.deleteById(Object)
    • removeShoppingCartByCustomer

      public void removeShoppingCartByCustomer(Customer customer) throws EmptyLoginException
      Removes a ShoppingCart associated with a specific Customer.
      Parameters:
      customer - the customer whose shopping cart should be deleted
      Throws:
      EmptyLoginException - if no shopping cart exists for the given customer
      See Also:
    • editShoppingCart

      public void editShoppingCart(ShoppingCart cart)
      Updates an existing ShoppingCart.

      The shopping cart must have a valid ID that exists in the database. Otherwise, a RuntimeException is thrown.

      Parameters:
      cart - the shopping cart to update
      Throws:
      RuntimeException - if the shopping cart does not exist or ID is null
      See Also:
      • CrudRepository.save(Object)
      • CrudRepository.existsById(Object)