mirror of
https://github.com/nprasad2077/NBA_Go.git
synced 2026-09-22 14:05:13 +00:00
playoffs added to totals model
This commit is contained in:
@@ -15,3 +15,11 @@ curl -XPOST http://localhost:8080/admin/keys \
|
||||
# 3. call a protected endpoint
|
||||
curl http://localhost:8080/api/playeradvancedstats \
|
||||
-H "X-API-Key: ab12cd…"
|
||||
|
||||
```
|
||||
|
||||
### Swagger Initiate Docs
|
||||
|
||||
```bash
|
||||
swag init -g main.go -o docs
|
||||
```
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
// @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]
|
||||
@@ -21,8 +22,9 @@ import (
|
||||
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)
|
||||
err := services.FetchAndStorePlayerTotalStats(db, season, isPlayoff)
|
||||
if err != nil {
|
||||
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
@@ -46,6 +48,7 @@ func FetchPlayerTotalStats(db *gorm.DB) fiber.Handler {
|
||||
// @Param pageSize query int false "Page size" default(20)
|
||||
// @Param sortBy query string false "Field to sort by (e.g. points, assists)"
|
||||
// @Param ascending query bool false "Sort ascending (default false)"
|
||||
// @Param isPlayoff query bool false "Whether the stats are for playoffs"
|
||||
// @Success 200 {object} map[string]interface{}
|
||||
// @Failure 500 {object} map[string]string
|
||||
// @Router /api/playertotals [get]
|
||||
@@ -78,6 +81,12 @@ func GetPlayerTotalStats(db *gorm.DB) fiber.Handler {
|
||||
query = query.Where("player_id = ?", playerId)
|
||||
}
|
||||
|
||||
isPlayoffStr := c.Query("isPlayoff")
|
||||
if isPlayoffStr != "" {
|
||||
isPlayoff := c.QueryBool("isPlayoff", false)
|
||||
query = query.Where("is_playoff = ?", isPlayoff)
|
||||
}
|
||||
|
||||
var total int64
|
||||
query.Count(&total)
|
||||
|
||||
|
||||
@@ -152,6 +152,12 @@ const docTemplate = `{
|
||||
"description": "Sort ascending (default false)",
|
||||
"name": "ascending",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "boolean",
|
||||
"description": "Whether the stats are for playoffs",
|
||||
"name": "isPlayoff",
|
||||
"in": "query"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
|
||||
@@ -149,6 +149,12 @@
|
||||
"description": "Sort ascending (default false)",
|
||||
"name": "ascending",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "boolean",
|
||||
"description": "Whether the stats are for playoffs",
|
||||
"name": "isPlayoff",
|
||||
"in": "query"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
|
||||
@@ -90,6 +90,10 @@ paths:
|
||||
in: query
|
||||
name: ascending
|
||||
type: boolean
|
||||
- description: Whether the stats are for playoffs
|
||||
in: query
|
||||
name: isPlayoff
|
||||
type: boolean
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
|
||||
@@ -22,7 +22,7 @@ import (
|
||||
"github.com/gofiber/fiber/v2/middleware/adaptor"
|
||||
"github.com/gofiber/fiber/v2/middleware/logger"
|
||||
"github.com/nprasad2077/NBA_Go/config"
|
||||
// "github.com/nprasad2077/NBA_Go/services"
|
||||
"github.com/nprasad2077/NBA_Go/services"
|
||||
"github.com/nprasad2077/NBA_Go/controllers"
|
||||
"github.com/nprasad2077/NBA_Go/routes"
|
||||
"github.com/nprasad2077/NBA_Go/utils/middleware"
|
||||
@@ -67,29 +67,29 @@ func main() {
|
||||
routes.RegisterPlayerTotalRoutes(app, db)
|
||||
|
||||
/* ---------- Optional import job ---------- */
|
||||
// go func() {
|
||||
// for season := 2023; season <= 2025; season++ {
|
||||
// if err := services.FetchAndStorePlayerAdvancedStats(db, season); err != nil {
|
||||
// log.Printf("Fetch failed for player advanced season %d: %v\n", season, err)
|
||||
// } else {
|
||||
// log.Printf("Fetch successful for player advanced season %d\n", season)
|
||||
// }
|
||||
// time.Sleep(1100 * time.Millisecond) // optional delay
|
||||
// }
|
||||
// log.Printf("player advanced Import Success")
|
||||
// }()
|
||||
go func() {
|
||||
for season := 2023; season <= 2025; season++ {
|
||||
if err := services.FetchAndStorePlayerAdvancedStats(db, season); err != nil {
|
||||
log.Printf("Fetch failed for player advanced season %d: %v\n", season, err)
|
||||
} else {
|
||||
log.Printf("Fetch successful for player advanced season %d\n", season)
|
||||
}
|
||||
time.Sleep(1100 * time.Millisecond) // optional delay
|
||||
}
|
||||
log.Printf("player advanced Import Success")
|
||||
}()
|
||||
|
||||
// go func() {
|
||||
// for season := 2023; season <= 2025; season++ {
|
||||
// if err := services.FetchAndStorePlayerTotalStats(db, season); err != nil {
|
||||
// log.Printf("Fetch failed for player totals season %d: %v\n", season, err)
|
||||
// } else {
|
||||
// log.Printf("Fetch successful for player totals season %d\n", season)
|
||||
// }
|
||||
// time.Sleep(1000 * time.Millisecond) // optional delay
|
||||
// }
|
||||
// log.Printf("player totals Import Success")
|
||||
// }()
|
||||
go func() {
|
||||
for season := 2023; season <= 2025; season++ {
|
||||
if err := services.FetchAndStorePlayerTotalStats(db, season, false); err != nil {
|
||||
log.Printf("Fetch failed for player totals season %d: %v\n", season, err)
|
||||
} else {
|
||||
log.Printf("Fetch successful for player totals season %d\n", season)
|
||||
}
|
||||
time.Sleep(1000 * time.Millisecond) // optional delay
|
||||
}
|
||||
log.Printf("player totals Import Success")
|
||||
}()
|
||||
|
||||
/* ---------- START & SHUTDOWN ---------- */
|
||||
go func() {
|
||||
|
||||
@@ -36,4 +36,5 @@ type PlayerTotalStat struct {
|
||||
Points int `json:"points"`
|
||||
Team string `gorm:"not null;uniqueIndex:idx_total_player_season_team" json:"team"`
|
||||
Season int `gorm:"not null;uniqueIndex:idx_total_player_season_team" json:"season"`
|
||||
IsPlayoff bool `gorm:"not null;default:false;uniqueIndex:idx_total_player_season_team" json:"isPlayoff"`
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
func FetchAndStorePlayerTotalStats(db *gorm.DB, season int) error {
|
||||
func FetchAndStorePlayerTotalStats(db *gorm.DB, season int, isPlayoff bool) error {
|
||||
url := fmt.Sprintf(
|
||||
"http://rest.nbaapi.com/api/PlayerDataTotals/query?season=%d&sortBy=PlayerName&ascending=true&pageNumber=1&pageSize=1000",
|
||||
season,
|
||||
@@ -29,9 +29,10 @@ func FetchAndStorePlayerTotalStats(db *gorm.DB, season int) error {
|
||||
|
||||
for _, stat := range stats {
|
||||
stat.Season = season
|
||||
stat.IsPlayoff = isPlayoff
|
||||
|
||||
err := db.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "player_id"}, {Name: "season"}, {Name: "team"}},
|
||||
Columns: []clause.Column{{Name: "player_id"}, {Name: "season"}, {Name: "team"}, {Name: "is_playoff"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{
|
||||
"player_name", "position", "age", "games", "games_started",
|
||||
"minutes_pg", "field_goals", "field_attempts", "field_percent",
|
||||
|
||||
Reference in New Issue
Block a user