big ole refactor
This commit is contained in:
@@ -11,18 +11,18 @@ import (
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
// SeasonsPage renders the full page with the seasons list, for use with GET requests
|
||||
// SeasonsPage renders the season list. On GET it returns the full page, otherwise it just returns the list
|
||||
func SeasonsPage(
|
||||
s *hws.Server,
|
||||
conn *bun.DB,
|
||||
conn *db.DB,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
pageOpts := pageOptsFromQuery(s, w, r)
|
||||
if pageOpts == nil {
|
||||
pageOpts, ok := db.GetPageOpts(s, w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var seasons *db.List[db.Season]
|
||||
if ok := db.WithReadTx(s, w, r, conn, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
||||
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 {
|
||||
@@ -32,31 +32,10 @@ func SeasonsPage(
|
||||
}); !ok {
|
||||
return
|
||||
}
|
||||
renderSafely(seasonsview.ListPage(seasons), s, r, w)
|
||||
})
|
||||
}
|
||||
|
||||
// SeasonsList renders just the seasons list, for use with POST requests and HTMX
|
||||
func SeasonsList(
|
||||
s *hws.Server,
|
||||
conn *bun.DB,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
pageOpts := pageOptsFromForm(s, w, r)
|
||||
if pageOpts == nil {
|
||||
return
|
||||
}
|
||||
var seasons *db.List[db.Season]
|
||||
if ok := db.WithReadTx(s, w, r, conn, 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
|
||||
}
|
||||
renderSafely(seasonsview.SeasonsList(seasons), s, r, w)
|
||||
if r.Method == "GET" {
|
||||
renderSafely(seasonsview.ListPage(seasons), s, r, w)
|
||||
} else {
|
||||
renderSafely(seasonsview.SeasonsList(seasons), s, r, w)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user