I'm trying to do a nested query in Vapor 3 and Fluent. The point is I need to get all the users, from each team where the teams have a specific eventID. Teams are children of Event. Users are children of Teams. Thanks in advance for your help. There are only 15 teams per event, but 12 users per team
Here is the Event model:
final class Event: Codable {
var id: Int?
var name: String
}
extension Event {
var teams: Children<Event, Team> {
return children(\.eventID)
}
}
Here is the Team model
final class Team: Codable {
var id: Int?
var name: String
var icon: String
var eventID: Event.ID
}
extension Team {
var user: Parent<Team, Event> {
return parent(\.eventID)
}
}
extension Team {
var users: Children<Team, User> {
return children(\.teamID)
}
}
Here is the User model.
final class User: Codable {
var id: UUID?
var name: String
var email: String
var eventID: Event.ID
var teamID: Team.ID
}
extension User {
var user: Parent<User, Team> {
return parent(\.teamID)
}
}
I need to send an event ID and I want it to return all the users in all the teams
func getUsersForEvent(_ req: Request) throws -> Future<[User]> {
return try req.parameters.next(Event.self).flatMap(to: [User].self) { event in
return try event.teams.query(on: req).all().flatMap(to: [User].self) { team in
return try team.users.query(on: req).all()
}
}
}