假设我有 4 个表:类(class)、教程、测验和教程-测验。
关系如下:
- 类(class)和教程是一对多:每门类(class)有多个教程,每个教程属于一门类(class)。
- 类(class)和测验是一对多:每门类(class)有多个测验,每个测验属于一门类(class)。
- 教程和测验是多对多:每个教程可以分配给多个测验,每个测验也可以分配给多个教程。
- 教程测验充当两者之间的桥梁。每条记录包含两个字段,分别是教程和测验的 ID。
现在,对于一门特定类(class),我可以通过一次操作将多个教程分配给多个测验(批量)。当为教程分配测验时,教程-测验表中会添加一条新记录。
执行此批量分配的适当方法和资源名称是什么?
PATCH /courses/id/tutorials/quizzes
PATCH /courses/id/quizzes/tutorials
PATCH /courses/id/tutorials-quizzes
这个question与我所要求的有点相关,但我的略有不同,因为我在操作中分配两个不同集合中的多个项目。
最佳答案
您可以使用 POST 或 Patch
帖子: POST 方法通常用于在列表资源上使用时添加元素,但您也可以支持此方法的多个操作。
POST /batch
[
{ method: 'POST', path: '/items', body: { title: 'foo' } },
{ method: 'DELETE', path: '/items/bar' }
]
POST/courses/id/tutorials/quizzes
补丁: 在这种情况下,无需定义描述更新的格式。 使用 PATCH 方法也是合适的,因为相应的请求对应于部分更新。根据 RFC5789 ( https://www.rfc-editor.org/rfc/rfc5789 )
PATCH /items
[ { id: 1, name: 'foo' }, { id: 2, name: 'bar' } ]
PATCH /courses/id/tutorials/quizzes
示例:Google 云端硬盘 API
发布https://www.googleapis.com/batch
Accept-Encoding: gzip
User-Agent: Google-HTTP-Java-Client/1.20.0 (gzip)
Content-Type: multipart/mixed; boundary=END_OF_PART
Content-Length: 963
--END_OF_PART
Content-Length: 337
Content-Type: application/http
content-id: 1
content-transfer-encoding: binary
POST https://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id
Authorization: Bearer authorization_token
Content-Length: 70
Content-Type: application/json; charset=UTF-8
{
"emailAddress":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0d5c8d1ddc0dcd5f0d1c0c0c3c2dfd3dbc39ed3dfdd" rel="noreferrer noopener nofollow">[email protected]</a>",
"role":"writer",
"type":"user"
}
--END_OF_PART
Content-Length: 353
Content-Type: application/http
content-id: 2
content-transfer-encoding: binary
POST https://www.googleapis.com/drive/v3/files/fileId/permissions?fields=id&sendNotificationEmail=false
Authorization: Bearer authorization_token
Content-Length: 58
Content-Type: application/json; charset=UTF-8
{
"domain":"appsrocks.com",
"role":"reader",
"type":"domain"
}
--END_OF_PART--
https://developers.google.com/drive/v3/web/batch
https://saw.saas.hp.com/help/en/full/Content/8000_DeveloperGuide/ApiRESTBulkUpdate.htm
资源命名指南:
http://www.restapitutorial.com/lessons/restfulresourcenaming.html
关于rest - 在多对多关系中进行批量分配的适当 REST 端点是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45222852/