Package com.example.springrest.services
Class ProductService
java.lang.Object
com.example.springrest.services.ProductService
Application service encapsulating business operations for
Product
s.
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
- Write methods (
create(Product)
,update(Long, Consumer)
,delete(Long)
) are annotated@Transactional
to ensure atomicity. - Read methods are non-transactional by default for better throughput.
- Since:
- 1.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCreates/Persists a new productvoid
Deletes a product by its IDgetOrThrow
(Long id) Retrieves a Product by its ID or throws an exception if not found.org.springframework.data.domain.Page
<Product> list
(org.springframework.data.domain.Pageable pageable) Returns a paginated/sorted view of all products.org.springframework.data.domain.Page
<Product> searchByName
(String q, org.springframework.data.domain.Pageable pageable) Searches for products by name (case-insensitive), paged.Applies a mutation function to an existing product and persists the change.
-
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 namespageable
- pagination and sorting information- Returns:
- a page of products matching the search criteria (possibly empty)
-
getOrThrow
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
Creates/Persists a new product- Parameters:
p
- the product to create (id must benull
)- Returns:
- the persisted product with generated ID
-
update
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 idmutator
- mutation to apply to the loaded entity- Returns:
- the updated, persisted product
- Throws:
ResourceNotFoundException
- if the id does not exist
-
delete
Deletes a product by its IDIdempotent 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
-