0x00 Chapter 10:Sibling Relationships
6.To actually create a relationship between two models
you need to use the pivot
Fluent provides convenience functions for creating and removing relationships
First, open Acronym.swift
and add a new property to the model below
var user: User:
@Siblings(
through: AcronymCategoryPivot.self,
from: \.$acronym,
to: \.$category
)
var categories: [Category]
7.Open AcronymsController.swift
and add the following route handler
below
getUserHandler(_:)
to set up the relationship between an acronym and a category
// http://127.0.0.1:8080/api/acronyms/<acronymID>/categories/<categoryID>
func addCategoriesHandler(_ req: Request) throws -> EventLoopFuture<HTTPStatus> {
let acronymQuery = Acronym.find(req.parameters.get("acronymID"), on: req.db)
.unwrap(or: Abort(.notFound))
let categoryQuery = Category.find(req.parameters.get("categoryID"), on: req.db)
.unwrap(or: Abort(.notFound))
return acronymQuery.and(categoryQuery)
.flatMap { acronym, category in
acronym.$categories
.attach(category, on: req.db)
.transform(to: .created)
}
}
Register this route handler at the bottom of boot(routes:)
acronymsRoutes.post(":acronymID", "categories", ":categoryID", use: addCategoriesHandler)
提交数据
url:http://127.0.0.1:8080/api/acronyms/<acronymID>/categories/<categoryID>
method:POST
parameter:无
成功后返回的状态码是:HTTP/1.1 201 Created
8.Acronym’s categories
Open AcronymsController.swift
and add a new route handler after addCategoriesHandler(:_)
// http://127.0.0.1:8080/api/acronyms/<acronymID>/categories
func getCategoriesHandler(_ req: Request) throws -> EventLoopFuture<[Category]> {
Acronym.find(req.parameters.get("acronymID"), on: req.db)
.unwrap(or: Abort(.notFound))
.flatMap { acronym in
acronym.$categories.query(on: req.db).all()
}
}
Register this route handler at the bottom of boot(routes:)
:
acronymsRoutes.get(":acronymID", "categories", use: getCategoriesHandler)
查询数据:
url:http://127.0.0.1:8080/api/acronyms/<acronymID>/categories
method:GET
成功后返回关联的 category
[{"id":"F9CDB53A-1560-4537-9BF4-670E8598CB11","name":"Teenager"}]
9.Category’s acronyms
Open Category.swift
and add a new property annotated with @Siblings below
var name: String
@Siblings(
through: AcronymCategoryPivot.self,
from: \.$category,
to: \.$acronym
)
var acronyms: [Acronym]
Open CategoriesController.swift
and add a new route handler after getHandler(_:)
// http://127.0.0.1:8080/api/categories/<categoryID>/acronyms
func getAcronymsHandler(_ req: Request) throws -> EventLoopFuture<[Acronym]> {
Category.find(req.parameters.get("categoryID"), on: req.db)
.unwrap(or: Abort(.notFound))
.flatMap { category in
category.$acronyms.get(on: req.db)
}
}
Register this route handler at the bottom of boot(routes:)
categoriesRoute.get(":categoryID", "acronyms", use: getAcronymsHandler)
查询数据:
url:http://127.0.0.1:8080/api/categories/<categoryID>/acronyms
method:GET
成功后返回关联的 acronym
[
{
"id":"F3320C03-8570-443F-A8AB-071681470DA4",
"short":"MMD",
"long":"么么哒",
"user":{
"id":"0DFD7B3A-F38D-43AA-8DD5-26B71E4FE3D0"
}
},
{
"id":"5FB16F6E-4BD1-48DB-8211-6F7611515913",
"short":"TGD",
"long":"明天是个好日子!",
"user":{
"id":"0DFD7B3A-F38D-43AA-8DD5-26B71E4FE3D0"
}
}
]
10.Removing the relationship
Open AcronymsController.swift
and add the following below getCategoriesHandler(req:)
// http://127.0.0.1:8080/api/acronyms/<acronymID>/categories/<categoryID>
func removeCategoriesHandler(_ req: Request) throws -> EventLoopFuture<HTTPStatus> {
let acronymQuery = Acronym.find(req.parameters.get("acronymID"), on: req.db)
.unwrap(or: Abort(.notFound))
let categoryQuery = Category.find(req.parameters.get("categoryID"), on: req.db)
.unwrap(or: Abort(.notFound))
return acronymQuery.and(categoryQuery)
.flatMap { acronym, category in
acronym.$categories
.detach(category, on: req.db)
.transform(to: .noContent)
}
}
register the route at the bottom of boot(routes:)
acronymsRoutes.delete(":acronymID", "categories", ":categoryID", use: removeCategoriesHandler)
删除关联
url:http://127.0.0.1:8080/api/acronyms/<acronymID>/categories/<categoryID>
method:DELETE
parameter:无
成功后返回的状态码是:HTTP/1.1 204 No Content
0x01 我的作品
欢迎体验我的作品之一:小编辑器
在线编辑器
App Store
搜索即可~