We have a RESTful API with /devices and /buttons, whereas one device may have 0 to ca. 1000 buttons. The request GET /devices/{deviceId}/buttons implements therefore pagination (with startIndex and limit). Each button has a sortSequence property, which is a number. Buttons must be sorted based on this property (there is no relation to alphabetic name or sth else, buttons are ordered based on the wishes of the end user with drag and drop in the list).
The question is, how do you handle the modification of the sortSequence? Now we have it in the PUT /devices/{deviceId}/buttons/{buttonId} Payload: [... and new sequence]. But what about other buttons? E.g. if we have four buttons with sequences 1,2,3,4 and the user drag and drops the last button on the first position, not one change, but several take place: 4->1, 1->2, 2->3, 3->4.
Who is responsible for the ensuing changes, the client or the server?
If client, than:
- Through one drag and drop, the client has to send possibly hundreds of REST calls to modify the sequence of all buttons in between
- As there is paging, the client cannot create a new button without querying all of them, as it needs to know the highest sequence
If server, than:
- A PUT to one resource (button) updates a bunch of other buttons, which I see as a violation of REST principles. Also it triggers a bunch of web socket update events for each button, which slow the UI down.
- If create/modify/delete buttons requests are coming quickly, and sequences somewhere in the middle are modified, the server shall run into concurrency exceptions, when multiple parallel request threads will try to update the same objects possibly to different values
And of course, whoever does it, if a button is created nearly at the same time from two clients, the sequence might dublicate itself.
Both approaches seem rather bad and problematic. So is there a generally accepted best practice how to handle such modifications?
Infrastructure: .net core server, signalR, web/iOS/Android clients, ms sql db