I'm wondering about best practice when designing the URIs for your REST API that contains resources with relationships between them.
I have 2 entities; Team and Player, and these URIs:
GET /teams <- list teams
GET /teams/{id} <- get single team
GET /teams/{id}/players <- list players in a team
POST /teams/{id}/players <- create a player and add to it to the given team
Now here's my question; the player being created through POST /teams/{id}/players
will have its own ID (a UUID) and live as a row in its own database table. This means that getting this player will have 2 alternative URIs that contains the necessary information for GETing this resource back:
GET /teams/{id}/players/{id} <- returns PlayerDto
GET /players/{id} <- returns PlayerDto
This really confuses me as the team ID will be redundant in the first case, wouldn't it?
It does however make more sense if you have two separate resources returned by the endpoints, for example:
GET /teams/{id}/players/{id} <- returnes TeamPlayerDto, which contains contextual information for the player in the given team
GET /players/{id} <- returns PlayerDto, which contains general information about the player, like which teams he has belonged to throughout his career
In your opinion, what makes more sense in this case? Is GET /players/{id}
sufficient if you only have a single representation (like PlayerDto
) for your resource?