fix sortby queries

This commit is contained in:
Ravi Prasad
2025-06-18 22:48:43 -05:00
parent 9475e1a245
commit 5775a42538
4 changed files with 72 additions and 17 deletions
+33 -8
View File
@@ -8,6 +8,15 @@ import (
"github.com/nprasad2077/NBA_Go/services"
)
var advancedSortMap = map[string]string{
"winShares": "win_shares",
"per": "per",
"tsPercent": "ts_percent",
"playerId": "player_id",
"season": "season",
"team": "team",
}
// AdvancedStatsResponse is the swagger response model for GetAllAdvancedPlayerStats
// It wraps the returned player advanced stats and pagination metadata.
type AdvancedStatsResponse struct {
@@ -43,6 +52,7 @@ type AdvancedStatsResponse struct {
// @Success 200 {object} map[string]string
// @Failure 400,500 {object} map[string]string
// @Router /api/playeradvancedstats/scrape [get]
// @ignore
func ScrapePlayerAdvancedStats(db *gorm.DB) fiber.Handler {
return func(c *fiber.Ctx) error {
season := c.QueryInt("season", 0)
@@ -80,23 +90,38 @@ func GetAllAdvancedPlayerStats(db *gorm.DB) fiber.Handler {
return func(c *fiber.Ctx) error {
var stats []models.PlayerAdvancedStat
// --- MODIFICATION FOR FILTERS ---
// Allow both "playerId" and "player_id"
playerId := c.Query("playerId")
if playerId == "" {
playerId = c.Query("player_id")
}
// Filters
season := c.QueryInt("season", 0)
team := c.Query("team")
playerId := c.Query("playerId")
// playerId := c.Query("playerId")
// Pagination
page := c.QueryInt("page", 1)
pageSize := c.QueryInt("pageSize", 20)
offset := (page - 1) * pageSize
// Sorting
sortBy := c.Query("sortBy", "win_shares")
ascending := c.QueryBool("ascending", false)
order := sortBy + " DESC"
if ascending {
order = sortBy + " ASC"
}
// --- MODIFICATION FOR SORTING ---
// Sorting
sortByParam := c.Query("sortBy", "winShares") // Default to a common field
ascending := c.QueryBool("ascending", false)
// Translate sortBy param to a valid DB column, defaulting if not found.
sortBy, ok := advancedSortMap[sortByParam]
if !ok {
sortBy = "win_shares" // Safe default
}
order := sortBy + " DESC"
if ascending {
order = sortBy + " ASC"
}
// Build query
query := db.Model(&models.PlayerAdvancedStat{})