I'm creating an API based on REST concept but I'm still a bit confused talking about relating resources.
I've a website where people can signup in multiple groups and choose multiple roles. For example let's take people that signup in companies as scenario:
Companies
- Apple
Roles
- Marketing
- Sales
- Development
- Customer support
So, when I want to create a user in a new company with certain roles, I would pass something like this into a POST request to /users endpoint
{
"username" : "raffaele.izzia",
"email" : "example@email.com",
"groups" : [{
"id" : 1,
"roles" : ["Sales","Customer support"]
},
{
"id" : 2,
"roles" : ["Sales","Marketing"]
}]
}
With this approach, once I get some users from the API I always know in which groups/roles they are.
But what about requests on /groups endpoint?
If I GET /groups/google I should receive info about users/roles too. So it could be something like this
{
groups: [{
"id" : 2,
"name" : "Google",
"users" : [2,3,4,10,35,50] //role will be included in the single resource once you expand this collection
}]
}
or maybe this:
{
groups: [{
"id" : 2,
"name" : "Google",
"roles" : [{
"name" : "Sales"
"users" : [2,3,4,10]
},{
"name" : "Marketing"
"users" : [4,10,8,57]
}]
}]
}
What do you think is the best solution for this kind of relationships?