heme
heme

Reputation: 313

Update the position of a single resource in a sequence while conforming to REST?

Given the following sequential collection of books:

Request: GET /favorites/books/

Response:

[
    {
        "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
    }
]

What would be the best means of updating the position of a book resource in the collection?

Request: PUT /favorites/books/54

Request Body:

{
    "id": "54"
    "title": "Ender's Game by Orson Scott Card",
    "position": 2
}
  1. Should the API automatically change the position of the other book resources back by 1?
    • If this is the case how does the client know the new "sequence state" of the entire list? Should I return the entire list in response after PUTting just 1 book?
  2. Should I only allow the collection to be updated and not the individual books?

Upvotes: 2

Views: 290

Answers (1)

nsarno
nsarno

Reputation: 164

The restful way would be to consider the list of positions as the resource.

You would then do something like :

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

Upvotes: 1

Related Questions