From a7969813ae18a48e6cb31e5b2f12cbafb8b974c5 Mon Sep 17 00:00:00 2001 From: Michael Simard Date: Wed, 16 Dec 2020 05:54:18 -0600 Subject: [PATCH] added new history paging functionality --- Sources/App/Controllers/StatsController.swift | 71 +++++++++++++++---- Sources/App/Models/MatchHistory.swift | 23 ++++++ 2 files changed, 80 insertions(+), 14 deletions(-) create mode 100644 Sources/App/Models/MatchHistory.swift diff --git a/Sources/App/Controllers/StatsController.swift b/Sources/App/Controllers/StatsController.swift index d21ac3b..6aae798 100644 --- a/Sources/App/Controllers/StatsController.swift +++ b/Sources/App/Controllers/StatsController.swift @@ -24,28 +24,71 @@ struct StatsController: RouteCollection { statsRoute.post("logMatch", use: logMatch) + statsRoute.get("history","page",":page", use: history) statsRoute.get("history", use: history) } + + + func history(req: Request) throws -> EventLoopFuture { + + if let page = req.parameters.get("page", as: Int.self) { + + return Match.query(on: req.db).count().flatMap { (totalMatches) -> EventLoopFuture in + + let startRecord = min (page * 20 + 20, totalMatches) + let lastRecord = min (startRecord + 20, totalMatches) - func deleteMatch(req: Request) throws -> EventLoopFuture { - guard let id = req.parameters.get("id", as: UUID.self) else { - throw Abort(.badRequest) + + return Match.query(on: req.db).sort(\.$date, .descending).range(startRecord.. (MatchHistory) in + + return MatchHistory(total:totalMatches, matches: matches, hasMorePages: lastRecord < totalMatches) + + } + + } } - return Match.find(id, on: req.db) - .unwrap(or: Abort(.notFound)) - .flatMap { $0.delete(on: req.db) } - .map { .ok } - } - - - - func history(req: Request) throws -> EventLoopFuture<[Match]> { - return Match.query(on: req.db).sort(\.$date, .descending).limit(20).all().map { (matches) -> ([Match]) in - return matches + 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 +// } +// } + } diff --git a/Sources/App/Models/MatchHistory.swift b/Sources/App/Models/MatchHistory.swift new file mode 100644 index 0000000..d9f8ba8 --- /dev/null +++ b/Sources/App/Models/MatchHistory.swift @@ -0,0 +1,23 @@ +// +// MatchHistory.swift +// App +// +// Created by Michael Simard on 12/15/20. +// + +import Foundation +import Fluent +import Vapor + +final class MatchHistory: Content { + var total:Int + var matches:[Match] + var hasMorePages:Bool + + init( total:Int, matches:[Match],hasMorePages:Bool) { + self.total = total; + self.matches = matches + self.hasMorePages = hasMorePages + } + +}