What belongs in a service layer?
Where to put business rules when a backend API starts to grow.
Start with a concrete rule
Imagine a library API with an endpoint for borrowing a book. Checking that the request contains a book ID is an HTTP concern. Deciding whether the book can be lent out is a business rule. That decision should be the same whether the request comes from a browser, a command-line tool, or a scheduled task.
Let the service coordinate the use case
The controller translates the request into a call to the service. The service loads the relevant records, applies the lending rules, and asks the repository to save the result. The controller then translates that outcome into an HTTP response. If two people can try to borrow the same copy at once, the database must also enforce that only one succeeds.
Test the rule directly
Exercise the borrowing rule without going through HTTP: a copy that is already on loan should be rejected. Keep a separate API test for how that rejection appears to the caller. That way, a change to the response format won’t obscure a regression in lending behavior.