Middle
REST API task: search for a book in an electronic library by partial data (author, title, year unknown), with pagination for a large number of results.
sobes.tech AI
Answer from AI
For the task of searching for a book in an electronic library by partial data (author, title, year unknown) with pagination, the REST API can be designed as follows:
-
Request parameters:
author(optional) — part or full name of the authortitle(optional) — part or full title of the bookyear(optional) — year of publicationpage— page numberpageSize— number of results per page
-
Request processing:
- The server constructs a database query with filters based on the provided parameters. If a parameter is not provided, the filter is not applied.
- Partial data search is implemented using LIKE operators or full-text search.
-
Pagination:
- LIMIT and OFFSET (or cursors) are used to fetch the required number of records.
- The response returns a list of books, as well as metadata: current page, total number of pages, or total number of results.
-
Example request:
GET /api/books?author=Толстой&title=война&page=2&pageSize=10
- Example response:
{
"page": 2,
"pageSize": 10,
"totalResults": 57,
"books": [
{"id": "123", "title": "Война и мир", "author": "Лев Толстой", "year": 1869},
...
]
}
This approach provides flexible search by incomplete data and convenient navigation through the results.