这是适用于我的 API 的调用,但我需要在叶子中呈现结果。这目前适用于 API。只是不知道我在哪里抓住球队。
func getTeamsWithUsersForEvent(_ req: Request) throws -> Future<[TeamWithUsers]> {
let currentID = try req.parameters.next(Int.self)
print("currentID \(currentID)")
return Team.query(on: req).filter(\Team.eventID == currentID).sort(\Team.rawScore, .descending).range(..<5).all().flatMap(to: [TeamWithUsers].self) { team in
try team.map { team in
try team.users.query(on: req).all().map { users in
var score: Int
if users.isEmpty {
score = 0
}else{
score = team.rawScore / users.count
}
return TeamWithUsers(
id: team.id,
name: team.name,
icon: team.icon,
eventID: team.eventID,
rawScore: team.rawScore,
//users: users,
count: users.count,
teamScore: score
// teamScore: team.rawScore / users.count
)
}
}.flatten(on: req)
}
}
struct TeamWithUsers: Content {
let id: Int?
let name: String
let icon: String
let eventID: Event.ID
let rawScore: Int
//let users: [User]
let count: Int?
let teamScore: Int
}
这是我已经开始的。这是我的背景。
struct AllTeamsContext: Encodable {
let title: String
let teams: [TeamWithUsers]
}
我创建了名为“Leaders”的 Leaf 模板,它只显示了一个每行包含 TeamWithUsers 的表格。
#if(count(teams) > 0) {
<table class="table table-bordered table-hover">
<thead class="thead-light">
<tr>
<th>Name</th>
<th>Icon</th>
<th>Score</th>
</tr>
</thead>
<tbody>
#for(team in teams) {
<tr>
<td>#(team.name)</td>
<td>#(team.icon)</td>
<td>#(team.teamScore)</td>
</tr>
}
</tbody>
</table>
} else {
<h2>There aren’t any teams yet.</h2>
}