I've got a question about designing REST API. Consider the following situation: we've got table called banners and table called images. Each banner has got one image and each image belongs to one banner (table images is used to store another images, not only banners, so joining the tables is not the solution).
table Banner table Images
________________________ ______________________
| id | Int | | id | Int |
| title | VARCHAR | | filename | VARCHAR|
|__________|___________| | banner_id | Int |
| article_id| Int |
|___________|________|
I've read some articles about creating REST APIs and according them, I should make this URIs for data retrieving:
1) api/banner/1
2) api/banner/1/image
But I will always need an image with banner, so why is not better to return everything by calling first API route? If I'll do it by this way (two routes), how should I implement the calls from frontend? Should I write two http.get() methods for retrieving banner first and then its associated image? Thanks for answers!
There are a few options for you in this situation:
As you noted, if you offer both resources, you would have to do two GET requests to retrieve all the data you need.
You can do both - api/banner/1/image can return data about the image, and api/banner/1 can return data about the banner, with the image embedded.
If you use a media type such as HAL it would be considered an 'embedded' resource:
{
"id": 1,
"title": "banner title",
"_embedded": {
"image" : {
"id": 1,
"filename": "some path",
"_links": {
"self": {
"href": "api/banner/1/image"
}
}
}
},
"_links": {
"self": {
"href": "/api/banner/1"
}
}
}
This way you could get all the data you need with a single GET.
There is no rule that says your database tables have to line up exactly with your API resources.
There's nothing wrong with only offering api/banner/1 and having it return something like:
{
"id": 1,
"title": "banner title",
"imageFileName": "some path"
}
The fact that you happen to store the data fields on separate tables internally in your system is just an implementation detail/
Although you are currently focussing on the GET method, your resource structure should also take into account the non-GET methods you want to offer, which means you should consider other operations, such as create, update and delete. This will help determine which of the above operations to offer - e.g.
You mention that the image might be used for other types of images, but that a banner could only have one image. If that is the case, I'd suggest reversing the direction of the relationship, e.g.:
table Banner table Images
________________________ ______________________
| id | Int | | id | Int |
| title | VARCHAR | | filename | VARCHAR|
| image_id | Int | |___________|________|
|__________|___________|
Otherwise, how would you handle it if two records in the Image table had the same banner_id?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With