48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
|
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"}),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|