Class CategoryService
java.lang.Object
com.goldenleaf.shop.service.CategoryService
Service class for managing
Category entities.
Provides business logic for retrieving, adding, updating, and deleting categories.
Acts as an intermediary between controllers and the CategoryRepository.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newCategoryServicewith the provided repository. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCategory(Category category) Adds a newCategoryto the database.voideditCategory(Category category) Updates an existingCategory.Retrieves all categories from the database.getCategoryById(Long id) Retrieves aCategoryby its unique ID.getCategoryByName(String name) Retrieves aCategoryby its unique name.voidremoveCategory(Category category) Removes an existingCategoryfrom the database.voidRemoves aCategoryby its ID.voidremoveCategoryByName(String name) Removes aCategoryby its name.
-
Constructor Details
-
CategoryService
Constructs a newCategoryServicewith the provided repository.- Parameters:
repo- the repository used to perform operations on categories- Throws:
IllegalArgumentException- ifrepoisnull- See Also:
-
-
Method Details
-
getAllCategories
-
getCategoryById
Retrieves aCategoryby its unique ID.- Parameters:
id- the ID of the category- Returns:
- the
Categorywith the specified ID - Throws:
RuntimeException- if no category with the given ID exists- See Also:
-
getCategoryByName
Retrieves aCategoryby its unique name.- Parameters:
name- the name of the category- Returns:
- the
Categorywith the specified name - Throws:
RuntimeException- if no category with the given name exists- See Also:
-
addCategory
-
removeCategory
-
removeCategoryById
-
removeCategoryByName
Removes aCategoryby its name.- Parameters:
name- the name of the category to delete- Throws:
RuntimeException- if no category with the given name exists- See Also:
-
editCategory
Updates an existingCategory.The category must have a valid ID that exists in the database. Otherwise, a
RuntimeExceptionis thrown.- Parameters:
category- the category to update- Throws:
RuntimeException- if the category does not exist or ID is null- See Also:
-