diff --git a/app/routers_v3/stratplay.py b/app/routers_v3/stratplay.py index 11eb2cd..fadcf6f 100644 --- a/app/routers_v3/stratplay.py +++ b/app/routers_v3/stratplay.py @@ -795,6 +795,35 @@ async def get_pitching_totals( # Group by the fields if pitch_select_fields: pitch_plays = pitch_plays.group_by(*pitch_select_fields) + + # Apply sorting + if sort is not None: + if sort == 'player': + pitch_plays = pitch_plays.order_by(StratPlay.pitcher) + elif sort == 'team': + pitch_plays = pitch_plays.order_by(StratPlay.pitcher_team) + elif sort == 'wpa-desc': + pitch_plays = pitch_plays.order_by(SQL('sum_wpa').desc()) + elif sort == 'wpa-asc': + pitch_plays = pitch_plays.order_by(SQL('sum_wpa').asc()) + elif sort == 'repri-desc': + pitch_plays = pitch_plays.order_by(SQL('sum_repri').desc()) + elif sort == 'repri-asc': + pitch_plays = pitch_plays.order_by(SQL('sum_repri').asc()) + elif sort == 'pa-desc': + pitch_plays = pitch_plays.order_by(SQL('sum_pa').desc()) + elif sort == 'pa-asc': + pitch_plays = pitch_plays.order_by(SQL('sum_pa').asc()) + elif sort == 'newest': + if group_by in ['playergame', 'teamgame']: + pitch_plays = pitch_plays.order_by(StratPlay.game.desc()) + elif sort == 'oldest': + if group_by in ['playergame', 'teamgame']: + pitch_plays = pitch_plays.order_by(StratPlay.game.asc()) + + if limit < 1: + limit = 1 + pitch_plays = pitch_plays.paginate(page_num, limit) # Execute the Peewee query return_stats = {