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