42 lines
1.0 KiB
Go
42 lines
1.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"git.haelnorr.com/h/golib/hws"
|
|
"git.haelnorr.com/h/oslstats/internal/db"
|
|
seasonsview "git.haelnorr.com/h/oslstats/internal/view/seasonsview"
|
|
"github.com/pkg/errors"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
// SeasonsPage renders the season list. On GET it returns the full page, otherwise it just returns the list
|
|
func SeasonsPage(
|
|
s *hws.Server,
|
|
conn *db.DB,
|
|
) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
pageOpts, ok := db.GetPageOpts(s, w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
var seasons *db.List[db.Season]
|
|
if ok := conn.WithReadTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
|
var err error
|
|
seasons, err = db.ListSeasons(ctx, tx, pageOpts)
|
|
if err != nil {
|
|
return false, errors.Wrap(err, "db.ListSeasons")
|
|
}
|
|
return true, nil
|
|
}); !ok {
|
|
return
|
|
}
|
|
if r.Method == "GET" {
|
|
renderSafely(seasonsview.ListPage(seasons), s, r, w)
|
|
} else {
|
|
renderSafely(seasonsview.SeasonsList(seasons), s, r, w)
|
|
}
|
|
})
|
|
}
|