added new history paging functionality
This commit is contained in:
@@ -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)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return Match.find(id, on: req.db)
|
||||
.unwrap(or: Abort(.notFound))
|
||||
.flatMap { $0.delete(on: req.db) }
|
||||
.map { .ok }
|
||||
}
|
||||
else {
|
||||
|
||||
|
||||
|
||||
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
|
||||
|
||||
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)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 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
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
23
Sources/App/Models/MatchHistory.swift
Normal file
23
Sources/App/Models/MatchHistory.swift
Normal 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
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user