package football import ( "context" "fmt" "strings" "git.lehouerou.net/laurent/sorare/graphql" "github.com/pkg/errors" ) type GamesScoresQuery struct { c *graphql.Client } func NewGamesScoresQuery(c *graphql.Client) *GamesScoresQuery { return &GamesScoresQuery{ c: c, } } func (r *GamesScoresQuery) Get(ctx context.Context, params map[string][]string) ([]So5Score, error) { var q struct { Football [][2]interface{} `graphql:"football"` } for gameId, playerSlugs := range params { if len(playerSlugs) == 0 { delete(params, gameId) } } type PlayerScore struct { So5Score graphql.Wrapper[So5Score] } q.Football = make([][2]interface{}, len(params)) cntGame := 0 for gameId, playerSlugs := range params { slugsParam := "" for _, slug := range playerSlugs { slugsParam += fmt.Sprintf("\"%s\",", slug) } slugsParam = strings.TrimRight(slugsParam, ",") pl := make([]PlayerScore, 1) pl[0] = PlayerScore{ So5Score: *graphql.NewWrapper[So5Score](fmt.Sprintf("so5Score(gameId:\"%s\")", gameId)), } q.Football[cntGame] = [2]interface{}{ fmt.Sprintf("players%d:players(slugs:[%s])", cntGame, slugsParam), &pl, } cntGame++ } err := r.c.Query(ctx, &q, nil) if err != nil { return nil, errors.Wrap(err, "querying games") } var res []So5Score for _, g := range q.Football { if g[1] == nil { continue } for _, s := range *g[1].(*[]PlayerScore) { res = append(res, s.So5Score.GetValue()) } } return res, nil } // func (r *GamesScoresQuery) Get(ctx context.Context, params map[string][]string) ([]So5Score, error) { // var q struct { // Football [][2]interface{} `graphql:"football"` // } // // for gameId, playerSlugs := range params { // if len(playerSlugs) == 0 { // delete(params, gameId) // } // } // // q.Football = make([][2]interface{}, len(params)) // // cntGame := 0 // cntPlayerSlug := 0 // for gameId, playerSlugs := range params { // game := make([][2]interface{}, len(playerSlugs)) // for i, playerSlug := range playerSlugs { // var score So5Score // game[i] = [2]interface{}{fmt.Sprintf("score%d:so5Score(playerSlug:\"%s\")", cntPlayerSlug, playerSlug), &score} // cntPlayerSlug++ // } // q.Football[cntGame] = [2]interface{}{fmt.Sprintf("game%d:game(id:\"%s\")", cntGame, gameId), &game} // cntGame++ // } // err := r.c.Query(ctx, &q, nil) // if err != nil { // return nil, errors.Wrap(err, "querying games") // } // var res []So5Score // for _, g := range q.Football { // if g[1] == nil { // continue // } // for _, s := range *g[1].(*[][2]interface{}) { // if s[1] == nil { // continue // } // res = append(res, *s[1].(*So5Score)) // } // } // return res, nil // // }