added new history paging functionality

This commit is contained in:
Michael Simard
2020-12-16 05:54:18 -06:00
parent a674a62dce
commit a7969813ae
2 changed files with 80 additions and 14 deletions

View File

@@ -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 deleteMatch(req: Request) throws -> EventLoopFuture<HTTPStatus> {
guard let id = req.parameters.get("id", as: UUID.self) else {
throw Abort(.badRequest)
func history(req: Request) throws -> EventLoopFuture<MatchHistory> {
if let page = req.parameters.get("page", as: Int.self) {
return Match.query(on: req.db).count().flatMap { (totalMatches) -> EventLoopFuture<MatchHistory> in
let startRecord = min (page * 20 + 20, totalMatches)
let lastRecord = min (startRecord + 20, totalMatches)
return Match.query(on: req.db).sort(\.$date, .descending).range(startRecord..<lastRecord).all().map { (matches) -> (MatchHistory) in
return MatchHistory(total:totalMatches, matches: matches, hasMorePages: lastRecord < totalMatches)
}
}
}
else {
return Match.query(on: req.db).count().flatMap { (totalMatches) -> EventLoopFuture<MatchHistory> 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)
}
}
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
}
// 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
// }
// }
}

View File

@@ -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
}
}