0

For example let's say I have employees and restaurant resources:

Employee {ID, Name, Gender, Birthday} exposed to GET\POST\PUT\DELETE
Restaurant {ID, Name, Address, Business_Number} exposed to GET\POST\PUT\DELETE

Also, I have many to many resources:

Restaurant_Has_Employee {ID, Employee_ID, Restaurant_ID, Employee_Work_In_Restaurant}

So the question is, what is the best practise to expose GET\POST\PUT\DELETE to this resource?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Shay Zambrovski
  • 401
  • 5
  • 21

1 Answers1

2

I always do this using the following pattern:

parents/{id}/children

In your case, if for the sake of the example a given restaurant had the id of 55, then the url GET restaurants/55/employees would a return a list of employees that belong to that restaurant, and POST restaurants/55/employees would be used for inserting a new employee to that restaurant's record. PUT isn't really relevant here, since it should be used directly on the employee endpoint, but DELETE restaurants/55/employees could be used to delete all employees belonging to that restaurant.

BTW, this looks like a duplicate of this question.

Edit: Made path nouns plural

ygesher
  • 1,133
  • 12
  • 26
  • 1
    Call it `restaurants/55/employees` (plural) which is closer to the REST guideline. – Quality Catalyst Feb 18 '18 at 21:11
  • @ygesher so if I call `restaurant/55 GET` it should return also the employees with the `restaurant {ID, Name, Address, Business_Number}` – Shay Zambrovski Feb 19 '18 at 05:42
  • No, usually a call like `restaurant/55` just returns the restaurant object. Depending on the relationship between your entities (`restaurant` and `employee`), it may make sense to return an array of employees as a member of the restaurant object (which in turn kind of makes the `restaurant/55/employee` call redundant, but not against the rules) – ygesher Feb 19 '18 at 15:43