51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
|
package db
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"github.com/samber/lo"
|
||
|
"github.com/uptrace/bun"
|
||
|
|
||
|
"git.lehouerou.net/laurent/sorarebuddy/model"
|
||
|
)
|
||
|
|
||
|
type CompetitionRepository struct {
|
||
|
*Repository[model.Competition]
|
||
|
}
|
||
|
|
||
|
func NewCompetitionRepository(db *bun.DB) *CompetitionRepository {
|
||
|
return &CompetitionRepository{
|
||
|
Repository: NewRepository[model.Competition](db, []string{"slug"}),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (r *CompetitionRepository) GetAllClubCompetitions(ctx context.Context, zones []int) ([]model.Competition, error) {
|
||
|
var res []model.Competition
|
||
|
request := r.db.NewSelect().Model(&res).
|
||
|
Where("competition_type = ?", "CLUB").
|
||
|
Relation("Zone")
|
||
|
if len(zones) > 0 {
|
||
|
request = request.Where("zone_id IN (?)", bun.In(zones))
|
||
|
}
|
||
|
if err := request.Scan(ctx); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return res, nil
|
||
|
}
|
||
|
|
||
|
func (r *CompetitionRepository) GetCompetitionSlugsNotInDb(
|
||
|
ctx context.Context,
|
||
|
competitionSlugs []string,
|
||
|
) ([]string, error) {
|
||
|
var competitions []model.Competition
|
||
|
err := r.db.NewSelect().Model(&competitions).Where("slug IN (?)", bun.In(competitionSlugs)).Scan(ctx)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
diff, _ := lo.Difference(
|
||
|
competitionSlugs,
|
||
|
lo.Map(competitions, func(c model.Competition, index int) string { return c.Slug }),
|
||
|
)
|
||
|
return diff, nil
|
||
|
}
|