2024-05-23 04:18:54 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/samber/lo"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
|
|
|
|
"git.lehouerou.net/laurent/sorarebuddy/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TeamRepository struct {
|
|
|
|
*Repository[model.Team]
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTeamRepository(db *bun.DB) *TeamRepository {
|
|
|
|
return &TeamRepository{
|
|
|
|
Repository: NewRepository[model.Team](db, []string{"slug"}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-06 05:52:54 +00:00
|
|
|
func (r *TeamRepository) GetOne(ctx context.Context, slug string, includeGames bool) (model.Team, error) {
|
|
|
|
var team model.Team
|
|
|
|
req := r.db.NewSelect().
|
|
|
|
Model(&team).
|
|
|
|
Where("team.slug = ?", slug).
|
|
|
|
Relation("Country").
|
|
|
|
Relation("DomesticLeague").
|
|
|
|
Relation("DomesticLeague.Zone")
|
|
|
|
if includeGames {
|
|
|
|
req = req.Relation("HomeGames").
|
|
|
|
Relation("AwayGames")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := req.Scan(ctx)
|
|
|
|
return team, err
|
|
|
|
}
|
|
|
|
|
2024-05-23 04:18:54 +00:00
|
|
|
func (r *TeamRepository) GetTeamSlugsNotInDb(ctx context.Context, teamSlugs []string) ([]string, error) {
|
|
|
|
var teams []model.Team
|
|
|
|
err := r.db.NewSelect().
|
|
|
|
Model(&teams).
|
|
|
|
Where("slug IN (?)", bun.In(teamSlugs)).
|
|
|
|
Scan(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "getting players not in db")
|
|
|
|
}
|
|
|
|
diff, _ := lo.Difference(teamSlugs, lo.Map(teams, func(t model.Team, index int) string { return t.Slug }))
|
|
|
|
return diff, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *TeamRepository) SearchByDisplayName(ctx context.Context, displayName string, limit int) ([]model.Team, error) {
|
|
|
|
var teams []model.Team
|
|
|
|
err := r.db.NewSelect().
|
|
|
|
Model(&teams).
|
|
|
|
Relation("Country").
|
|
|
|
Relation("DomesticLeague").
|
|
|
|
Relation("DomesticLeague.Zone").
|
|
|
|
Where("f_unaccent(team.display_name) ILIKE ?", "%"+displayName+"%").
|
|
|
|
Limit(limit).
|
|
|
|
Scan(ctx)
|
|
|
|
return teams, err
|
|
|
|
}
|
2024-06-06 05:52:54 +00:00
|
|
|
|
|
|
|
func (r *TeamRepository) GetRoster(ctx context.Context, slug string) ([]model.Player, error) {
|
|
|
|
var players []model.Player
|
|
|
|
err := r.db.NewSelect().
|
|
|
|
Model(&players).
|
|
|
|
Where("player.team_slug = ?", slug).
|
|
|
|
Relation("CardSupply").
|
|
|
|
Relation("ClubMembership").
|
|
|
|
Relation("Country").
|
|
|
|
Relation("GamePlayers").
|
|
|
|
Relation("GamePlayers.Score").
|
|
|
|
Relation("GamePlayers.Game").
|
|
|
|
Scan(ctx)
|
|
|
|
return players, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *TeamRepository) GetMemberships(ctx context.Context, slug string) ([]model.Membership, error) {
|
|
|
|
var memberships []model.Membership
|
|
|
|
err := r.db.NewSelect().
|
|
|
|
Model(&memberships).
|
|
|
|
Relation("Player").
|
|
|
|
Relation("Player.ClubMembership").
|
|
|
|
Relation("Player.ClubMembership.Team").
|
|
|
|
Where("membership.team_slug = ?", slug).
|
|
|
|
Scan(ctx)
|
|
|
|
return memberships, err
|
|
|
|
}
|