且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将项目添加到数组字段的正确REST端点是什么?

更新时间:2023-09-09 19:00:52

其中资源表示为链接

where resources are represented as links

这不是事实.链接可能是操作调用,因此它们表示可能的资源状态转换.

This is not true. Links are possibly operations calls, so they are representing possible resource state transitions.

要向集合中添加内容,您需要一个集合资源,并且必须确定要在该集合中存储的内容.在您的情况下,这可能是两件事:团体学生会员身份或学生.如果这是1:n关系,则可以存储学生并删除学生.如果这是n:m关系,则您必须存储成员身份并删除成员身份,因为您不想从存储中删除学生,只需成员身份即可.

To add something to a collection, you need a collection resource and you have to decide what you want to store in that collection. In your case this can be 2 things: group-student memberships or students. If this is an 1:n relation, then you can store students and remove students. If this is an n:m relation then you have to store memberships and remove memberships, since you don't want to remove the students from your storage, just the memberships.

您可以通过两种方式识别成员身份:

You can identify the memberships 2 ways:

  • 您可以使用参与者的ID:/groups/1/memberships/student:1/students/1/memberships/group:1
  • 您可以为每个成员资格添加唯一的ID:/memberships/1234
  • you can use the ids of the participants: /groups/1/memberships/student:1 or /students/1/memberships/group:1
  • you can add a unique id to each membership: /memberships/1234

注释:

  • URI结构仅从人的角度来看很重要. REST客户端将检查链接关系,而不是URI结构.
  • 资源与数据库中的实体不同.只有通过简单的CRUD应用程序才能代表它们相同的事物.因此,REST与您的数据库结构无关.