I have developed an API with Vapor/Fluent based on EventLoopFuture that works very well. Now I want to change the API to Async/Await. Here I have a problem when creating a new record in the auxiliary table of a many to many relationship.
Here is my example with EventLoopFuture:
// POST localhost:8080/information_templates/:inf_id/container_templates/:cont_id
func createCombine(req: Request) throws -> EventLoopFuture<HTTPStatus> {
let iTemplate = FMInformationTemplate.find(req.parameters.get("inf_id"), on: req.db)
.unwrap(or: Abort(.notFound))
let cTemplate = FMContainerTemplate.find(req.parameters.get("cont_id"), on: req.db)
.unwrap(or: Abort(.notFound))
return cTemplate.and(iTemplate).flatMap { (cTemplate, iTemplate) in
cTemplate.$informations.attach(iTemplate, on: req.db)
}.transform(to: .ok)
}
And now the new Function with Async/Await:
// POST localhost:8080/information_templates/:inf_id/container_templates/:cont_id
func createCombine(req: Request) async throws -> HTTPStatus {
guard let iTemplate = try await FMInformationTemplate.find(req.parameters.get("inf_id"), on: req.db) else {
throw Abort(.notFound)
}
guard let cTemplate = try await FMContainerTemplate.find(req.parameters.get("cont_id"), on: req.db) else {
throw Abort(.notFound)
}
// ??? I can't get any further here 😰
}
If someone could help me with this, I would be happy