add query to retrieve multiple game scores in the same query

This commit is contained in:
Laurent Le Houerou 2024-03-27 20:54:06 +04:00
parent 0d2135178e
commit 56dd5c5156
4 changed files with 137 additions and 4 deletions

2
.gitignore vendored
View File

@ -1,5 +1,5 @@
.vscode/
.idea/
go.work
go.work.sum

View File

@ -21,7 +21,7 @@ func main() {
if audience == "" {
log.Fatal("No audience provided")
}
api := sorare.New()
api := sorare.New().Debug()
api.SetJWTToken(
graphql.JwtToken{
Token: token,
@ -32,9 +32,18 @@ func main() {
ctx := context.Background()
cu, err := api.Country.Get(ctx, graphql.SlugParams{Slug: "fr"})
//cu, err := api.Country.Get(ctx, graphql.SlugParams{Slug: "fr"})
params := make(map[string][]string)
params["0c7d5f17-7c01-4427-90b8-a736a44ea632"] = []string{"joshua-kimmich", "leon-goretzka"}
params["0d641a1d-c64a-40c9-85b0-7ddeca428abf"] = []string{"joshua-kimmich", "leon-goretzka"}
cu, err := api.Football.GamesScores.Get(
ctx,
params,
)
if err != nil {
panic(err)
}
fmt.Println(cu)
for _, game := range cu {
log.Println(game.Game.Id.Value + " " + game.Player.Slug + " " + fmt.Sprintf("%s", game.Score))
}
}

View File

@ -18,6 +18,7 @@ type Football struct {
Competition *graphql.Query[Competition, graphql.SlugParams]
Game *graphql.Query[GameWithFormation, graphql.IdParams]
GamesFormation *GamesFormationQuery
GamesScores *GamesScoresQuery
MyLiveGames *graphql.Query[[]Game, graphql.EmptyParams]
NationalTeam *graphql.Query[NationalTeam, graphql.SlugParams]
NationalTeams *graphql.Query[[]NationalTeam, graphql.SlugsParams]
@ -81,6 +82,9 @@ func NewFootball(c *graphql.Client) *Football {
GamesFormation: NewGamesFormationQuery(
c,
),
GamesScores: NewGamesScoresQuery(
c,
),
MyLiveGames: graphql.NewQuery[[]Game, graphql.EmptyParams](
c,
"myLiveGames",

View File

@ -0,0 +1,120 @@
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
//
// }