将项目添加到数组字段的正确REST端点是什么? [英] What is the correct REST endpoint for adding an item to an array field?

查看:70
本文介绍了将项目添加到数组字段的正确REST端点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我正在尝试模拟用Gogo与MongoDB编写的RESTful API将学生添加到组中的动作.

Say I'm trying to model the action of adding a Student to a Group in a RESTful API written in Go with MongoDB.

组的建模如下:

type Group struct {
Section mgo.DBRef
Instructor mgo.DBRef
Students []mgo.DBRef
}

另一个限制是该API正在实现 HAL + JSON 协议,其中资源表示为链接.

An additional constraint is that the API is implementing HAL+JSON protocol, where resources are represented as links.

我已经看到了几个选择(如下):

I've seen a couple of options (below):

  • POST/groups/{groupID}/students/{studentID}会将具有学生ID的学生添加到该组中.这种方法的问题在于,由于我正在实现HAL + JSON协议,所以我不希望客户端手动提取ID并生成此链接.所有资源都将被表示,即/person/123可能是一个学生.

PUT/groups/{groupID},同时发送应属于该组的完整学生组.似乎它将引入很多复杂的解析逻辑.

PUT /groups/{groupID} while sending the complete array of Students that should belong to the group. This seems like it will introduce a lot of complicated parsing logic.

如果还有其他选择,我也会开放的.

If there are other options I'd be open to it too.

我要使用的方法如下: * POST/groupmembership/,通过发送带有学生ID和要添加学生的组ID的JSON.但是,在后端,我不是生成新模型,而是获取对象,并以编程方式将指定的学生添加到指定的组中.

The approach that I'm going with is the following: * POST /groupmembership/ by sending a JSON with the ID of the student and the ID of the group to add the student to. However, on the backend, I'm not generating a new model, but instead taking the object and programmatically adding the specified student to the specified group.

然后的问题是我如何将学生从小组中删除?我可以使用

The question then is how would I remove the Student from the Group? Can I similar send a DELETE request to /groupmembership with

{
  "student": 123,
  "group": 456
}

要从组456中删除学生123?

to remove student 123 from group 456?

推荐答案

其中资源表示为链接

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与您的数据库结构无关.

这篇关于将项目添加到数组字段的正确REST端点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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