REST的语法应该是什么样的操作(例如插入/更新/删除)多个2-table表,如下面针对我的Rails应用程序所述。
create table foos (id integer(11), name varchar(255))
create table bars (id integer(11), name varchar(255))
create table foo_bars (foo_id integer(11), bar_id integer(11)
foo_id FK foos(id)
bar_id FK bars(id)
我应该使用类似的东西吗?
POST /foo/:fooid/bar (to create fooid, barid)
PUT /foo/:fooid/bar/:barid (to update fooid, barid)
DELETE /foo/:fooid/bar/:barid (to delete a foo_id, bar_id combination)
答案 0 :(得分:1)
我会考虑使用第三个端点,可能名为checkouts
来表示用户签出的书,就像你的foo-bars表一样。我认为这是可取的,因为除了用户和书籍ID之外,这种关系还有与之相关的其他信息。例如,这本书何时归还?您还可以使用此端点来跟踪有关图书或用户的历史活动的历史信息。我出于示例目的使用布尔值,但日期更合适。
// check out some books from the library
POST /checkouts
{ "userId":25, "bookIds": [ 12, 45, 341 ] }
// renew the checkout for another month
PUT /checkouts
{
"user": "/users/25",
"book": "/books/12",
"dueBack": 1438612187,
"returned": false,
...
}
// get all books currently checked out by Bob
GET /checkouts?userId=25&returned=false
{
"checkouts" : [ {
"user": "/users/25",
"book": "/books/12",
"dueBack": 1438612187,
"returned": false,
...
},
...
]
}
// find the checkout history of War and Peace
GET /checkouts?bookId=12