I'm creating a REST API for my application, and I have a doubt about how to design my API, following REST principles. I'm using PHP and Lumen, but I guess it's more a design doubt than a technical one.
Consider I have two entities:
Company:
- id
- name
User:
- id
- name
- email
- company_id
Each Company
must have one or more User
's, and I'd like to assert that rule in my API. As far as I've understood REST specifications, I should create one endpoint for each entity, so the API client should make a POST to http:\\myserver\api\company
to include a company, and after that make another POST to http:\\myserver\api\company\{id}\users
to include a User
in the new Company
. The problem with that approach is that the client could only create a Company
and leave it without User
's.
In a non-rest API, I could create a method called createNewUser
, which would receive the user's data together with Company
data, and this method would ensure that both entities are created.
How could I achieve that in a REST API?