57 lines
1.3 KiB
Swift
57 lines
1.3 KiB
Swift
//
|
|
// DBHelpers.swift
|
|
// App
|
|
//
|
|
// Created by Michael Simard on 8/4/21.
|
|
//
|
|
|
|
import Fluent
|
|
import Vapor
|
|
|
|
class DBHelpers {
|
|
|
|
static func relcalulateRecords(db:Database, completion:(_ message:String)->()){
|
|
|
|
let group = DispatchGroup()
|
|
_ = try? StatsController().forceCalculatedStats(db: db).map({ statsDict in
|
|
|
|
|
|
|
|
for key in statsDict.keys {
|
|
print (key)
|
|
group.enter()
|
|
}
|
|
|
|
for key in statsDict.keys {
|
|
|
|
let s:EventLoopFuture<Void> = WinLossRecords.query(on: db).filter(\.$codTrackerId == key).first()
|
|
.unwrap(or: Abort(.notFound))
|
|
.flatMap {
|
|
|
|
$0.update(statistics: WinLossRecords(id: $0.id, title: $0.title, wins: statsDict[key]?.totalWins ?? 0, losses: statsDict[key]?.totalLosses ?? 0, codTrackerId: $0.codTrackerId))
|
|
|
|
return $0.save(on: db)
|
|
}
|
|
|
|
|
|
s.whenComplete { result in
|
|
group.leave()
|
|
return
|
|
}
|
|
|
|
|
|
}
|
|
})
|
|
.wait()
|
|
|
|
|
|
group.wait()
|
|
|
|
completion("complete")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|