2016-09-23 46 views
-2

我正在通過爲「問題跟蹤應用程序」創建RESTful服務來學習REST體系結構。現在你們許多人都知道ITA有用戶,項目,問題和評論。如何管理RESTful API中的關係?

的關係如下:

  • 項目有問題,這些問題也受到了一定的用戶進行管理。
  • 問題有評論,反過來也由某個用戶管理(讓我們發佈)
  • 項目也由用戶管理。

我被困在決定構建路線。我在一個文本文件中寫了一些路線,我在這裏發佈。請看看並提出改進建議並找出錯誤。

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,因爲關係在這個架構中最重要和最艱難的事情的用戶。

+0

可能是[codereview.se] –

+0

@ DanielA.White爲什麼我會在此發佈代碼審查?是不是有一些叫做學習? 是的,它被稱爲學習有效的方法。 –

+0

在引用特定資源(即'api/user /:id',而不是「用戶」)時最好使用單數形式。複數返回數組,單數返回對象。 – georg

回答

1

我想對DELETE動詞發表評論。在所有記錄上使用PUT和DELETE是一種不好的做法。

你已經在幾個地方的所有實例上使用了DELETE。這可能是你的要求。在這種情況下,別無選擇,但如果這不是您的絕對要求,最好遵循最佳做法。

+0

哦,是的,+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的結束,但是這只是我個人的口味。