So I got the models User
& Poll
where a user can create many polls (so we got a 1:n relation here) and the join table Participant
as a user can be part of many polls and a poll can have many users.
I need to define two REST endpoints: One for getting a list of polls created by a user and one for getting a list of polls where a user is a participant.
For the first endpoint I defined:
GET /users/:id/polls
... which is the usual way of defining 1:n relations.
But for the second endpoint I'm completely clueless how this would look like in REST. I basically have the user and need all the polls where the user is a participant. I want to get things done and don't want to waste too much time so I came up with this abomination:
GET /users/:id/polls/participation
Has anyone a sane idea how this can be designed? Is it even possible to map this in a single call with REST?
I would advocate making polls
a top-level resource. You can then support
GET /polls?createdBy={userId}
GET /polls?participant={userId}
You can add other query parameters as needed. Another alternative is to have a separate resource, poll-participants
, which holds the mapping.
GET /poll-participants?creator={userId}
GET /poll-participants?particpant={userId}
In this case, you'd get a resource which has a list of mapping objects. They could hold the whole user and poll in each one, or they could have links you have to follow to GET the full user and poll. This works best when the user and poll are cacheable.