Class ProductService
java.lang.Object
com.goldenleaf.shop.service.ProductService
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 Summary
ConstructorsConstructorDescriptionConstructs a newProductServicewith the provided repository. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddProduct(Product product) Adds a newProductto the database.voideditProduct(Product product) Updates an existingProduct.Retrieves all products from the database.getProductByBrand(String brand) Retrieves aProductby its brand.getProductById(Long id) Retrieves aProductby its unique ID.getProductByName(String name) Retrieves aProductby its name.voidremoveProduct(Product product) Removes an existingProductfrom the database.voidRemoves aProductby its ID.voidremoveProductByName(String name) Removes aProductby its name.
-
Constructor Details
-
ProductService
Constructs a newProductServicewith the provided repository.- Parameters:
repo- the repository used to perform operations on products- Throws:
IllegalArgumentException- ifrepoisnull- See Also:
-
-
Method Details
-
getAllProducts
-
getProductById
Retrieves aProductby its unique ID.- Parameters:
id- the ID of the product- Returns:
- the
Productwith the specified ID - Throws:
RuntimeException- if no product with the given ID exists- See Also:
-
getProductByName
Retrieves aProductby its name.- Parameters:
name- the name of the product- Returns:
- the
Productwith the specified name - Throws:
RuntimeException- if no product with the given name exists- See Also:
-
getProductByBrand
Retrieves aProductby its brand.- Parameters:
brand- the brand of the product- Returns:
- the
Productwith the specified brand - Throws:
RuntimeException- if no product with the given brand exists- See Also:
-
addProduct
-
removeProduct
-
removeProductById
-
removeProductByName
-
editProduct
Updates an existingProduct.The product must have a valid ID that exists in the database. Otherwise, a
RuntimeExceptionis thrown.- Parameters:
product- the product to update- Throws:
RuntimeException- if the product does not exist or ID is null- See Also:
-