package sorare_utils import ( "context" "git.lehouerou.net/laurent/sorare" "git.lehouerou.net/laurent/sorare/graphql" "git.lehouerou.net/laurent/sorarebuddy/db" "git.lehouerou.net/laurent/sorarebuddy/model" "github.com/pkg/errors" "github.com/rs/zerolog/log" "github.com/samber/lo" ) func NewCountryFromSorare(s sorare.Country) model.Country { return model.Country{ Slug: s.Slug, Code: s.Code, DisplayName: s.Name, ThreeLetterCode: s.ThreeLetterCode, FlagFlat64Url: s.FlagFlat64Url, FlagFlat32Url: s.FlagFlat32Url, FlagRound64Url: s.FlagRound64Url, FlagRound32Url: s.FlagRound32Url, } } type CountryUpdater struct { db *db.Client s *sorare.Sorare cache []sorare.Country slugsToRead []string } func NewCountryUpdater(s *sorare.Sorare, db *db.Client) *CountryUpdater { return &CountryUpdater{db: db, s: s} } func (u *CountryUpdater) AddSlugsToRead(slugs []string) { slugs = lo.Filter(slugs, func(slug string, index int) bool { return slug != "" }) u.slugsToRead = append(u.slugsToRead, slugs...) } func (u *CountryUpdater) Read(ctx context.Context, onlyMissings bool) error { if len(u.slugsToRead) == 0 { return nil } log.Debug().Msgf("reading %d countries...", len(u.slugsToRead)) slugs := u.slugsToRead u.slugsToRead = nil if onlyMissings { log.Debug().Msgf("filtering countries in db...") slugsNotInDb, err := u.db.Countries.GetCountrySlugsNotInDb(ctx, slugs) if err != nil { return errors.Wrap(err, "getting countries not in db") } slugs = slugsNotInDb log.Debug().Msgf("%d countries not in db", len(slugs)) } log.Debug().Msgf("getting countries from sorare...") u.cache = nil for i, chunk := range lo.Chunk(slugs, 100) { log.Debug().Msgf("\tbatch %d/%d", i+1, (len(slugs)/100)+1) c, err := u.s.Countries.Get(ctx, graphql.SlugsParams{Slugs: chunk}) if err != nil { return err } u.cache = append(u.cache, c...) } log.Debug().Msgf("%d countries fetched from sorare", len(u.cache)) return nil } func (u *CountryUpdater) Write(ctx context.Context) error { log.Debug().Msg("inserting countries into db...") err := u.db.Countries.CreateOrUpdateMany( ctx, lo.Map(u.cache, func(country sorare.Country, index int) model.Country { return NewCountryFromSorare(country) }), ) if err != nil { return errors.Wrap(err, "inserting countries") } log.Debug().Msgf("%d countries inserted", len(u.cache)) u.cache = nil return nil }