Bonjour à tous,
je développe en ce moment un webservice Vapor, je reçoit un tableau en json contenant des éléments à modifier dans la db je le traite les modifies et les enregistres en db ça, ça marche seulement, je n’arrive pas à le faire de manière asynchrone car je souhaite retourner les éléments modifiés en json. Du coup j’ai été obligé de le faire de manière synchrone et si jamais il y a beaucoup d’éléments à modifier cela pourrait prendre pas mal de temps.
func modifyInstallations(req: Request) throws -> [Installation]{
try req.auth.require(User.self)
let token = try req.auth.require(UserToken.self)
userTokenController.refreshToken(req: req, token: token)
let database = req.db
let modifiedInstallationArray = try req.content.decode([Installation.Modify].self)
var installationsModified:[Installation] = []
if modifiedInstallationArray.count > 0 {
for installationUpdated in modifiedInstallationArray {
Installation.find(installationUpdated.id, on: database)
.unwrap(or: Abort(.notFound))
.map({ (installationNeedUpdate) in
let installationHasUpdated = updateInstallationForDb(from: installationUpdated, to: installationNeedUpdate)
installationsModified.append(installationHasUpdated)
installationHasUpdated.update(on: database)
})
}
return installationsModified
}
throw Abort(HTTPStatus.badRequest)
}
// if modifiedInstallationArray.count > 0 {
// return modifiedInstallationArray.compactMap { (updatedInstallation) -> EventLoopFuture<Installation> in
// return Installation.find(updatedInstallation.id, on: database)
// .unwrap(or: Abort(.notFound))
// .map { (installationNeedUpdate) in
// return updateInstallation(from: updatedInstallation, to: installationNeedUpdate)
// }
// }
// }
// throw Abort(HTTPStatus.badRequest)
// }
En gros j’avais essayé de le faire avec les lignes commenté mais cela me retourne un [EventLoopFuture] et en théorie cela pourrait marcher mais la route ne passe pas: "Referencing instance method ‹ post(_:use:) › on ‹ Array › requires that ‹ EventLoopFuture › conform to ‹ Content › "
Il faudrait que je retourne au pire un EventLoopFuture<[Installation]> mais je n’est pas trouvé comment faire. Merci d’avance de votre soutien