如何在RESTful API中管理关系?

时间:2016-09-23 11:07:40

标签: javascript node.js web-services rest

我正在通过为“问题跟踪应用程序”创建RESTful服务来学习REST架构。现在,许多人都知道ITA有用户,项目,问题和评论。

关系如下:

  • 项目存在问题,这些问题也由特定用户管理。
  • 问题有评论,而评论又由某个用户管理(假设已发布)
  • 项目也由用户管理。

我坚持决定构建路线。我在文本文件中写了一些路由,我在这里发帖。请查看并提出改进建议并确定错误。

    Models:
  - User
  - Project
  - Issue
  - Comment

1. User
  > Immediate Requirement
    GET       api/users/                /* Authenticate, and then return list of all users */
    GET       api/users/:id             /* Authenticate, and return a particular user */
    POST      api/users/                /* Create a new user */
    PUT       api/users/:id             /* Authenticate, and update a particular user */
  > Add-on Requirement
    GET       api/users/:id/projects/   /* Authenticate, and return list of projects of particular user */
  > Never Mind Requirement
    DELETE    api/users/                /* Authenticate, and delete all users */
    DELETE    api/users/:id             /* Authenticate, and delete a particular user */

2. Project
  > Immediate Requirement
    GET       api/projects/             /* Authenticate, and then return list of all projects */
    GET       api/projects/:id          /* Authenticate, and return a particular project */
    POST      api/projects/             /* Authenticate, and create a new project */
    PUT       api/projects/:id          /* Authenticate, and update a particular project */
  > Add-on Requirement
    GET       api/projects/:id/issues   /* Authenticate, and return a list of issues of particular project */
    POST      api/projects/:id/issues   /* Authenticate, and create new issue for particular project */
    DELETE    api/projects/:id/issues   /* Authenticate, and delete all issues of particular project */
  > Never Mind Requirement
    DELETE    api/projects/             /* Authenticate, and delete all projects */
    DELETE    api/projects/:id          /* Authenticate, and delete a particular project */

3. Issue
  > Immediate Requirement
    GET       api/issues/               /* Authenticate, and then return list of all issues */
    GET       api/issues/:id            /* Authenticate, and return a particular issue */
    PUT       api/issues/:id            /* Authenticate, and update a particular issue */
  > Add-on Requirement
    GET       api/issues/:id/comments   /* Authenticate, and return a list of comments of particular issue */
    POST      api/issues/:id/comments   /* Authenticate, and create a new comment for particular issue */
    DELETE    api/issues/:id/comments   /* Authenticate, and delete all comments of particular issue */
  > Never Mind Requirement
    DELETE    api/issues/               /* Authenticate, and delete all issues */
    DELETE    api/issues/:id            /* Authenticate, and delete a particular issue */

4. Comment
  > Immediate Requirement
    GET       api/comments/             /* Authenticate, and then return list of all comments */
    GET       api/comments/:id          /* Authenticate, and return a particular comment */
    PUT       api/comments/:id          /* Authenticate, and update a particular comment */
  > Never Mind Requirement
    DELETE    api/comments/             /* Authenticate, and delete all comments */
    DELETE    api/comments/:id          /* Authenticate, and delete a particular comment */

这篇文章将帮助许多尝试学习REST的用户,因为关系是这个架构中最重要和最难的事情。

2 个答案:

答案 0 :(得分:1)

我想对DELETE动词发表评论。在所有记录上使用PUT和DELETE是一种不好的做法。

您在少数几个地方的所有实例上都使用了DELETE。这可能是您的要求。在这种情况下没有其他选择,但如果这不是您的绝对要求,最好遵循最佳做法。

答案 1 :(得分:0)

REST不会对URI设计施加约束。例如,通过以下URI api/issues/:id/comments,唯一重要的部分是:id。其他任何东西都可以基于意见。这是因为通过REST发送回链接和链接将包含URI或URI模板,因此您的客户端将不需要从头开始重构URI。这些链接具有链接关系,客户端将使用它来检查链接的作用以及如何显示链接。所以URI仅供内部使用,没有必要记录您是否遵循HATEOAS constraint。另一个参考:http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven

如果你问我,我更喜欢平面URI结构而不是层次结构:api/comments/?issue=:id我用/放在集合URI的末尾,但这只是我的个人品味。