S
S
Stepan2021-09-09 17:06:40
API
Stepan, 2021-09-09 17:06:40

Is it possible to design an API like this?

I am making a simple book reading service on Spring Boot.
I threw the following API structure: api/users/{id}/shelves/{id}/books/{id}/pages
It turns out the following order of obtaining data:

  1. First, we get a list of users:api/users
  2. Then we get a list of user shelves:api/users/{id}/shelves
  3. The following is a list of books on the shelf:api/users/{id}/shelves/{id}/books
  4. Then we get a list of pages of the book:api/users/{id}/shelves/{id}/books/{id}/pages
  5. Next, we work with the pages of the book.


Wouldn't it be a mistake to simplify the structure to:
  1. Get a list of users:api/users
  2. Get a list of shelves for a specific user:api/users/{id}/shelves
  3. Get a list of books on a specific shelf:api/shelves/{id}/books
  4. Get a list of book pages:api/books/{id}/pages
  5. Next, we work with the pages of the book.


Or is such a simplification not according to Feng Shui?

And another nuance: there are no particular difficulties with adding / viewing / updating / deleting information about books and shelves.
What if the book needs to be moved from one shelf to another?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2021-09-09
@steff

If the book id is unique throughout the database, then api/books/{id}/pages is sufficient.

What if the book needs to be moved from one shelf to another?
Change an entry in the shelves_books link table. PATCH /api/books/{id} request with body { "shelf": shelfId }.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question