I try to find the best practice in order to create a Many-to-Many relationship in a Restful API. The usecase is really simple, but i cannot really found the "right" way to do it.
In our model, we have Kid that are related with a Many-to-Many relationships to Guardian. In the relationship table, we have 2 extra parameters, type (parent, nanny, emergency, etc.) and active (Boolean).
You can only add Guardian to a existing Kid, but an existing Guardian can be link with another Kid.
Today, we do it like that
POST kids/{kidID}/guardians
{
"type": "parent"
"active": false
"guardian": {
"first_name": "foo"
"last_name": "bar"
}
}
This create the Guardian and add it to the Kid. But, with this method we cannot handle the case where I want to add an existing Guardian to a Kid. Here the answers I found in order to represent this, but I don't know which one is the best (and restful) way (maybe none of them are good...) :
Solution 1 - Keep the endpoint as today
But put an non-mandatory id field to the guardian. If id is empty, the API have to create the ressource otherwise is just retrieve it and update values if need.
POST kids/{kidID}/guardians/
{
"type": "parent"
"active": false
"guardian": {
"id": "ab65f263-dd3d-bbc6-8b7b-57a3b4b26c21"
}
}
Solution 2 - Break this endpoint in 2 calls
# Create the Guardian
POST guardians/
{
"first_name": "foo"
"last_name": "bar"
}
# This method can only "link" the models
POST kids/{kidID}/guardians/
{
"type": "parent"
"active": false
"guardian_id": "ab65f263-dd3d-bbc6-8b7b-57a3b4b26c21"
}
[Edited] Solution 2.5 - Create the relationship with a PUT
As before, you have to create the guardian, but in order to add the relationship you make a
PUT kids/{kidID}/guardians/{guardianID}
{
"type": "parent"
"active": false
}
Subsidiary solution : In the second choice we can change the URI of the ressource by:
POST kids/{kidID}/kid-guardians/
Because it doesn't really POST a "guardian" ressource, but a kid-guardian ressource (the relationship). I don't really like it because with the old URI we can assume more easily that
GET kids/{kidID}/guardians/
Will give you all Guardians related with the Kid, but not that
DELETE kids/{kidID}/guardians/{guardianID}
Will delete the relationship and not the Guardian.
So as you understand, i'm really lost and would appreciate your help.
Best regards,