在符合REST的情况下更新序列中单个资源的位置?

时间:2014-10-22 22:09:19

标签: api rest architecture

鉴于以下连续收集的书籍:

请求: GET /favorites/books/

响应:

[
    {
        "id": "50"
        "title": "The Hitchhiker's Guide to the Galaxy by Douglas Adams",
        "position": 1
    },
    {
        "id": "51"
        "title": "1984 by George Orwell",
        "position": 2
    },
    {
        "id": "52"
        "title": "Dune by Frank Herbert",
        "position": 3
    },
    {
        "id": "53"
        "title": "Slaughterhouse 5 by Kurt Vonnegut",
        "position": 4
    },
    {
        "id": "54"
        "title": "Ender's Game by Orson Scott Card",
        "position": 5
    }
]

更新集合中图书资源位置的最佳方法是什么?

请求: PUT /favorites/books/54

请求正文:

{
    "id": "54"
    "title": "Ender's Game by Orson Scott Card",
    "position": 2
}
  1. API应该自动将其他图书资源的位置改回1吗?
    • 如果是这种情况,客户如何知道整个列表的新“序列状态”? PUTIT只有一本书后,我应该回复整个列表吗?
  2. 我是否应仅允许更新集合而不是单独的图书?

1 个答案:

答案 0 :(得分:1)

宁静的方法是将职位列表视为资源。

然后你会做类似的事情:

PUT /favorites/books/positions
[ 
  { "book_id" : "50", "position" : "1"},
  { "book_id" : "54", "position" : "2"},
  { "book_id" : "51", "position" : "3"},
  ...
]