126 lines
2.9 KiB
Go
126 lines
2.9 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
type Season struct {
|
|
bun.BaseModel `bun:"table:seasons,alias:s"`
|
|
|
|
ID int `bun:"id,pk,autoincrement"`
|
|
Name string `bun:"name,unique"`
|
|
ShortName string `bun:"short_name,unique"`
|
|
StartDate time.Time `bun:"start_date,notnull"`
|
|
EndDate bun.NullTime `bun:"end_date"`
|
|
FinalsStartDate bun.NullTime `bun:"finals_start_date"`
|
|
FinalsEndDate bun.NullTime `bun:"finals_end_date"`
|
|
}
|
|
|
|
type SeasonList struct {
|
|
Seasons []Season
|
|
Total int
|
|
PageOpts PageOpts
|
|
}
|
|
|
|
func NewSeason(ctx context.Context, tx bun.Tx, name, shortname string, start time.Time) (*Season, error) {
|
|
if name == "" {
|
|
return nil, errors.New("name cannot be empty")
|
|
}
|
|
if shortname == "" {
|
|
return nil, errors.New("shortname cannot be empty")
|
|
}
|
|
season := &Season{
|
|
Name: name,
|
|
ShortName: strings.ToUpper(shortname),
|
|
StartDate: start.Truncate(time.Hour * 24),
|
|
}
|
|
_, err := tx.NewInsert().
|
|
Model(season).
|
|
Exec(ctx)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "tx.NewInsert")
|
|
}
|
|
return season, nil
|
|
}
|
|
|
|
func ListSeasons(ctx context.Context, tx bun.Tx, pageOpts *PageOpts) (*SeasonList, error) {
|
|
if pageOpts == nil {
|
|
pageOpts = &PageOpts{}
|
|
}
|
|
if pageOpts.Page == 0 {
|
|
pageOpts.Page = 1
|
|
}
|
|
if pageOpts.PerPage == 0 {
|
|
pageOpts.PerPage = 10
|
|
}
|
|
if pageOpts.Order == "" {
|
|
pageOpts.Order = bun.OrderDesc
|
|
}
|
|
if pageOpts.OrderBy == "" {
|
|
pageOpts.OrderBy = "start_date"
|
|
}
|
|
seasons := []Season{}
|
|
err := tx.NewSelect().
|
|
Model(&seasons).
|
|
OrderBy(pageOpts.OrderBy, pageOpts.Order).
|
|
Offset(pageOpts.PerPage * (pageOpts.Page - 1)).
|
|
Limit(pageOpts.PerPage).
|
|
Scan(ctx)
|
|
if err != nil && err != sql.ErrNoRows {
|
|
return nil, errors.Wrap(err, "tx.NewSelect")
|
|
}
|
|
total, err := tx.NewSelect().
|
|
Model(&seasons).
|
|
Count(ctx)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "tx.NewSelect")
|
|
}
|
|
sl := &SeasonList{
|
|
Seasons: seasons,
|
|
Total: total,
|
|
PageOpts: *pageOpts,
|
|
}
|
|
return sl, nil
|
|
}
|
|
|
|
func GetSeason(ctx context.Context, tx bun.Tx, shortname string) (*Season, error) {
|
|
season := new(Season)
|
|
err := tx.NewSelect().
|
|
Model(season).
|
|
Where("short_name = ?", strings.ToUpper(shortname)).
|
|
Limit(1).
|
|
Scan(ctx)
|
|
if err != nil && err != sql.ErrNoRows {
|
|
return nil, errors.Wrap(err, "tx.NewSelect")
|
|
}
|
|
return season, nil
|
|
}
|
|
|
|
func IsSeasonNameUnique(ctx context.Context, tx bun.Tx, name string) (bool, error) {
|
|
count, err := tx.NewSelect().
|
|
Model((*Season)(nil)).
|
|
Where("name = ?", name).
|
|
Count(ctx)
|
|
if err != nil {
|
|
return false, errors.Wrap(err, "tx.NewSelect")
|
|
}
|
|
return count == 0, nil
|
|
}
|
|
|
|
func IsSeasonShortNameUnique(ctx context.Context, tx bun.Tx, shortname string) (bool, error) {
|
|
count, err := tx.NewSelect().
|
|
Model((*Season)(nil)).
|
|
Where("short_name = ?", shortname).
|
|
Count(ctx)
|
|
if err != nil {
|
|
return false, errors.Wrap(err, "tx.NewSelect")
|
|
}
|
|
return count == 0, nil
|
|
}
|