Class ShoppingItemService

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

@Service public class ShoppingItemService extends Object
Service class for managing ShoppingItem entities.

Provides business logic for retrieving, adding, updating, and deleting shopping items. Supports operations by ShoppingCart and Product. Acts as an intermediary between controllers and the ShoppingItemRepository.

See Also:
  • Constructor Details

  • Method Details

    • getAllShoppingItems

      public List<ShoppingItem> getAllShoppingItems()
      Retrieves all shopping items from the database.
      Returns:
      a List of all ShoppingItem entities
      See Also:
      • ListCrudRepository.findAll()
    • getShoppingItemsByCart

      public List<ShoppingItem> getShoppingItemsByCart(ShoppingCart cart)
      Retrieves all shopping items in a specific shopping cart.
      Parameters:
      cart - the ShoppingCart whose items are retrieved
      Returns:
      a List of ShoppingItem in the cart
      See Also:
    • getShoppingItemById

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

      public void addShoppingItem(ShoppingItem item)
      Adds a new ShoppingItem to the database.
      Parameters:
      item - the shopping item to add
      See Also:
      • CrudRepository.save(Object)
    • removeShoppingItem

      public void removeShoppingItem(ShoppingItem item)
      Removes an existing ShoppingItem from the database.

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

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

      public void removeShoppingItemById(Long id)
      Removes a ShoppingItem by its ID.
      Parameters:
      id - the ID of the shopping item to delete
      See Also:
      • CrudRepository.deleteById(Object)
    • removeShoppingItemByProduct

      public void removeShoppingItemByProduct(Product product)
      Removes a ShoppingItem associated with a specific Product.
      Parameters:
      product - the product whose shopping item should be deleted
      Throws:
      RuntimeException - if no shopping item exists for the given product
      See Also:
    • editShoppingItem

      public void editShoppingItem(ShoppingItem item)
      Updates an existing ShoppingItem.

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

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