mercredi 13 septembre 2017

What is the best practice for storing and updating a display order in a REST API?

We would like to store a property in a data-store that keeps track of the display order in the clients. However the clients will be able to move entities around, and we then need to update the display order.

The most obvious solution I can think of is just storing the display orders as integers in the same sequence as the entities is displayed. The problem with this solution is that if I move the last item first, I will need to update all items in the list.

The other idea I can come up with is to store the display order as a sequence with gaps like 1000, 2000, ..., 100 000. When moving entities we will always give the display value as the middle value of the entities you are putting it between, so if I want to put an entity between the entities with display value 1000 and 2000 I would give it the value 1500. With this solution you mostly would just have to update the entity you are moving, but if you run out of numbers between two nodes you would have to make some kind of rearrangement affecting more entities. However you could make the gap so big that it would almost never happen.

Is there any best practice to solve this problem?

Aucun commentaire:

Enregistrer un commentaire