In chapter 9, after we’ve added a parent-child relationship from User to Acronym, we have to revisit our previous update(_ method to include the newly-added UserID property, like so:
func updateHandler(_ req: Request) throws -> Future<Acronym> {
return try flatMap(to: Acronym.self,
req.parameter(Acronym.self),
req.content.decode(Acronym.self)) {
acronym, updatedAcronym in
acronym.short = updatedAcronym.short
acronym.long = updatedAcronym.long
acronym.userID = updatedAcronym.userID
return acronym.save(on: req)
}
}
I found myself instead using an approach that seemed natural to me:
func updateHandler(_ req: Request) throws -> Future<Acronym> {
return try flatMap(to: Acronym.self,
req.parameter(Acronym.self),
req.content.decode(Acronym.self)) {
existingAcronym, updatedAcronym in
updatedAcronym.id = existingAcronym.id
return updatedAcronym.update(on: req)
}
}
Given how much less repeating ourselves there is in Vapor 3, I find myself really wanting to avoid any kind of repetition, and this approach appeals to me in that I don’t have to touch my controller methods as I modify my model. But I’m wondering… Is there anything less performant or error-prone about my approach that I’m perhaps missing?