Mrvonwyl
Mrvonwyl

Reputation: 347

Nested/Chained Resources in Rest

This is a more of an architectural question. And I am rather new two Rest.

Let's assume these resources /offers and /offers/:id. And the offer does have a single connection to an organization. My first thought was creating a resource: /offers/:id/organization This would be singular, because it feels unnatural to call the resource in plural, when there is always only one element returned.

First question: Would you always use plural no matter what?

This /offers/:id/organization/:id seems to be useless, because there is only one organization linked to the offer.

To make things complicated. The organizations need to be a separate resource as well: /organizations and /organizations/:id.

So basically I have to ways to achieve my goal. I could get the /offers. And then with the retrieved organizationId get /organization/:id. Or I could nest the organization into the offer that I get everything in one request either /offers or /offers/:id.

The second option would potentially get rid of /offers/:id/organization(s). Except I wanted to get the organization by the offerId and not be the organizationId. (One organization has cn offers).

Second question: When there is a standalone resource, i.e /organization, should I bother of implementing a nested resource as well /offer/:id/organization(s).

There is also the issue of how to implement the services (i am using jersey) if the organization is available at /offers/:id/organization(s) but that is probably a Question on its own.

Any thoughts?

Upvotes: 0

Views: 166

Answers (1)

sschrass
sschrass

Reputation: 7156

As usually, it depends.

If your offer has only a single organization

/offers/:id/organization 

is fine, because that is how your domain-model works.

This is different at

/organizations/:id/offers/:id

because, so I suspect, an organization could have multiple offers. So the id makes sense, also the resource /organizations/:id/offers, what are all offers for this organization.

You could implement

/offers/organization/:id

what could redirect to /organizations/:id/offers because it is semantically the same.

Upvotes: 2

Related Questions