class BookController extends Controller (View source)

Controller responsible for managing books (CRUD operations).

Provides endpoints to list, create, retrieve, update, and delete books.

Traits

AuthorizesRequests
DispatchesJobs
ValidatesRequests

Trait JsonResponseTrait.

Provides standardized and safe error logging with UTF-8 encoding, sensitive data masking, and controlled stack traces.

Methods

JsonResponse
successResponse(array $data = [])

Return a 200 OK JSON response.

JsonResponse
createdResponse(array $data = [])

Return a 201 Created JSON response.

JsonResponse
acceptedResponse()

Return a 202 Accepted JSON response (request accepted for processing).

JsonResponse
noContentResponse()

Return a 204 No Content JSON response.

JsonResponse
resetContentResponse()

Return a 205 Reset Content JSON response.

JsonResponse
badRequestResponse(array $errors)

Return a 400 Bad Request JSON response.

JsonResponse
unauthorizedResponse(string $message = 'Não autorizado.')

Return a 401 Unauthorized JSON response.

JsonResponse
forbiddenResponse(string $message = 'Acesso negado.')

Return a 403 Forbidden JSON response.

JsonResponse
notFoundResponse(string $message = 'Recurso não encontrado.')

Return a 404 Not Found JSON response.

JsonResponse
conflictResponse(array $errors)

Return a 409 Conflict JSON response.

JsonResponse
validationErrorResponse(array $errors)

Return a 422 Unprocessable Entity JSON response for validation errors.

JsonResponse
tooManyRequestsResponse(string $message = 'Muitas requisições. Tente novamente mais tarde.')

Return a 429 Too Many Requests JSON response.

JsonResponse
internalErrorResponse(Throwable $e, string $message = 'Erro interno.')

Return a 500 Internal Server Error JSON response.

void
logError(string $message, Throwable $exception, array $context = [], string|null $channel = null)

Logs an error with consistent formatting and context.

array
sanitizeSensitiveData(array $data)

Masks common sensitive fields (e.g., passwords, tokens) in the given context array.

array
encodeStringsUtf8(array $data)

Recursively converts all string values to UTF-8.

string
getLimitedTrace(Throwable $exception, int $maxLines = 10)

Returns a string representation of the exception trace, limited to the given number of lines.

CursorPaginator
paginateWithSettings(Builder $query, Request $request, int $maxLimit = 250)

Paginate query results dynamically using cursor pagination.

JsonResponse
index(Request $request)

Retrieve a paginated list of books with optional search terms and filters.

JsonResponse
store(BookStoreRequest $request, CopyService $copyService)

Store a new book and its copies in the database.

JsonResponse
show(string $id)

Retrieve details of a single book by its UUID.

JsonResponse
findByIsbn(string $isbn)

Find a book by ISBN and return its available copies.

JsonResponse
fetchMetadata(string $isbn, BookMetadataService $metadataService)

Retrieve book metadata from external sources (Google Books / OpenLibrary) by ISBN.

JsonResponse
update(BookUpdateRequest $request, string $id)

Update a book's details.

JsonResponse
destroy(string $id)

Delete a book if no active copies exist.

Details

protected JsonResponse successResponse(array $data = [])

Return a 200 OK JSON response.

Parameters

array $data

response payload

Return Value

JsonResponse

protected JsonResponse createdResponse(array $data = [])

Return a 201 Created JSON response.

Parameters

array $data

newly created resource data

Return Value

JsonResponse

protected JsonResponse acceptedResponse()

Return a 202 Accepted JSON response (request accepted for processing).

Return Value

JsonResponse

protected JsonResponse noContentResponse()

Return a 204 No Content JSON response.

Return Value

JsonResponse

protected JsonResponse resetContentResponse()

Return a 205 Reset Content JSON response.

Return Value

JsonResponse

protected JsonResponse badRequestResponse(array $errors)

Return a 400 Bad Request JSON response.

Parameters

array $errors

validation or request errors

Return Value

JsonResponse

protected JsonResponse unauthorizedResponse(string $message = 'Não autorizado.')

Return a 401 Unauthorized JSON response.

Parameters

string $message

optional error message

Return Value

JsonResponse

protected JsonResponse forbiddenResponse(string $message = 'Acesso negado.')

Return a 403 Forbidden JSON response.

Parameters

string $message

optional error message

Return Value

JsonResponse

protected JsonResponse notFoundResponse(string $message = 'Recurso não encontrado.')

Return a 404 Not Found JSON response.

Parameters

string $message

optional error message

Return Value

JsonResponse

protected JsonResponse conflictResponse(array $errors)

Return a 409 Conflict JSON response.

Parameters

array $errors

conflict details

Return Value

JsonResponse

protected JsonResponse validationErrorResponse(array $errors)

Return a 422 Unprocessable Entity JSON response for validation errors.

Parameters

array $errors

validation error details

Return Value

JsonResponse

protected JsonResponse tooManyRequestsResponse(string $message = 'Muitas requisições. Tente novamente mais tarde.')

Return a 429 Too Many Requests JSON response.

Parameters

string $message

optional rate limit message

Return Value

JsonResponse

protected JsonResponse internalErrorResponse(Throwable $e, string $message = 'Erro interno.')

Return a 500 Internal Server Error JSON response.

Logs the exception and returns a standardized JSON error message.

Parameters

Throwable $e

the thrown exception

string $message

optional user-friendly message

Return Value

JsonResponse

protected void logError(string $message, Throwable $exception, array $context = [], string|null $channel = null)

Logs an error with consistent formatting and context.

Parameters

string $message

descriptive error message

Throwable $exception

the thrown exception

array $context

additional context to include in the log

string|null $channel

Optional log channel (e.g., 'classes').

Return Value

void

protected array sanitizeSensitiveData(array $data)

Masks common sensitive fields (e.g., passwords, tokens) in the given context array.

Parameters

array $data

Return Value

array

protected array encodeStringsUtf8(array $data)

Recursively converts all string values to UTF-8.

Parameters

array $data

Return Value

array

protected string getLimitedTrace(Throwable $exception, int $maxLines = 10)

Returns a string representation of the exception trace, limited to the given number of lines.

Parameters

Throwable $exception
int $maxLines

Return Value

string

CursorPaginator paginateWithSettings(Builder $query, Request $request, int $maxLimit = 250)

Paginate query results dynamically using cursor pagination.

Parameters

Builder $query
Request $request
int $maxLimit

Maximum items per page (default 250)

Return Value

CursorPaginator

JsonResponse index(Request $request)

Retrieve a paginated list of books with optional search terms and filters.

This endpoint processes the incoming request, applies search keywords, filters, and pagination rules, and returns a structured JSON payload containing the resulting dataset, pagination cursors, and filter metadata used to build the dynamic UI.

Parameters

Request $request

The HTTP request containing query parameters for search, filtering, ordering, and pagination settings

Return Value

JsonResponse

A JSON response containing: - data: the paginated list of books - pagination: cursor-based pagination metadata - filterData: aggregated metadata for dynamic filter components

Exceptions

Throwable

JsonResponse store(BookStoreRequest $request, CopyService $copyService)

Store a new book and its copies in the database.

Parameters

BookStoreRequest $request

validated request containing book data and number of copies

CopyService $copyService

service to handle copy creation

Return Value

JsonResponse

JSON response indicating success or failure

JsonResponse show(string $id)

Retrieve details of a single book by its UUID.

Parameters

string $id

book UUID

Return Value

JsonResponse

JSON response with book data or error message

JsonResponse findByIsbn(string $isbn)

Find a book by ISBN and return its available copies.

Parameters

string $isbn

the ISBN to search for

Return Value

JsonResponse

JSON response with book data and available copies

JsonResponse fetchMetadata(string $isbn, BookMetadataService $metadataService)

Retrieve book metadata from external sources (Google Books / OpenLibrary) by ISBN.

Parameters

string $isbn

the ISBN to search for

BookMetadataService $metadataService

service responsible for fetching book metadata

Return Value

JsonResponse

JSON response containing merged book metadata or error message

JsonResponse update(BookUpdateRequest $request, string $id)

Update a book's details.

Parameters

BookUpdateRequest $request

validated request containing updated data

string $id

book UUID

Return Value

JsonResponse

JSON response indicating success, conflict, or error

JsonResponse destroy(string $id)

Delete a book if no active copies exist.

Parameters

string $id

book UUID

Return Value

JsonResponse

JSON response indicating success or validation failure