I am trying to figure out the best way to call REST actions that perform multiple actions and multiple database updates from a single call.
In my data model, I have Diners and LunchBoxes, and Foods. LunchBoxes are just many-to-many relationships between Diners and Foods, but with a count attribute that says how many of that type of food the given Diner has.
I want to set up a call that indicates that the Diner has eaten one of their Foods, which increases the health of the Diner accordingly. Certain foods are more nutritious than others, and consequently increase the Diner's health by different amounts. The actions that constitute this would be:
- Reduce the count attribute on the Diner's LunchBox for the given food by the correct amount
- Increase the Diner's health accordingly
So two tables need to be updated here: Diner and Lunchbox, both within a single transaction.
Trying to use nouns, the best I could come up with was:
POST /diner/frank/meal
Where the XML describing a meal would be something like
<meal>
<food>
<id>apple</id>
</food>
<count>2</count>
</meal>
However, this strikes me as pretty contrived. POSTing a meal in REST should create a Meal resource. In this case not only are we not creating a Meal resource, but we are updating two other resources: Diner and LunchBox.
I suppose one approach is to have the client handle this in two separate calls - one to update the Diner, and one to update the LunchBox. However, this seems wrong because we have multiple clients (HTML, Flash, etc.) that all need to perform this action. If we ever update the business logic in the future that is used to consume foods then we would need to make that change on many clients instead of on a single server.
How have others approached this admittedly pretty basic problem?