45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.haelnorr.com/h/golib/hws"
|
|
"git.haelnorr.com/h/oslstats/internal/db"
|
|
"git.haelnorr.com/h/oslstats/internal/view/component/admin"
|
|
"github.com/pkg/errors"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
// AdminUsersList shows all users
|
|
func AdminUsersList(s *hws.Server, conn *bun.DB) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
tx, err := conn.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
throwInternalServiceError(s, w, r, "DB Transaction failed", errors.Wrap(err, "conn.BeginTx"))
|
|
return
|
|
}
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
// Get all users
|
|
pageOpts, err := pageOptsFromForm(r)
|
|
if err != nil {
|
|
throwBadRequest(s, w, r, "invalid form data", err)
|
|
return
|
|
}
|
|
users, err := db.GetUsers(ctx, tx, pageOpts)
|
|
if err != nil {
|
|
throwInternalServiceError(s, w, r, "Failed to load users", errors.Wrap(err, "db.GetUsers"))
|
|
return
|
|
}
|
|
|
|
_ = tx.Commit()
|
|
|
|
renderSafely(admin.UserList(users), s, r, w)
|
|
})
|
|
}
|