I have couple of nested resources like Merchant
, Hotel
, Room
. A merchant can have many hotels and similarly a hotel can have many rooms.
Right now for managing these resources I doing something like:
Create
POST api/v1/merchants/11/hotels
creates a new hotel.
Update
PUT api/v1/merchants/11/hotels/42
updates the given hotel.
Same for read and delete.
For rooms:
Create
POST api/v1/merchants/11/hotels/42/rooms
creates a new room.
Update
PUT api/v1/merchants/11/hotels/42/rooms/42
updates the given room etc.
In future there will be more nested resources like room facilities etc. and following this scheme will turn hairy.
I am in the early stage of development and I may expose these APIs for developers hence I can't change API scheme very quickly.
I am in doubt from day one regarding this approach. Can I assume that each entity has unique ID (which is true for now as I am using relational database and has its own table)? If yes, then the can I use these URLs for APIs instead of above ones?
POST api/v1/rooms
PUT api/v1/rooms/42
Etc for more nested resources.
Is there any violation of semantics or standard that I may be missing? I am using similar approach in views eg. in URLs in browsers which is looking ugly too.
PUT api/v1/merchants/12/hotels/42
am I talking to the same hotel as I am inPUT api/v1/merchants/11/hotels/42
? Or is that a different hotel? Or is this sure to produce an error?merchants/12
and talk directly tohotels/42
without caring if there even is a merchant.hotels/42
is meaningless withoutmerchants/12
. 42 isn't a unique id. It's part of a composite key. Now, suppose the merchant sells the hotel to a different merchant? What happens now?