Performing multiple database operations in a single REST call

I am trying to find a better way to invoke REST actions that perform several actions and multiple database updates from a single call.

In my data model, I have Diners and LunchBoxes, and Foods. LunchBoxes are just a many-to-many relationship between Diners and Foods, but with a count attribute that tells how many of this type of food this Diner has.

I want to set up a challenge that indicates that Diner has eaten one of his products, which accordingly increases Diner's health. Certain foods are more nutritious than others, and therefore increase Diner's health in varying amounts. The actions that make up this will be as follows:

  • Decrease the count attribute on the Diner LunchBox for this food by the correct amount.
  • Increase Diner Health accordingly.

So, you need to update two tables: Diner and Lunchbox, both within the same transaction.

Trying to use nouns, the best I could come up with was:

POST / diner / frank / meal

Where the XML describing the food will be something like

<meal>
  <food>
    <id>apple</id>
  </food>
  <count>2</count>
</meal>

However, it amazes me how beautifully contrived. When sending food to REST, you must create a food resource. In this case, we not only create the Meal resource, but also update two other resources: Diner and LunchBox.

, , : Diner LunchBox. , (HTML, Flash ..), . - - , , , .

, , ?

+3
2

Diner, , . , eat. REST , , . Diner, LunchBox. , , , , ..

REST , . , , .

, .

-

# routes.rb
map.resources :diner, :member => {:eat => :post}

#controller
def eat
  @diner = Diner.find(params[:id])
  @diner.eat(params[:foods])
  respond_to ...
  end
end

, . , Diner LunchBox. LunchBox. , .

UPDATE , . , REST, HTTP Rails.

, , Meal , , .

, HTTP, . http, API . , API , .

+1

-, . REST api.

, , , .

URL- .

GET /DiningService
Content-Type: application/vnd.sample.diningservice+xml
200 OK

<DiningService>
 <Link rel="diners" href="./diners"/>
 <Link rel="lunchboxes" href="./lunchboxes"/>
 <Link rel="foods" href="./foods"/>
</DiningService>

, , , , . rel= "diners" .

GET /DiningService/diners
Content-Type: application/vnd.sample.diners+xml
200 OK

<Diners>
 <Diner Name="Frank">
  <Link rel="lunchbox" href="./Frank/lunchbox"/>
 </Diner>
 <Diner Name="Bob">
  <Link rel="lunchbox" href="./Bob/lunchbox"/>
 </Diner>
</Diners>

, . , - Atom- . , . , Frank rel= "lunchbox". URL .

GET /DiningService/Frank/lunchbox
Content-Type: application/vnd.sample.lunchbox+xml
200 OK

<Lunchbox>
 <Link rel="diner" href="/DiningService/Frank"/>
 <Food Name="CheeseSandwich" NutritionPoints="10">
          <Link rel="eat" Method="POST" href="/DiningService/Frank?food=/DiningService/Food/CheeseSandwich"/>
 </Food>
 <Food Name="CucumberSandwich" NutritionPoints="15">
  <Link rel="eat" Method="POST" href="/DiningService/Frank?food=/DiningService/Food/CucumberSandwich"/>
 </Food>
</Lunchbox>

, , , , , . , URL-, , rel= "" URL-. .

POST /DiningService/Frank?food=/DiningService/Food/CucumberSandwich
Content-Type: None
200 OK

, URL-, ,

<Link rel="eat" Method="POST" href="/DiningService/Frank/Mouth?food=/DiningService/Food?id=759"/>

<Link rel="eat" Method="POST" href="/DiningService/Food/CheeseSandwich?eatenBy=Frank"/>

, rel= "" URL-. , URL- -, . URL- , .

, , URL. "URL RESTful" , REST, - SOAP!

+1

Source: https://habr.com/ru/post/1730626/


All Articles