50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"git.haelnorr.com/h/golib/hws"
|
|
"git.haelnorr.com/h/oslstats/internal/db"
|
|
adminview "git.haelnorr.com/h/oslstats/internal/view/adminview"
|
|
"github.com/pkg/errors"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
// AdminUsersPage renders the full admin dashboard page with users section
|
|
func AdminUsersPage(s *hws.Server, conn *bun.DB) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var users *db.List[db.User]
|
|
if ok := db.WithReadTx(s, w, r, conn, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
|
var err error
|
|
users, err = db.GetUsersWithRoles(ctx, tx, nil)
|
|
if err != nil {
|
|
return false, errors.Wrap(err, "db.GetUsersWithRoles")
|
|
}
|
|
return true, nil
|
|
}); !ok {
|
|
return
|
|
}
|
|
renderSafely(adminview.DashboardPage(users), s, r, w)
|
|
})
|
|
}
|
|
|
|
// AdminUsersList shows all users (HTMX content replacement)
|
|
func AdminUsersList(s *hws.Server, conn *bun.DB) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var users *db.List[db.User]
|
|
if ok := db.WithReadTx(s, w, r, conn, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
|
var err error
|
|
// Get users with their roles
|
|
users, err = db.GetUsersWithRoles(ctx, tx, nil)
|
|
if err != nil {
|
|
return false, errors.Wrap(err, "db.GetUsersWithRoles")
|
|
}
|
|
return true, nil
|
|
}); !ok {
|
|
return
|
|
}
|
|
renderSafely(adminview.UserList(users), s, r, w)
|
|
})
|
|
}
|