Class ProductService

java.lang.Object
com.example.springrest.services.ProductService

@Service public class ProductService extends Object
Application service encapsulating business operations for Products.

Responsibilities:

  • Read operations with pagination & search.
  • Create, update, delete with transactional safety.
  • Consistent "not found" behavior via getOrThrow(Long).

Usage


 // Controller example
 @GetMapping("/products")
 Page<ProductResponse> list(Pageable pageable) {
   return productService.list(pageable).map(mapper::toResponse);
 }

 @PatchMapping("/products/{id}")
 ProductResponse patch(@PathVariable Long id, @RequestBody ProductRequest req) {
   Product updated = productService.update(id, prod -> mapper.updateEntity(prod, req));
   return mapper.toResponse(updated);
 }
 

Transactional rules

Since:
1.0
  • Constructor Details

    • ProductService

      public ProductService()
  • Method Details

    • list

      public org.springframework.data.domain.Page<Product> list(org.springframework.data.domain.Pageable pageable)
      Returns a paginated/sorted view of all products.
      Parameters:
      pageable - pagination and sorting information (page number, size, sort)
      Returns:
      a page of products (possibly empty)
    • searchByName

      public org.springframework.data.domain.Page<Product> searchByName(String q, org.springframework.data.domain.Pageable pageable)
      Searches for products by name (case-insensitive), paged.
      Parameters:
      q - the query string to match against product names
      pageable - pagination and sorting information
      Returns:
      a page of products matching the search criteria (possibly empty)
    • getOrThrow

      public Product getOrThrow(Long id)
      Retrieves a Product by its ID or throws an exception if not found.
      Parameters:
      id - the ID of the product to retrieve
      Returns:
      the product with the given ID
      Throws:
      ResourceNotFoundException - if no product exists with the given ID
    • create

      @Transactional public Product create(Product p)
      Creates/Persists a new product
      Parameters:
      p - the product to create (id must be null)
      Returns:
      the persisted product with generated ID
    • update

      @Transactional public Product update(Long id, Consumer<Product> mutator)
      Applies a mutation function to an existing product and persists the change.

      Pattern: pass a lambda that updates only what you intend to change. Combine this with a mapper's partial update for PATCH-like flows.

      
       productService.update(id, prod -> {
         prod.setName("New Name");
         prod.setPrice(new BigDecimal("19.99"));
       });
       
      Parameters:
      id - target product id
      mutator - mutation to apply to the loaded entity
      Returns:
      the updated, persisted product
      Throws:
      ResourceNotFoundException - if the id does not exist
    • delete

      @Transactional public void delete(Long id)
      Deletes a product by its ID

      Idempotent from the controller perspective: attempts to delete a non-existent ID will result in ResourceNotFoundException, which is mapped to HTTP 404.

      Parameters:
      id - the ID of the product to delete
      Throws:
      ResourceNotFoundException - if no product exists with the given ID