以 RESTful 方式重新排序项目 [英] Reordering items in a RESTful way

查看:36
本文介绍了以 RESTful 方式重新排序项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下数据的表:

I have a table that has the following data:

id position name
== ======== =========
1     4      Fred
2     2      Wilma
3     1      Pebbles
4     5      Barney
5     3      Betty

这是用于用户可以重新排列的列表(因此是位置列).我的问题是,我怎样才能以一种宁静的方式做到这一点.例如,如果我想将 Fred 移动到位置 2,我将如何发送请求?

This is for a list which the user can rearrange (hence the position column). My question is, how can I do this in a restful way. For example, if I want to move Fred to position 2, how would I send the request?

目前,我有这样的事情:

Currently, I have something like this:

PUT /user/1/reorder/2

这会将当前处于位置 4 的用户 1 (Fred) 移动到位置 2.SQL 代码还将运行一个查询,该查询将更改其他用户的位置以适应 Fred 的新位置.

This would move user 1 (Fred) who is currently in position 4 to position 2. The SQL code would also run a query that would change the position of the other users to adjust for Fred's new position.

执行此操作的正确 RESTful 方法是什么?

What is the proper RESTful way to do this?

推荐答案

为了有效地做到这一点,请使用哈希表并且从不重新排序所有内容,只需通过更改索引键将它们放在中间即可.什么意思?

In order to do it efficiently use a hash table and never reorder everything just put them in between by changing their index key. What do I mean?

让我们说

1 a
2 b
3 c
4 d
5 e

PUT /user/1/reorder/2

在我会做的里面,位置也是从索引中的位置导出的

Inside I would do, also the position is derived from the position in your index

1  2   b
2  2.5 a
3  3   c
4  4   d
5  5 e

正如 Kajow 所指出的,这种方法有两个问题:

As noted by Kajow, this approach has two issues:

  • 多次重新排序后,浮点数可能不够

  • After multiple reorders, float numbers might not suffice

浮点数精度

解决它们:

  • 我们不使用浮点数,而是使用大数字 100000、200000、300000 等并重新排序、200000、250000、30000 等

  • We don't use floats but big numbers 100000,200000,300000 etc and reorder, 200000,250000,30000,etc.

在 x 次重新订购后,我们重置为 100000,200000,300000,400000.可能有一种方法每 x 个分区只对小部分重新排序,以保持这个过程简单、小而零星

After x number of reorders we reset to 100000,200000,300000,400000. There might be a way to reorder only small parts every x divisions in order to keep this process simple, small and sporadic

这篇关于以 RESTful 方式重新排序项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆