0

我正在使用 nodejs/express 设计 restful api。

  • GET /reports(获取所有报告)
  • PUT /reports/23(使用 id 23 更新报告)
  • GET /reports/23(获取带有 id 23 的报告)
  • POST /reports(创建报告)
  • DELETE /reports/23(删除带有 id 23 的报告)

到目前为止,相当标准的东西。现在我想按时间过滤(时间戳属性)。环顾四周,我找不到任何按时进行宁静过滤的好例子。任何人都有任何好的例子。

我想出的最好的方法是:/reports?start=2013-07-13&stop=2013-7-14

没关系,除了那时我将不得不假设 <= 或 < 和 > 或 >= 并且不能在查询中表达不同的东西。

有没有代表以下查询的好方法?

  • 时间戳 > 2013-07-13 && 时间戳 < 2013-07-14
  • 时间戳 >= 2013-07-13 && 时间戳 < 2013-07-14
  • 时间戳 > 2013-07-13 && 时间戳 <= 2013-07-14
  • 时间戳 >= 2013-07-13 && 时间戳 <= 2013-07-14

提前致谢

4

1 回答 1

1

提供> or >=类似的表达式不是一个好的 REST 设计。我建议使用不同的查询参数,例如 startDate - 表示请求的过滤器持续时间的开始日期 - 从 startDate 开始,以天为单位。然后你的请求看起来像

timestamp > 2013-07-13 && timestamp < 2013-07-14 ---  startDate=2013-07-13&duration=0
timestamp >= 2013-07-13 && timestamp < 2013-07-14 --- startDate=2013-07-13&duration=1
timestamp > 2013-07-13 && timestamp <= 2013-07-14 --- startDate=2013-07-14&duration=1
timestamp >= 2013-07-13 && timestamp <= 2013-07-14--- startDate=2013-07-13&duration=2

REST API 的查询参数应该明确定义目的。

于 2013-09-13T11:50:20.273 回答