How can I update an array of models into mysql
using transactions
? and return Ok
response after successful operation
So, I solved my problem without transactions:
func update(_ req: Request, todos: [CreateTodoRequest]) throws -> Future<HTTPStatus> {
let user = try req.requireAuthenticated(User.self)
// 1
return try todos.map { try Todo(todo: $0, userID: user.requireID()).create(on: req) }
// 2
.flatten(on: req)
// 3
.transform(to: .ok)
}
.create(on: req)
returns an EventLoopFuture<Todo>
. So todos.map
will return an array of [EventLoopFuture<Todo>]
. The second step is to transform it to EventLoopFuture<[Todo]>
. And the third step is to transform it to HttpStatus.ok
response
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With