sorarebuddy/db/fixture.go

70 lines
1.6 KiB
Go
Raw Permalink Normal View History

2024-05-23 04:18:54 +00:00
package db
import (
"context"
"github.com/uptrace/bun"
"git.lehouerou.net/laurent/sorarebuddy/model"
)
type FixtureRepository struct {
*Repository[model.Fixture]
}
func NewFixtureRepository(db *bun.DB) *FixtureRepository {
return &FixtureRepository{
Repository: NewRepository[model.Fixture](db, []string{"slug"}),
}
}
func (r *FixtureRepository) GetByGameWeek(ctx context.Context, gameweeks ...int) ([]model.Fixture, error) {
var fixtures []model.Fixture
err := r.db.NewSelect().
Model(&fixtures).
Where("game_week IN (?)", bun.In(gameweeks)).
Scan(ctx)
return fixtures, err
}
func (r *FixtureRepository) GetBySlug(ctx context.Context, slug string) (model.Fixture, error) {
var fixture model.Fixture
err := r.db.NewSelect().
Model(&fixture).
Where("slug = ?", slug).
Scan(ctx)
return fixture, err
}
func (r *FixtureRepository) GetStarted(ctx context.Context) ([]model.Fixture, error) {
var fixtures []model.Fixture
err := r.db.NewSelect().
Model(&fixtures).
Where("state = ?", "started").
Scan(ctx)
return fixtures, err
}
func (r *FixtureRepository) GetOpened(ctx context.Context) ([]model.Fixture, error) {
var fixtures []model.Fixture
err := r.db.NewSelect().
Model(&fixtures).
Where("state = ?", "opened").
Scan(ctx)
return fixtures, err
}
func (r *FixtureRepository) GetLastClosed(ctx context.Context) (model.Fixture, error) {
var fixture model.Fixture
err := r.db.NewSelect().
Model(&fixture).
Where("state IN (?)", bun.In([]string{"closed", "computed"})).
Order("game_week DESC").
Limit(1).
Scan(ctx)
return fixture, err
}