fixed db issues

This commit is contained in:
2026-02-05 20:07:37 +11:00
parent 4c31c24069
commit 697bef80e9
9 changed files with 140 additions and 61 deletions

View File

@@ -42,6 +42,7 @@ func NewSeason(ctx context.Context, tx bun.Tx, name, shortname string, start tim
}
_, err := tx.NewInsert().
Model(season).
Returning("id").
Exec(ctx)
if err != nil {
return nil, errors.Wrap(err, "tx.NewInsert")
@@ -50,22 +51,18 @@ func NewSeason(ctx context.Context, tx bun.Tx, name, shortname string, start tim
}
func ListSeasons(ctx context.Context, tx bun.Tx, pageOpts *PageOpts) (*SeasonList, error) {
pageOpts = setDefaultPageOpts(pageOpts, 1, 10, bun.OrderDesc, "start_date")
seasons := new([]*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)
query := tx.NewSelect().
Model(seasons)
total, err := query.Count(ctx)
if err != nil {
return nil, errors.Wrap(err, "tx.NewSelect")
return nil, errors.Wrap(err, "query.Count")
}
query, pageOpts = setPageOpts(query, pageOpts, 1, 10, bun.OrderDesc, "start_date")
err = query.Scan(ctx)
if err != nil && err != sql.ErrNoRows {
return nil, errors.Wrap(err, "query.Scan")
}
sl := &SeasonList{
Seasons: *seasons,
@@ -82,7 +79,10 @@ func GetSeason(ctx context.Context, tx bun.Tx, shortname string) (*Season, error
Where("short_name = ?", strings.ToUpper(shortname)).
Limit(1).
Scan(ctx)
if err != nil && err != sql.ErrNoRows {
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, errors.Wrap(err, "tx.NewSelect")
}
return season, nil