Add endpoint for fetching my-team/players

This commit is contained in:
Garrett Mills 2020-11-04 23:09:36 -06:00
parent 19567dcfb0
commit 33c37f6855
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246
4 changed files with 23 additions and 5 deletions

View File

@ -21,6 +21,13 @@ class Teams extends Controller {
return res.api(await req.user_team.to_api()) return res.api(await req.user_team.to_api())
} }
async get_my_team_players(req, res, next) {
const players = await req.user_team.players()
console.log(players)
console.log(await players[0].to_api())
return res.api(await Promise.all(players.map(x => x.to_api())))
}
async list_all_teams(req, res) { async list_all_teams(req, res) {
const TeamModel = this.models.get('Team') const TeamModel = this.models.get('Team')
const teams = await TeamModel.find() const teams = await TeamModel.find()

View File

@ -67,11 +67,12 @@ class Player extends Model {
async to_api() { async to_api() {
return { return {
player_number: this.player_number, number: this.player_number,
player_name: this.player_name, name: this.full_name,
player_position: this.player_position, position: this.fantasy_position,
team_name: this.team_name, team_name: this.patch_data.patch_team_name,
image_url: this.image_url, image: this.photo_url,
stats: {}, // TODO - populate some stats!
} }
} }
} }

View File

@ -50,6 +50,15 @@ class Team extends Model {
return new_team return new_team
} }
async players() {
const Player = this.models.get('Player')
return Player.find({
_id: {
$in: this.player_ids.map(x => Player.to_object_id(x))
}
})
}
async to_api() { async to_api() {
const User = this.models.get('auth:User') const User = this.models.get('auth:User')

View File

@ -48,6 +48,7 @@ const index = {
], ],
'/my-team': ['controller::Teams.get_my_team'], '/my-team': ['controller::Teams.get_my_team'],
'/my-team/players': ['controller::Teams.get_my_team_players'],
}, },
/* /*