Class ProductService

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

@Service public class ProductService extends Object
Service class for managing Product entities.

Provides business logic for retrieving, adding, updating, and deleting products. Acts as an intermediary between controllers and the ProductRepository.

See Also:
  • Constructor Details

  • Method Details

    • getAllProducts

      public List<Product> getAllProducts()
      Retrieves all products from the database.
      Returns:
      a List of all Product entities
      See Also:
      • ListCrudRepository.findAll()
    • getProductById

      public Product getProductById(Long id)
      Retrieves a Product by its unique ID.
      Parameters:
      id - the ID of the product
      Returns:
      the Product with the specified ID
      Throws:
      RuntimeException - if no product with the given ID exists
      See Also:
      • CrudRepository.findById(Object)
    • getProductByName

      public Product getProductByName(String name)
      Retrieves a Product by its name.
      Parameters:
      name - the name of the product
      Returns:
      the Product with the specified name
      Throws:
      RuntimeException - if no product with the given name exists
      See Also:
    • getProductByBrand

      public Product getProductByBrand(String brand)
      Retrieves a Product by its brand.
      Parameters:
      brand - the brand of the product
      Returns:
      the Product with the specified brand
      Throws:
      RuntimeException - if no product with the given brand exists
      See Also:
    • addProduct

      public void addProduct(Product product)
      Adds a new Product to the database.
      Parameters:
      product - the product to add
      See Also:
      • CrudRepository.save(Object)
    • removeProduct

      public void removeProduct(Product product)
      Removes an existing Product from the database.

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

      Parameters:
      product - the product to remove
      See Also:
      • CrudRepository.delete(Object)
      • CrudRepository.existsById(Object)
    • removeProductById

      public void removeProductById(Long id)
      Removes a Product by its ID.
      Parameters:
      id - the ID of the product to delete
      See Also:
      • CrudRepository.deleteById(Object)
    • removeProductByName

      public void removeProductByName(String name)
      Removes a Product by its name.
      Parameters:
      name - the name of the product to delete
      See Also:
    • editProduct

      public void editProduct(Product product)
      Updates an existing Product.

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

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