1

/group/1并且是/group/2/item/42成员/group/1。我想从中删除/item/42并将/group/1其放入/group/2. 我目前的解决方案是这样的:

GET    /group/1/item/42   => Get the item from the first group
POST   /group/2/item      => Create a clone of the item in the 2nd group
DELETE /group/1/item/42   => Delete the original item from the 1st group

这个解决方案有(至少)两个严重的问题:

  1. 如果客户端在 之前停止DELETE,则该项目将成为两个组的成员。
  2. 项目的 ID 在 中不会相同/group/2,这看起来好像项目会失去其身份。

如果我想在一个步骤中更改项目的组成员身份(如果可能,保留其身份),我应该如何重新设计 API?

4

2 回答 2

1

在您的情况下,我不会使用 URI 将项目链接到组。

如果项目到组是 1 到 n 关系,则每个项目都应该有一个到其组的链接(例如,数据库外键)。

因此,您的 URI 空间可能要简单得多:

./[groups|items]/{id}

RESTful 方式是使用 POST 修改项目资源。

POST /items/42 { group: 2 }

在这种情况下,您的后端将请求标识为对现有资源的更新。

以下是 API 如何工作的一些演示。

GET /items --list of all items by IDs
GET /groups --list of all groups by IDs
GET /items/42 --item 42 properties

POST /items { id: 33, name: "Cool Item", group: 2} -- adds a new item, linked to group 2
PUT /groups/4 { id: 4, name: "Hot group"} --adds a new group
POST /groups/4 {name: "Cool group" } --updates the name of group 4
POST /items/33 { group: 4 } --moves the new item 33 to group 4

GET /items?group=4 --list of all items belonging to group 4

如果需要,您可以使用 POST 而不是 PUT。

于 2013-09-15T18:08:31.730 回答
0

如果您需要原子操作,我会考虑

POST /group/2?moveFrom=/group/1/item/42
于 2013-09-13T13:03:45.957 回答