// ListTeamsForSeason is a http handler that returns a brief summary // of all of the NBA games for the given season. func (s *Server) ListGamesForSeason(ctx context.Context, rw http.ResponseWriter, req *http.Request, ps parameterSet) (interface{}, error) { var season data.Season err := season.UnmarshalText([]byte(ps["season"])) if err != nil { return nil, ErrBadRequest("Invalid season identifier") } const q = ` SELECT games.id, home_team_id, visitor_team_id, status, time, length_minutes FROM games WHERE season = ? ORDER BY time DESC, status ASC, length_minutes ASC ` var games []*gameSummary err = s.db.DB.Select(&games, q, season.String()) if err != nil { return nil, err } for _, g := range games { g.HomeTeam = teamSummaryFromData(s.teams.ByID(g.HomeTeamID)) g.VisitorTeam = teamSummaryFromData(s.teams.ByID(g.VisitorTeamID)) } return games, nil }
// ListPlayersForSeason is a http handler that returns a list of all the // players active during the given season. func (s *Server) RetrievePlayerForSeason(ctx context.Context, rw http.ResponseWriter, req *http.Request, ps parameterSet) (interface{}, error) { var ( season data.Season playerID int ) err := season.UnmarshalText([]byte(ps["season"])) if err != nil { return nil, ErrBadRequest("Invalid season identifier") } playerID, err = ps.Int("playerID") if err != nil { return nil, ErrBadRequest("Bad player identifier") } var player data.PlayerDetails err = s.db.DB.Get(&player, playerID) if err == sql.ErrNoRows { return nil, ErrNotFound("Player not found") } if err != nil { return nil, err } resp := struct { Player data.PlayerDetails `json:"player"` GamesPlayed []*playerStatLine `json:"games_played"` }{ Player: player, GamesPlayed: []*playerStatLine{}, } // Pull in their games too. const q = ` SELECT player_game_stats.team_id, player_game_stats.game_id, stats.*, games.home_team_id, games.visitor_team_id, games.time FROM player_game_stats INNER JOIN stats ON player_game_stats.stats_id = stats.id INNER JOIN games ON player_game_stats.game_id = games.id WHERE games.season = ? AND player_game_stats.player_id = ? ORDER BY games.time DESC ` err = s.db.DB.Select(&resp.GamesPlayed, q, season.String(), playerID) if err != nil { return nil, err } for _, gp := range resp.GamesPlayed { if gp.TeamID == gp.HomeTeamID { gp.HomeOrAway = data.Home } else { gp.HomeOrAway = data.Away } // Calculate percentages, and other derived statistics that aren't // actually stored in the nbagame db because they're redundant. gp.Stats.Calculate() } return resp, nil }