added some initial routes, ways to show overall stats, matches

This commit is contained in:
Michael Simard
2020-05-29 14:52:20 -05:00
parent c6f7dcec32
commit f48ddd77bf
9 changed files with 277 additions and 55 deletions

View File

@@ -0,0 +1,117 @@
////
//// StatsAPIController.swift
//// App
////
//// Created by Michael Simard on 5/29/20.
////
//
//import Fluent
//import Vapor
//
//
//
//
//
//protocol ContentController {
// associatedtype Model: Fluent.Model
//}
//
//protocol StatsContentRepresentable {
// associatedtype StatsItem: Content
// var statsContent : StatsItem { get }
//}
//
//
//protocol StatsContentController: ContentController where Model: StatsContentRepresentable {
//
// func stats (_:Request) throws -> EventLoopFuture<Page<Model.StatsItem>>
// func setupStatsRoute( routes: RoutesBuilder)
//}
//
//extension StatsContentController {
//
//
// func stats (req:Request) throws -> EventLoopFuture<Page<Model.StatsItem>> {
// return Model.query(on: req.db).paginate(for: req).map { $0.map(\.statsContent)}
//
// }
//
// func setupStatsRoute( routes: RoutesBuilder)
// {
// routes.get(use: self.stats)
// }
//
//}
//
//
//
////***********
//
//final class StatsModel: Model, Content {
// typealias IDValue = <#type#>
//
// static var schema: String
//
// init() {
// <#code#>
// }
//
// var winLoss:String
// var totalWins:Int
// var totalLosses:Int
// init( winLoss:String, totalWins:Int, totalLosses:Int) {
//
// self.winLoss = winLoss
// self.totalWins = totalWins
// self.totalLosses = totalLosses
// }
//}
//
//extension StatsModel: StatsContentRepresentable {
//
// struct StatsItem: Content {
//
// var winLoss:String
// var totalWins:Int
// var totalLosses:Int
//
// init(model:StatsModel) {
//
// self.winLoss = model.winLoss
// self.totalWins = model.totalWins
// self.totalLosses = model.totalLosses
// }
// }
//
// var statsContent: StatsItem { .init(model:self) }
//}
//
//
////***********
//
//
//struct StatsAPIController: StatsContentController {
//
// typealias Model = StatsModel
//
//
//}
//
//struct StatsRouter: RouteCollection {
//
// func boot(routes: RoutesBuilder) throws {
// let statsRoute = routes.grouped("api", "stats")
//
// StatsAPIController().setupStatsRoute(routes: statsRoute)
//
//// statsRoute.get("allMatches", use: index)
//// statsRoute.get("totalWins", use: totalWins)
//// statsRoute.get("totalLosses", use: totalLosses)
// //statsRoute.get("overall", use: totalLosses)
//
// //beaconsRoute.get("overallStats", use: index)
// //beaconsRoute.post("", use: create)
// // beaconsRoute.delete(":beaconID", use: delete)
//
// }
//}

View File

@@ -0,0 +1,86 @@
import Fluent
import Vapor
struct StatsController: RouteCollection {
func boot(routes: RoutesBuilder) throws {
let statsRoute = routes.grouped("api", "stats")
statsRoute.get("allMatches", use: index)
statsRoute.get("totalWins", use: totalWins)
statsRoute.get("totalLosses", use: totalLosses)
statsRoute.get("overall", use: overall)
//beaconsRoute.get("overallStats", use: index)
//beaconsRoute.post("", use: create)
// beaconsRoute.delete(":beaconID", use: delete)
}
func index(req: Request) throws -> EventLoopFuture<[Match]> {
return Match.query(on: req.db).all()
}
func totalWins(req: Request) throws -> EventLoopFuture<Int> {
return Match.query(on: req.db)
.filter(\.$win == true)
.count()
}
func totalLosses(req: Request) throws -> EventLoopFuture<Int> {
return Match.query(on: req.db)
.filter(\.$win == false)
.count()
}
func overall(req: Request) throws -> EventLoopFuture<Stats> {
let lossCount = Match.query(on: req.db)
.filter(\.$win == false)
.count()
let winCount = Match.query(on: req.db)
.filter(\.$win == true )
.count()
let combined = winCount.and(lossCount)
return combined.map { (winCount, lossCount) -> (Stats) in
let ratio:Double = (Double(winCount) / Double(lossCount)).truncate(places: 2)
return Stats.init(winLoss: String(ratio), totalWins: winCount, totalLosses: lossCount)
}
}
//
// func create(req: Request) throws -> EventLoopFuture<Beacon> {
// let newBeacon = try req.content.decode(Beacon.Create.self)
// let beacon = Beacon(id: UUID(), ownerId: nil)
// return beacon.save(on: req.db).map { beacon }
// }
//
// func delete(req: Request) throws -> EventLoopFuture<HTTPStatus> {
// return Beacon.find(req.parameters.get("beaconID"), on: req.db)
// .unwrap(or: Abort(.notFound))
// .flatMap { $0.delete(on: req.db) }
// .transform(to: .ok)
// }
}
extension Double
{
func truncate(places : Int)-> Double
{
return Double(floor(pow(10.0, Double(places)) * self)/pow(10.0, Double(places)))
}
}

View File

@@ -1,29 +0,0 @@
import Fluent
import Vapor
struct TodoController: RouteCollection {
func boot(routes: RoutesBuilder) throws {
let todos = routes.grouped("match")
todos.get(use: index)
todos.post(use: create)
todos.group(":matchID") { todo in
todo.delete(use: delete)
}
}
func index(req: Request) throws -> EventLoopFuture<[Todo]> {
return Todo.query(on: req.db).all()
}
func create(req: Request) throws -> EventLoopFuture<Todo> {
let todo = try req.content.decode(Todo.self)
return todo.save(on: req.db).map { todo }
}
func delete(req: Request) throws -> EventLoopFuture<HTTPStatus> {
return Todo.find(req.parameters.get("matchID"), on: req.db)
.unwrap(or: Abort(.notFound))
.flatMap { $0.delete(on: req.db) }
.transform(to: .ok)
}
}