I'm designing a pragmatic REST API and I'm a little stuck on how best to add existing entities to a collection. My domain model includes a Project that has a collection of Sites. This is a strict many-to-many relationship and I have no need to create an entity that explicitly models the relationship (i.e. ProjectSite).
My API will allow consumers to add an existing Site to a Project. Where I'm getting hung up is that the only data I really need are ProjectId and SiteId. My initial idea was:
1. POST myapi/projects/{projectId}/sites/{siteId}
But I also thought about
2. POST myapi/projects/{projectId}/sites
with a Site entity sent as JSON content.
Option 1 is simple and works but doesn't feel quite right, and I have other relationships that cannot follow this pattern so it adds inconsistency to my API.
Option 2 feels better but leads to two concerns:
- Should I create a Site or throw an exception if a new Site is posted (SiteId = 0)?
- Because I only need ProjectId and SiteId to create the relationship, the Site could be posted with wrong or missing data for other properties.
A 3rd option is to provide a simple endpoint solely for creating and deleting the relationship. This endpoint would expect a JSON payload containing just ProjectId and SiteId.
What do you think?