fixed some migration issues and added generics for update and insert
This commit is contained in:
@@ -13,34 +13,22 @@ 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"`
|
||||
Name string `bun:"name,unique,notnull"`
|
||||
ShortName string `bun:"short_name,unique,notnull"`
|
||||
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"`
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
// NewSeason returns a new season. It does not add it to the database
|
||||
func NewSeason(name, shortname string, start time.Time) *Season {
|
||||
season := &Season{
|
||||
Name: name,
|
||||
ShortName: strings.ToUpper(shortname),
|
||||
StartDate: start.Truncate(time.Hour * 24),
|
||||
}
|
||||
_, err := tx.NewInsert().
|
||||
Model(season).
|
||||
Returning("id").
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "tx.NewInsert")
|
||||
}
|
||||
return season, nil
|
||||
return season
|
||||
}
|
||||
|
||||
func ListSeasons(ctx context.Context, tx bun.Tx, pageOpts *PageOpts) (*List[Season], error) {
|
||||
@@ -60,31 +48,16 @@ func GetSeason(ctx context.Context, tx bun.Tx, shortname string) (*Season, error
|
||||
return GetByField[Season](tx, "short_name", shortname).GetFirst(ctx)
|
||||
}
|
||||
|
||||
func UpdateSeason(ctx context.Context, tx bun.Tx, season *Season) error {
|
||||
if season == nil {
|
||||
return errors.New("season cannot be nil")
|
||||
// Update updates the season struct. It does not insert to the database
|
||||
func (s *Season) Update(start, end, finalsStart, finalsEnd time.Time) {
|
||||
s.StartDate = start.Truncate(time.Hour * 24)
|
||||
if !end.IsZero() {
|
||||
s.EndDate.Time = end.Truncate(time.Hour * 24)
|
||||
}
|
||||
if season.ID == 0 {
|
||||
return errors.New("season ID cannot be 0")
|
||||
if !finalsStart.IsZero() {
|
||||
s.FinalsStartDate.Time = finalsStart.Truncate(time.Hour * 24)
|
||||
}
|
||||
// Truncate dates to day precision
|
||||
season.StartDate = season.StartDate.Truncate(time.Hour * 24)
|
||||
if !season.EndDate.IsZero() {
|
||||
season.EndDate.Time = season.EndDate.Time.Truncate(time.Hour * 24)
|
||||
if !finalsEnd.IsZero() {
|
||||
s.FinalsEndDate.Time = finalsEnd.Truncate(time.Hour * 24)
|
||||
}
|
||||
if !season.FinalsStartDate.IsZero() {
|
||||
season.FinalsStartDate.Time = season.FinalsStartDate.Time.Truncate(time.Hour * 24)
|
||||
}
|
||||
if !season.FinalsEndDate.IsZero() {
|
||||
season.FinalsEndDate.Time = season.FinalsEndDate.Time.Truncate(time.Hour * 24)
|
||||
}
|
||||
_, err := tx.NewUpdate().
|
||||
Model(season).
|
||||
Column("start_date", "end_date", "finals_start_date", "finals_end_date").
|
||||
Where("id = ?", season.ID).
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "tx.NewUpdate")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user