Class CategoryService

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

@Service public class CategoryService extends Object
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 Details

  • Method Details

    • getAllCategories

      public List<Category> getAllCategories()
      Retrieves all categories from the database.
      Returns:
      a List of all Category entities
      See Also:
      • ListCrudRepository.findAll()
    • getCategoryById

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

      public Category getCategoryByName(String name)
      Retrieves a Category by its unique name.
      Parameters:
      name - the name of the category
      Returns:
      the Category with the specified name
      Throws:
      RuntimeException - if no category with the given name exists
      See Also:
    • addCategory

      public void addCategory(Category category)
      Adds a new Category to the database.
      Parameters:
      category - the category to add
      See Also:
      • CrudRepository.save(Object)
    • removeCategory

      public void removeCategory(Category category)
      Removes an existing Category from the database.

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

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

      public void removeCategoryById(Long id)
      Removes a Category by its ID.
      Parameters:
      id - the ID of the category to delete
      See Also:
      • CrudRepository.deleteById(Object)
    • removeCategoryByName

      public void removeCategoryByName(String name)
      Removes a Category by its name.
      Parameters:
      name - the name of the category to delete
      Throws:
      RuntimeException - if no category with the given name exists
      See Also:
    • editCategory

      public void editCategory(Category category)
      Updates an existing Category.

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

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