0x00 Chapter 9: Parent-Child Relationships
1.Parent-child relationships describe a relationship where one model has “ownership” of one or more models . They are also known as one-to-one and one-to-many relationships.
2.In the TIL application, users will create acronyms . Users (the parent) can have many acronyms , and an acronym (the child) can only be created by one user.
3.Creating a user create User.swift in Sources/App/Models create CreateUser.swift in Sources/App/Migrations create UsersController.swift in Sources/App/Controllers
用户数据模型:User.swift
import Vapor
import Fluent
final class User: Model, Content {
// 数据库中的表名
static let schema = "users"
// id 字段
@ID
var id: UUID?
// name 字段
@Field(key: "name")
var name: String
// username 字段
@Field(key: "username")
var username: String
init() {}
init(id: UUID? = nil, name: String, username: String) {
self.name = name
self.username = name
}
}
用户数据表创建: CreateUser.swift
import Fluent
struct CreateUser: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void>{
database.schema("users")
.id()
.field("name", .string, .required)
.field("username", .string, .required)
.create()
}
func revert(on database: Database) -> EventLoopFuture<Void>{
database.schema("users").delete()
}
}
Finally, open configure.swift to add CreateUser to the migration list. Insert the following after app.migrations.add(CreateAcronym()):
app.migrations.add(CreateUser())
4.User controller UsersController.swift
import Fluent
import Vapor
struct UsersController: RouteCollection {
func boot(routes: RoutesBuilder) throws {
// 路由组
let usersRoute = routes.grouped("api", "users")
// 路由
usersRoute.post(use: createHandler)
usersRoute.get(use: getAllHandler)
usersRoute.get(":userID", use: getHandler)
}
// 创建用户
func createHandler(_ req: Request) throws -> EventLoopFuture<User> {
let user = try req.content.decode(User.self)
return user.save(on: req.db).map { user }
}
// 查询所有用户
func getAllHandler(_ req: Request) throws -> EventLoopFuture<[User]> {
User.query(on: req.db).all()
}
// 根据 ID 查询用户
// http://127.0.0.1:8080/api/users/<ID>
func getHandler(_ req: Request) throws -> EventLoopFuture<User> {
User.find(req.parameters.get("userID"), on: req.db)
.unwrap(or: Abort(.notFound))
}
}
open routes.swift and add the following to the end of routes(_:)
try app.register(collection: UsersController())
5.测试接口 以上文件的创建,与 Acronym 类似 标准三件套:
XXX.swift
CreateXXX.swift
XXXsController.swift
XXX 是具体的数据模型 CreateXXX 用于创建数据表 XXXsController 用于处理路由事件
a.创建用户 需要提交数据 url: http://127.0.0.1:8080/api/users method: POST parameters: {"name": "老五", "username":"王五"}
b.查询所有用户
url: http://127.0.0.1:8080/api/users method: GET parameters: 无
直接在浏览器访问也行的 返回数据 [{"name":"老三","id":"723A2733-DFD7-4193-9A81-4E85A708326B","username":"张三"},{"name":"老四","id":"E31B5335-EFD9-4D37-9AE2-4CD327D418BE","username":"李四"},{"name":"老五","id":"A9D70526-A793-432A-9E4A-7548F9EE3D19","username":"王五"}]
c.根据 ID 查询用户
url: http://127.0.0.1:8080/api/users/A9D70526-A793-432A-9E4A-7548F9EE3D19 method: GET parameters: 无
直接在浏览器访问 返回数据 {"id":"A9D70526-A793-432A-9E4A-7548F9EE3D19","username":"王五","name":"老五"}
0x01 创建关联关系
下篇正在准备中… 敬请期待~
0x02 我的作品
欢迎体验我的作品之一:小五笔 86 版 五笔学习好帮手! App Store 搜索即可~
|