1

这是适用于我的 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>
}
4

1 回答 1

1

编辑:根据评论中的附加信息更改答案。

虽然您可以更改现有函数以直接返回视图,但我个人会保持原样并从呈现视图的第二秒调用它。尝试将它与您当前的功能放在同一个路由器中:

func teamsView(_ req: Request) throws -> Future<View> {
   let currentID =  try req.parameters.next(Int.self)
   let teamsContext = getTeamsWithUsersForEvent(request, currentID)
   return request.view().render("Leaders", teamsContext )
}

然后,在你的routes.swift或同等的地方,输入:

router.grouped("admin","teams") teamsRoute.get("leaders", Int.parameter, use: teamsView)

将参数移动到tsamsview上面的函数中,然后您需要将原始函数头修改为:

func getTeamsWithUsersForEvent(_ req: Request, _ currentID:Int) throws -> Future<[TeamWithUsers]>

然后从此函数中删除原始参数提取。

当您的路由器被分组时,您的视图应该在以下位置可见:

http://localhost:8080/admin/teams/nnn/leaders

nnn将被翻译成的内容在哪里currentID

或者:

如果这变得有点混乱(尽管我第一次评论更喜欢将功能分开),您总是可以将现有功能更改为:

func getTeamsWithUsersForEvent(_ req: Request) throws -> Future<View> {
    let currentID =  try req.parameters.next(Int.self)
    print("currentID \(currentID)")
    let teamContext = 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
                )
            }
        }.flatten(on: req)
    }
    return request.view().render("Leaders", teamContext )
}
于 2019-12-29T15:03:55.543 回答