应该使用什么 REST url 来访问多对多 table 中的数据
What REST urls should be used for accessing data from a many-2-many table
用于操纵(例如 insert/update/delete)多对多 table 的 REST 调用的语法应该是什么,如下所述,我的 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)
我会考虑使用第三个端点,可能命名为 checkouts
,来表示用户签出的一本书,就像您的 foo-bars table。我认为这是可取的,因为除了用户和图书 ID 之外,该关系还有其他相关信息。例如,这本书什么时候归还?您还可以使用此端点来跟踪有关书籍或用户历史 activity 的历史信息。我使用布尔值作为示例,但日期更合适。
// 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
用于操纵(例如 insert/update/delete)多对多 table 的 REST 调用的语法应该是什么,如下所述,我的 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)
我会考虑使用第三个端点,可能命名为 checkouts
,来表示用户签出的一本书,就像您的 foo-bars table。我认为这是可取的,因为除了用户和图书 ID 之外,该关系还有其他相关信息。例如,这本书什么时候归还?您还可以使用此端点来跟踪有关书籍或用户历史 activity 的历史信息。我使用布尔值作为示例,但日期更合适。
// 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