support for update

This commit is contained in:
Michael Simard
2021-01-09 09:56:27 -06:00
parent ae66dfd1d6
commit 523b480376
3 changed files with 52 additions and 2 deletions

View File

@@ -25,10 +25,23 @@ struct MatchController: RouteCollection {
guard let id = req.parameters.get("id", as: UUID.self) else {
throw Abort(.badRequest)
}
let newMatch = try req.content.decode(Match.self)
return Match.find(id, on: req.db)
.unwrap(or: Abort(.notFound))
.flatMap { $0.update(on: req.db) }
.flatMap {
$0.update(newMatch: newMatch)
return $0.save(on: req.db)
}
.map { .ok }
}
func deleteMatch(req: Request) throws -> EventLoopFuture<HTTPStatus> {

View File

@@ -46,6 +46,22 @@ final class Match: Model, Content {
self.players = players
}
func update( newMatch:Match) {
guard let _ = newMatch.id else {
return
}
self.map = newMatch.map
self.win = newMatch.win
self.date = newMatch.date
self.roundsWon = newMatch.roundsWon
self.roundsLost = newMatch.roundsLost
self.codGame = newMatch.codGame;
self.notes = newMatch.notes;
self.players = newMatch.players
}
}
extension Match {

View File

@@ -35,12 +35,33 @@ public func configure(_ app: Application) throws {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
decoder.dateDecodingStrategy = .formatted(formatter)
let encoder = JSONEncoder()
let encodeFormatter = DateFormatter()
encodeFormatter.calendar = Calendar(identifier: .iso8601)
encodeFormatter.locale = Locale(identifier: "en_US_POSIX")
let date = Date()
let tz = TimeZone.current
// if tz.isDaylightSavingTime(for: date) {
// encodeFormatter.timeZone = TimeZone(abbreviation: "EST")
// }
// else{
// encodeFormatter.timeZone = TimeZone(abbreviation: "EDT")
//
// }
encodeFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
encoder.dateEncodingStrategy = .formatted(encodeFormatter)
// override the global encoder used for the `.json` media type
ContentConfiguration.global.use(decoder: decoder, for: .json)
// ContentConfiguration.global.use(encoder: encoder, for: .json)
// register routes
try routes(app)