Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

GETting entity from Web API via primary key vs composite key

$
0
0

I am developing a RESTful web api, which concerns itself with estate agents doing inspections of houses. So we have entities such as properties, which contain rooms which contain items. For each of these, one can specify the condition of the room/item, make notes, take pictures, specify what needs to be fixed and by whom and when, etc. In my database (MySQL) I created a `properties` table, a `rooms` table and an `items` table. The `rooms` and `items` table are lookup table with all possible types of rooms (bedroom, bathroom, kitchen, etc) and items (bed, light, carpet, etc). I then have junction tables to link items to rooms and rooms to properties. So I have two tables called, called `propertyrooms` and `roomitems`. The `propertyrooms` table has a `PropertyId` and a `RoomId`, as well as room_condition, notes, photos, etc. I have also, maybe foolishly, included an auto-incrementing ID primary key column. I've done exactly the same for `roomitems`.

I realized when creating the web api (ASP .Net Core 1.1) that I had to decide how to structure the routing. For example, to get all properties, I have:

GET: /api/properties

and a specific property:

GET: /api/properties/5

to get all rooms in a property, I have:

GET: /api/properties/5/rooms

but the tricky part I am facing now is to get a specific room. I could either have:

GET: /api/properties/5/rooms/7

or

GET:/api/propertyrooms/23

In the two lines above, both endpoints refer to the same room. In the first endpoint I am identifying the room by a composite key of PropertyId = 5 and AreaId = 7, and in the second I am using the `propertyrooms` table ID primary key field. I guess I don't need a primary key in the `propertyrooms` and `roomitems` tables - I can just have the two composite keys. Is there perhaps a best practice regarding this, either from a database perspective or from a RESTful perspective? Should I keep both the composite key, and the auto-incrementing primary key, so that the client consuming the API has the option of calling a room using either method? I'd love to hear any thoughts on this... Thank you in advance...


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>