From 87726f6b99e62a7cb752fae78fee4928a395d373 Mon Sep 17 00:00:00 2001 From: Michael Simard Date: Mon, 21 Dec 2020 10:26:32 -0600 Subject: [PATCH] added post method for updating match --- Sources/App/Controllers/MatchController.swift | 22 +++++++++++++ Sources/App/Controllers/StatsController.swift | 32 ++----------------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/Sources/App/Controllers/MatchController.swift b/Sources/App/Controllers/MatchController.swift index e68b352..60b21ce 100644 --- a/Sources/App/Controllers/MatchController.swift +++ b/Sources/App/Controllers/MatchController.swift @@ -15,6 +15,20 @@ struct MatchController: RouteCollection { func boot(routes: RoutesBuilder) throws { let matchRoute = routes.grouped("cod-tracker","api", "match") matchRoute.delete("delete", "id",":id", use: deleteMatch) + matchRoute.post("update", "id",":id", use: updateMatch) + matchRoute.post("add", use: logMatch) + + } + + + func updateMatch(req: Request) throws -> EventLoopFuture { + guard let id = req.parameters.get("id", as: UUID.self) else { + throw Abort(.badRequest) + } + return Match.find(id, on: req.db) + .unwrap(or: Abort(.notFound)) + .flatMap { $0.update(on: req.db) } + .map { .ok } } func deleteMatch(req: Request) throws -> EventLoopFuture { @@ -27,4 +41,12 @@ struct MatchController: RouteCollection { .map { .ok } } + + func logMatch(req: Request) throws -> EventLoopFuture { + + let newMatch = try req.content.decode(Match.self) + return newMatch.save(on: req.db).map { newMatch} + } + + } diff --git a/Sources/App/Controllers/StatsController.swift b/Sources/App/Controllers/StatsController.swift index 3397583..7e07a62 100644 --- a/Sources/App/Controllers/StatsController.swift +++ b/Sources/App/Controllers/StatsController.swift @@ -26,6 +26,8 @@ struct StatsController: RouteCollection { statsRoute.get("history","page",":page", use: history) statsRoute.get("history", use: history) + + } @@ -47,43 +49,15 @@ struct StatsController: RouteCollection { } else { - - - return Match.query(on: req.db).count().flatMap { (totalMatches) -> EventLoopFuture in - - return Match.query(on: req.db).sort(\.$date, .descending).limit(20).all().map { (matches) -> (MatchHistory) in - return MatchHistory(total:totalMatches, matches: matches, hasMorePages: totalMatches > 20) } } } - - - - - -// let startRecord = page * 20 -// -// -// -// -// if totalRecords.map -// -// -// return Match.query(on: req.db).sort(\.$date, .descending).range(startRecord...).all().map { (matches) -> ([Match]) in -// return matches -// } -// } -// else { -// return Match.query(on: req.db).sort(\.$date, .descending).limit(20).all().map { (matches) -> ([Match]) in -// return matches -// } -// } - + }