开发者

Ruby on Rails REST design question - transfer money between accounts

开发者 https://www.devze.com 2023-01-29 18:38 出处:网络
I have an Account class, want to implement transfer screens to allow a user to transfer money between 2 accounts.

I have an Account class, want to implement transfer screens to allow a user to transfer money between 2 accounts.

How would I implement this ins RESTfull way?

I have the standard account and rest actions for that, thats fine. But how wo开发者_如何学编程uld I implement transfer?

Normally I would just add a method called "transfer" (called to render the screen) and "transfer_update"(called on submit) to the accounts controller and corresponding views, but I don think this is very RESTfull.

thanks Joel


You mention your Account class, but not the class that represents postings or journals. See http://homepages.tcp.co.uk/~m-wigley/gc_wp_ded.html (Archived).

Using the language of the referenced site, the "resource" that's being created for a transfer is a journal (entry), consisting of two postings, each to different accounts. So you would want a JournalsController. To add a transfer you would POST to the index action of the JournalsController. Parameters would include date, amount, debit_account, credit_account, payee, memo, etc.

Using REST on AccountsController would be for creating, updating, or deleting accounts, not postings (transactions) which are contained by accounts.


An example of a restful request to perform a transfer.

POST /transfers HTTP/1.1
Host: restful.bank.com
Content-Type: application/json; charset=utf-8
Accept: application/json

{ "transfer": {
  "source_account_id": "9d2d894c242f391a",
  "destination_account_id": "83ac039d8302abd5"
  "amount": "$200.00"
} }

Corresponding response.

HTTP/1.1 201 Created
Date: #{right-now}
Content-Type: application/json; charset=utf-8
Location: https://restful.bank.com/transfers/938ac39cb5ddccfa

{ "transfer": {
  "id": "938ac39cb5ddccfa",
  "href": "https://restful.bank.com/transfers/938ac39cb5ddccfa",
  "source_account_id": "9d2d894c242f391a",
  "destination_account_id": "83ac039d8302abd5"
  "amount": "$200.00"
} }


The book RESTful Web Services has a good example of how to approach this exact problem, and what's better, the example is in Rails :)

If you can't check it out from a library, what the heck, just buy the thing. It's not that expensive and it has lots of useful information about how to implement REST and ROA.

0

精彩评论

暂无评论...
验证码 换一张
取 消