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 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)
|
||||
|
||||
func deleteMatch(req: Request) throws -> EventLoopFuture<HTTPStatus> {
|
||||
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..<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 }
|
||||
}
|
||||
|
||||
|
||||
|
||||
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<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
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user