如何使用 child 保存模型并使用 child 返回保存的模型作为 Vapor 3 中的响应
How to save a model with a child and get the saved model with child back as a response in Vapor 3
我正在尝试编写一个控制器函数来同时插入一个带有 child 模型的模型,并在 return 中获得相同的 JSON 响应,但填充了从数据库添加的数据(即自动生成的id)。
对于下面的简化示例,JSON 请求正文如下所示:
{
"field1": "blabla",
"subContent": {
"field2": "lalala"
}
}
我期待以下回复:
{
"id": 5,
"field1": "blabla",
"subContent": {
"id": 44,
"field2": "lalala"
}
}
不幸的是,下面的代码无法编译,错误消息显示“无法将类型 'EventLoopFuture' 的表达式转换为 return 类型 'Response'。这发生在行
return subModel.create(on: req).flatMap { savedSubModel ->
代码如下:
struct MainContent: Content {
var id: UUID?
var field1: String
var subContent: SubContent
}
struct SubContent: Content {
var id: UUID?
var field2: String
}
final class Controller {
func create(_ req: Request) throws -> EventLoopFuture<Response> {
return try req.content.decode(MainContent.self).map { request in
let subModel = SubModel(
field2: request.subContent.field2
)
return subModel.create(on: req).flatMap { savedSubModel -> EventLoopFuture<Response> in
let mainModel = mainModel(
subModelId: savedSubModel.id!,
field1: request.field1
)
return mainModel.create(on: req).flatMap { savedMainModel -> EventLoopFuture<Response> in
let content = MainContent(
id: savedMainModel.id,
field1: savedMainModel.field1,
subContent: SubContent(
id: savedSubContent.id,
field2: savedSubContent.field2
)
)
return content.encode(status: .created, for: req)
}
}
}
}
}
struct MainContent: Content {
var id: UUID?
var field1: String
var subContent: SubContent
}
struct SubContent: Content {
var id: UUID?
var field2: String
}
final class Controller {
func create(_ req: Request) throws -> EventLoopFuture<Response> {
return try req.content.decode(MainContent.self).flatMap { request in
let subModel = SubModel(
field2: payload.subContent.field2
)
return subModel.create(on: req)
}.flatMap { savedSubModel in
let mainModel = mainModel(
subModelId: try savedSubModel.requireID(),
field1: request.field1
)
return mainModel.create(on: req).flatMap { savedMainModel in
let content = MainContent(
id: try savedMainModel.requireID(),
field1: savedMainModel.field1,
subContent: SubContent(
id: try savedSubModel.requireID(),
field2: savedSubModel.field2
)
)
return content.encode(status: .created, for: req)
}
}
}
}
我正在尝试编写一个控制器函数来同时插入一个带有 child 模型的模型,并在 return 中获得相同的 JSON 响应,但填充了从数据库添加的数据(即自动生成的id)。
对于下面的简化示例,JSON 请求正文如下所示:
{
"field1": "blabla",
"subContent": {
"field2": "lalala"
}
}
我期待以下回复:
{
"id": 5,
"field1": "blabla",
"subContent": {
"id": 44,
"field2": "lalala"
}
}
不幸的是,下面的代码无法编译,错误消息显示“无法将类型 'EventLoopFuture' 的表达式转换为 return 类型 'Response'。这发生在行
return subModel.create(on: req).flatMap { savedSubModel ->
代码如下:
struct MainContent: Content {
var id: UUID?
var field1: String
var subContent: SubContent
}
struct SubContent: Content {
var id: UUID?
var field2: String
}
final class Controller {
func create(_ req: Request) throws -> EventLoopFuture<Response> {
return try req.content.decode(MainContent.self).map { request in
let subModel = SubModel(
field2: request.subContent.field2
)
return subModel.create(on: req).flatMap { savedSubModel -> EventLoopFuture<Response> in
let mainModel = mainModel(
subModelId: savedSubModel.id!,
field1: request.field1
)
return mainModel.create(on: req).flatMap { savedMainModel -> EventLoopFuture<Response> in
let content = MainContent(
id: savedMainModel.id,
field1: savedMainModel.field1,
subContent: SubContent(
id: savedSubContent.id,
field2: savedSubContent.field2
)
)
return content.encode(status: .created, for: req)
}
}
}
}
}
struct MainContent: Content {
var id: UUID?
var field1: String
var subContent: SubContent
}
struct SubContent: Content {
var id: UUID?
var field2: String
}
final class Controller {
func create(_ req: Request) throws -> EventLoopFuture<Response> {
return try req.content.decode(MainContent.self).flatMap { request in
let subModel = SubModel(
field2: payload.subContent.field2
)
return subModel.create(on: req)
}.flatMap { savedSubModel in
let mainModel = mainModel(
subModelId: try savedSubModel.requireID(),
field1: request.field1
)
return mainModel.create(on: req).flatMap { savedMainModel in
let content = MainContent(
id: try savedMainModel.requireID(),
field1: savedMainModel.field1,
subContent: SubContent(
id: try savedSubModel.requireID(),
field2: savedSubModel.field2
)
)
return content.encode(status: .created, for: req)
}
}
}
}