Suppose we have a M:M relationship between a User model and a Project model in a project management SaaS. Naturally we have a pivot table mapping users to projects. An entry is deleted either if the user leaves or if the project manager removes that user from the project.
Even though the final result is essentially the same (i.e., the entry is deleted), I imagine it would be better to distinguish between these two actions by defining two separate API endpoints as follows:
user leaves project
route DELETE /users/{id}/projects/{id}
action UserProjectController@destroy($id) where $id refers to the project
project manager removes user
route DELETE /projects/{id}/participants/{id}
action ProjectParticipantController@destroy($id) where $id refers to the user
Should I ignore Cruddy by Design and RESTful design principles and simply define leave() join() remove() actions instead and use verbs in the URIs? like so:
POST /projects/{id}/join
POST /projects/{id}/leave
POST /projects/{id}/participants/{id}/remove
join,leave,removeare arguably RPC rather than REST. Pivot tables etc are how the domain is implemented and are irrelevant to the caller of the API, which works at the domain level. How do the URLs map to your domain model?If
Projecthas one or moreUserwhy not just useto manage everything about
ProjectandParticipantinstances?Useris in aProjectbut only ifhas been called.
FWIW,
looks like it's deleting the
Projectinstance owned by aUseras theProjectobject hasparticipantsbut theUserdoesn't use that terminology. Perhaps it would be:for consistency with the domain model.
However it seems much easier to just use
Projectto manageProjectinstances. Removing aUserfrom aProjectusing theUserAPI seems a convenience that complicates the backend and doesn't really match theProjectterminology. That would mean only havingUserControllerandProjectControllerclasses.You would have to consider whether a
Participantidis the same as aUseridand whether the pivot table handles that but that won't affect the API. The API should be an intuitive representation of your domain model.