I am implementing a delete route handler using Vapor Fluent.
For this handler, I wanted to verify that the user sending the product deletion request is the owner of the product, and Abort the request otherwise.
func deleteHandler(_ req: Request) throws -> EventLoopFuture<HTTPStatus> {
let user = req.auth.get(User.self)
return Product.find(req.parameters.get("productID"), on: req.db)
.unwrap(or: Abort(.notFound))
.flatMap { product in
return product.$user.get(on: req.db).flatMapThrowing { owner in
guard try user?.requireID() == owner.requireID() else {
throw Abort(.forbidden)
}
return try product.delete(on: req.db)
.transform(to: HTTPStatus.noContent) // error here
}
}
}
But Vapor throws an error at return try product.delete(on: req.db).transform(to: HTTPStatus.noContent) saying Cannot convert return expression of type 'EventLoopFuture<HTTPResponseStatus>' to return type 'HTTPStatus' (aka 'HTTPResponseStatus').
I tried chaining again using map({}), that did not help. Using wait()solves the error but introduces a runtime bug.
Thanks for any help!