totals controller update in dev

This commit is contained in:
Ravi Prasad
2025-08-06 02:39:14 -05:00
parent 14ea92f88c
commit 3cfdcdbc4d
5 changed files with 481 additions and 85 deletions
+2 -2
View File
@@ -4,8 +4,8 @@ FROM golang:1.24-bullseye AS builder
ENV CGO_ENABLED=1 ENV CGO_ENABLED=1
ENV GOOS=linux ENV GOOS=linux
# Changed from Apple Silicon arm64 config. # Changed from Apple Silicon arm64 config.
ENV GOARCH=amd64 # ENV GOARCH=amd64
# ENV GOARCH=arm64 ENV GOARCH=arm64
WORKDIR /app WORKDIR /app
+127 -61
View File
@@ -1,15 +1,3 @@
// FetchPlayerTotalStats godoc
// @Summary Fetch player total stats from external API
// @Description Imports totals data and stores or updates in DB
// @Tags PlayerTotals
// @Accept json
// @Produce json
// @Param season query int false "Season (e.g. 2000)"
// @Param isPlayoff query bool false "Whether the stats are for playoffs"
// @Success 200 {object} map[string]string
// @Failure 500 {object} map[string]string
// @Router /api/playertotals/fetch [get]
package controllers package controllers
import ( import (
@@ -19,6 +7,57 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
// --- MODIFICATION START ---
// A DTO is created to control the JSON output.
// It omits fields like ID, ExternalID, CreatedAt, UpdatedAt, and DeletedAt.
type PlayerTotalStatDTO struct {
PlayerID string `json:"playerId"`
PlayerName string `json:"playerName"`
Position string `json:"position"`
Age int `json:"age"`
Games int `json:"games"`
GamesStarted int `json:"gamesStarted"`
MinutesPG float64 `json:"minutesPg"`
FieldGoals int `json:"fieldGoals"`
FieldAttempts int `json:"fieldAttempts"`
FieldPercent float64 `json:"fieldPercent"`
ThreeFG int `json:"threeFg"`
ThreeAttempts int `json:"threeAttempts"`
ThreePercent float64 `json:"threePercent"`
TwoFG int `json:"twoFg"`
TwoAttempts int `json:"twoAttempts"`
TwoPercent float64 `json:"twoPercent"`
EffectFGPercent float64 `json:"effectFgPercent"`
FT int `json:"ft"`
FTAttempts int `json:"ftAttempts"`
FTPercent float64 `json:"ftPercent"`
OffensiveRB int `json:"offensiveRb"`
DefensiveRB int `json:"defensiveRb"`
TotalRB int `json:"totalRb"`
Assists int `json:"assists"`
Steals int `json:"steals"`
Blocks int `json:"blocks"`
Turnovers int `json:"turnovers"`
PersonalFouls int `json:"personalFouls"`
Points int `json:"points"`
Team string `json:"team"`
Season int `json:"season"`
IsPlayoff bool `json:"isPlayoff"`
}
// A dedicated response struct is created for cleaner, type-safe code.
type PlayerTotalsResponse struct {
Data []PlayerTotalStatDTO `json:"data"`
Pagination struct {
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"pageSize"`
Pages int64 `json:"pages"`
} `json:"pagination"`
}
// --- MODIFICATION END ---
var totalSortMap = map[string]string{ var totalSortMap = map[string]string{
"playerId": "player_id", "playerId": "player_id",
"playerName": "player_name", "playerName": "player_name",
@@ -53,29 +92,15 @@ var totalSortMap = map[string]string{
"season": "season", "season": "season",
} }
// func FetchPlayerTotalStats(db *gorm.DB) fiber.Handler {
// return func(c *fiber.Ctx) error {
// season := c.QueryInt("season", 2025)
// isPlayoff := c.QueryBool("isPlayoff", false)
// err := services.FetchAndStorePlayerTotalStats(db, season, isPlayoff)
// if err != nil {
// return c.Status(500).JSON(fiber.Map{"error": err.Error()})
// }
// return c.JSON(fiber.Map{"message": "Player total stats fetched and saved."})
// }
// }
// ScrapePlayerTotalStats godoc // ScrapePlayerTotalStats godoc
// @ignore // @ignore
// @Summary Scrape player total stats from BR website // @Summary Scrape player total stats from BR website
// @Tags PlayerTotals // @Tags PlayerTotals
// @Param season query int true "Season (e.g. 2025)" // @Param season query int true "Season (e.g. 2025)"
// @Param isPlayoff query bool false "Whether playoffs?" // @Param isPlayoff query bool false "Whether playoffs?"
// @Success 200 {object} map[string]string // @Success 200 {object} map[string]string
// @Failure 400,500 {object} map[string]string // @Failure 400,500 {object} map[string]string
// //@Router /api/playertotals/scrape [get] // //@Router /api/playertotals/scrape [get]
func ScrapePlayerTotalStats(db *gorm.DB) fiber.Handler { func ScrapePlayerTotalStats(db *gorm.DB) fiber.Handler {
return func(c *fiber.Ctx) error { return func(c *fiber.Ctx) error {
season := c.QueryInt("season", 0) season := c.QueryInt("season", 0)
@@ -98,7 +123,7 @@ func ScrapePlayerTotalStats(db *gorm.DB) fiber.Handler {
// @Tags PlayerTotals // @Tags PlayerTotals
// @x-order 1 // @x-order 1
// @Group Player Stats // @Group Player Stats
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Param season query int false "Season (e.g. 2000)" // @Param season query int false "Season (e.g. 2000)"
// @Param team query string false "Team abbreviation (e.g. LAL)" // @Param team query string false "Team abbreviation (e.g. LAL)"
@@ -108,7 +133,7 @@ func ScrapePlayerTotalStats(db *gorm.DB) fiber.Handler {
// @Param sortBy query string false "Field to sort by (e.g. points, assists)" // @Param sortBy query string false "Field to sort by (e.g. points, assists)"
// @Param ascending query bool false "Sort ascending (default false)" // @Param ascending query bool false "Sort ascending (default false)"
// @Param isPlayoff query bool false "Whether the stats are for playoffs" // @Param isPlayoff query bool false "Whether the stats are for playoffs"
// @Success 200 {object} map[string]interface{} // @Success 200 {object} controllers.PlayerTotalsResponse
// @Failure 500 {object} map[string]string // @Failure 500 {object} map[string]string
// @Router /api/playertotals [get] // @Router /api/playertotals [get]
func GetPlayerTotalStats(db *gorm.DB) fiber.Handler { func GetPlayerTotalStats(db *gorm.DB) fiber.Handler {
@@ -116,32 +141,31 @@ func GetPlayerTotalStats(db *gorm.DB) fiber.Handler {
var stats []models.PlayerTotalStat var stats []models.PlayerTotalStat
// --- FILTERS --- // --- FILTERS ---
playerId := c.Query("playerId") playerId := c.Query("playerId")
if playerId == "" { if playerId == "" {
playerId = c.Query("player_id") playerId = c.Query("player_id")
} }
season := c.QueryInt("season", 0) season := c.QueryInt("season", 0)
team := c.Query("team") team := c.Query("team")
// playerId := c.Query("playerId")
page := c.QueryInt("page", 1) page := c.QueryInt("page", 1)
pageSize := c.QueryInt("pageSize", 20) pageSize := c.QueryInt("pageSize", 20)
// --- MODIFICATION FOR SORTING --- // --- SORTING ---
sortByParam := c.Query("sortBy", "points") sortByParam := c.Query("sortBy", "points")
ascending := c.QueryBool("ascending", false) ascending := c.QueryBool("ascending", false)
// Translate sortBy param to a valid DB column, defaulting if not found. // Translate sortBy param to a valid DB column, defaulting if not found.
sortBy, ok := totalSortMap[sortByParam] sortBy, ok := totalSortMap[sortByParam]
if !ok { if !ok {
sortBy = "points" // Safe default sortBy = "points" // Safe default
} }
offset := (page - 1) * pageSize offset := (page - 1) * pageSize
order := sortBy + " DESC" order := sortBy + " DESC"
if ascending { if ascending {
order = sortBy + " ASC" order = sortBy + " ASC"
} }
query := db.Model(&models.PlayerTotalStat{}) query := db.Model(&models.PlayerTotalStat{})
@@ -169,14 +193,56 @@ func GetPlayerTotalStats(db *gorm.DB) fiber.Handler {
return c.Status(500).JSON(fiber.Map{"error": err.Error()}) return c.Status(500).JSON(fiber.Map{"error": err.Error()})
} }
return c.JSON(fiber.Map{ // --- MODIFICATION START ---
"data": stats, // Transform the database models into DTOs.
"pagination": fiber.Map{ playerTotalDTOs := make([]PlayerTotalStatDTO, len(stats))
"total": total, for i, s := range stats {
"page": page, playerTotalDTOs[i] = PlayerTotalStatDTO{
"pageSize": pageSize, PlayerID: s.PlayerID,
"pages": (total + int64(pageSize) - 1) / int64(pageSize), PlayerName: s.PlayerName,
}, Position: s.Position,
}) Age: s.Age,
Games: s.Games,
GamesStarted: s.GamesStarted,
MinutesPG: s.MinutesPG,
FieldGoals: s.FieldGoals,
FieldAttempts: s.FieldAttempts,
FieldPercent: s.FieldPercent,
ThreeFG: s.ThreeFG,
ThreeAttempts: s.ThreeAttempts,
ThreePercent: s.ThreePercent,
TwoFG: s.TwoFG,
TwoAttempts: s.TwoAttempts,
TwoPercent: s.TwoPercent,
EffectFGPercent: s.EffectFGPercent,
FT: s.FT,
FTAttempts: s.FTAttempts,
FTPercent: s.FTPercent,
OffensiveRB: s.OffensiveRB,
DefensiveRB: s.DefensiveRB,
TotalRB: s.TotalRB,
Assists: s.Assists,
Steals: s.Steals,
Blocks: s.Blocks,
Turnovers: s.Turnovers,
PersonalFouls: s.PersonalFouls,
Points: s.Points,
Team: s.Team,
Season: s.Season,
IsPlayoff: s.IsPlayoff,
}
}
// Build the final response using the new DTOs and response struct.
resp := PlayerTotalsResponse{
Data: playerTotalDTOs,
}
resp.Pagination.Total = total
resp.Pagination.Page = page
resp.Pagination.PageSize = pageSize
resp.Pagination.Pages = (total + int64(pageSize) - 1) / int64(pageSize)
return c.JSON(resp)
// --- MODIFICATION END ---
} }
} }
+132 -8
View File
@@ -355,8 +355,7 @@ const docTemplate = `{
"200": { "200": {
"description": "OK", "description": "OK",
"schema": { "schema": {
"type": "object", "$ref": "#/definitions/controllers.PlayerTotalsResponse"
"additionalProperties": true
} }
}, },
"500": { "500": {
@@ -408,9 +407,6 @@ const docTemplate = `{
"arena": { "arena": {
"type": "string" "type": "string"
}, },
"boxScoreUrl": {
"type": "string"
},
"date": { "date": {
"type": "string" "type": "string"
}, },
@@ -418,6 +414,7 @@ const docTemplate = `{
"type": "string" "type": "string"
}, },
"gameId": { "gameId": {
"description": "ID uint ` + "`" + `json:\"id\"` + "`" + `",
"type": "string" "type": "string"
}, },
"homePts": { "homePts": {
@@ -426,13 +423,11 @@ const docTemplate = `{
"homeTeam": { "homeTeam": {
"type": "string" "type": "string"
}, },
"id": {
"type": "integer"
},
"isPlayoff": { "isPlayoff": {
"type": "boolean" "type": "boolean"
}, },
"lineScores": { "lineScores": {
"description": "BoxScoreURL string ` + "`" + `json:\"boxScoreUrl\"` + "`" + `",
"type": "array", "type": "array",
"items": { "items": {
"$ref": "#/definitions/controllers.LineScoreDTO" "$ref": "#/definitions/controllers.LineScoreDTO"
@@ -678,6 +673,135 @@ const docTemplate = `{
} }
} }
}, },
"controllers.PlayerTotalStatDTO": {
"type": "object",
"properties": {
"age": {
"type": "integer"
},
"assists": {
"type": "integer"
},
"blocks": {
"type": "integer"
},
"defensiveRb": {
"type": "integer"
},
"effectFgPercent": {
"type": "number"
},
"fieldAttempts": {
"type": "integer"
},
"fieldGoals": {
"type": "integer"
},
"fieldPercent": {
"type": "number"
},
"ft": {
"type": "integer"
},
"ftAttempts": {
"type": "integer"
},
"ftPercent": {
"type": "number"
},
"games": {
"type": "integer"
},
"gamesStarted": {
"type": "integer"
},
"isPlayoff": {
"type": "boolean"
},
"minutesPg": {
"type": "number"
},
"offensiveRb": {
"type": "integer"
},
"personalFouls": {
"type": "integer"
},
"playerId": {
"type": "string"
},
"playerName": {
"type": "string"
},
"points": {
"type": "integer"
},
"position": {
"type": "string"
},
"season": {
"type": "integer"
},
"steals": {
"type": "integer"
},
"team": {
"type": "string"
},
"threeAttempts": {
"type": "integer"
},
"threeFg": {
"type": "integer"
},
"threePercent": {
"type": "number"
},
"totalRb": {
"type": "integer"
},
"turnovers": {
"type": "integer"
},
"twoAttempts": {
"type": "integer"
},
"twoFg": {
"type": "integer"
},
"twoPercent": {
"type": "number"
}
}
},
"controllers.PlayerTotalsResponse": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/controllers.PlayerTotalStatDTO"
}
},
"pagination": {
"type": "object",
"properties": {
"page": {
"type": "integer"
},
"pageSize": {
"type": "integer"
},
"pages": {
"type": "integer"
},
"total": {
"type": "integer"
}
}
}
}
},
"controllers.TeamGameAdvStatDTO": { "controllers.TeamGameAdvStatDTO": {
"type": "object", "type": "object",
"properties": { "properties": {
+132 -8
View File
@@ -352,8 +352,7 @@
"200": { "200": {
"description": "OK", "description": "OK",
"schema": { "schema": {
"type": "object", "$ref": "#/definitions/controllers.PlayerTotalsResponse"
"additionalProperties": true
} }
}, },
"500": { "500": {
@@ -405,9 +404,6 @@
"arena": { "arena": {
"type": "string" "type": "string"
}, },
"boxScoreUrl": {
"type": "string"
},
"date": { "date": {
"type": "string" "type": "string"
}, },
@@ -415,6 +411,7 @@
"type": "string" "type": "string"
}, },
"gameId": { "gameId": {
"description": "ID uint `json:\"id\"`",
"type": "string" "type": "string"
}, },
"homePts": { "homePts": {
@@ -423,13 +420,11 @@
"homeTeam": { "homeTeam": {
"type": "string" "type": "string"
}, },
"id": {
"type": "integer"
},
"isPlayoff": { "isPlayoff": {
"type": "boolean" "type": "boolean"
}, },
"lineScores": { "lineScores": {
"description": "BoxScoreURL string `json:\"boxScoreUrl\"`",
"type": "array", "type": "array",
"items": { "items": {
"$ref": "#/definitions/controllers.LineScoreDTO" "$ref": "#/definitions/controllers.LineScoreDTO"
@@ -675,6 +670,135 @@
} }
} }
}, },
"controllers.PlayerTotalStatDTO": {
"type": "object",
"properties": {
"age": {
"type": "integer"
},
"assists": {
"type": "integer"
},
"blocks": {
"type": "integer"
},
"defensiveRb": {
"type": "integer"
},
"effectFgPercent": {
"type": "number"
},
"fieldAttempts": {
"type": "integer"
},
"fieldGoals": {
"type": "integer"
},
"fieldPercent": {
"type": "number"
},
"ft": {
"type": "integer"
},
"ftAttempts": {
"type": "integer"
},
"ftPercent": {
"type": "number"
},
"games": {
"type": "integer"
},
"gamesStarted": {
"type": "integer"
},
"isPlayoff": {
"type": "boolean"
},
"minutesPg": {
"type": "number"
},
"offensiveRb": {
"type": "integer"
},
"personalFouls": {
"type": "integer"
},
"playerId": {
"type": "string"
},
"playerName": {
"type": "string"
},
"points": {
"type": "integer"
},
"position": {
"type": "string"
},
"season": {
"type": "integer"
},
"steals": {
"type": "integer"
},
"team": {
"type": "string"
},
"threeAttempts": {
"type": "integer"
},
"threeFg": {
"type": "integer"
},
"threePercent": {
"type": "number"
},
"totalRb": {
"type": "integer"
},
"turnovers": {
"type": "integer"
},
"twoAttempts": {
"type": "integer"
},
"twoFg": {
"type": "integer"
},
"twoPercent": {
"type": "number"
}
}
},
"controllers.PlayerTotalsResponse": {
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/controllers.PlayerTotalStatDTO"
}
},
"pagination": {
"type": "object",
"properties": {
"page": {
"type": "integer"
},
"pageSize": {
"type": "integer"
},
"pages": {
"type": "integer"
},
"total": {
"type": "integer"
}
}
}
}
},
"controllers.TeamGameAdvStatDTO": { "controllers.TeamGameAdvStatDTO": {
"type": "object", "type": "object",
"properties": { "properties": {
+88 -6
View File
@@ -22,23 +22,21 @@ definitions:
properties: properties:
arena: arena:
type: string type: string
boxScoreUrl:
type: string
date: date:
type: string type: string
gameDuration: gameDuration:
type: string type: string
gameId: gameId:
description: ID uint `json:"id"`
type: string type: string
homePts: homePts:
type: integer type: integer
homeTeam: homeTeam:
type: string type: string
id:
type: integer
isPlayoff: isPlayoff:
type: boolean type: boolean
lineScores: lineScores:
description: BoxScoreURL string `json:"boxScoreUrl"`
items: items:
$ref: '#/definitions/controllers.LineScoreDTO' $ref: '#/definitions/controllers.LineScoreDTO'
type: array type: array
@@ -201,6 +199,91 @@ definitions:
trb: trb:
type: integer type: integer
type: object type: object
controllers.PlayerTotalStatDTO:
properties:
age:
type: integer
assists:
type: integer
blocks:
type: integer
defensiveRb:
type: integer
effectFgPercent:
type: number
fieldAttempts:
type: integer
fieldGoals:
type: integer
fieldPercent:
type: number
ft:
type: integer
ftAttempts:
type: integer
ftPercent:
type: number
games:
type: integer
gamesStarted:
type: integer
isPlayoff:
type: boolean
minutesPg:
type: number
offensiveRb:
type: integer
personalFouls:
type: integer
playerId:
type: string
playerName:
type: string
points:
type: integer
position:
type: string
season:
type: integer
steals:
type: integer
team:
type: string
threeAttempts:
type: integer
threeFg:
type: integer
threePercent:
type: number
totalRb:
type: integer
turnovers:
type: integer
twoAttempts:
type: integer
twoFg:
type: integer
twoPercent:
type: number
type: object
controllers.PlayerTotalsResponse:
properties:
data:
items:
$ref: '#/definitions/controllers.PlayerTotalStatDTO'
type: array
pagination:
properties:
page:
type: integer
pageSize:
type: integer
pages:
type: integer
total:
type: integer
type: object
type: object
controllers.TeamGameAdvStatDTO: controllers.TeamGameAdvStatDTO:
properties: properties:
astPercent: astPercent:
@@ -614,8 +697,7 @@ paths:
"200": "200":
description: OK description: OK
schema: schema:
additionalProperties: true $ref: '#/definitions/controllers.PlayerTotalsResponse'
type: object
"500": "500":
description: Internal Server Error description: Internal Server Error
schema: schema: