hydra-webauthn/internal/route/api/list_users.go

43 lines
999 B
Go

package api
import (
"net/http"
"forge.cadoles.com/wpetit/hydra-webauthn/internal/storage"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/api"
"gitlab.com/wpetit/goweb/logger"
"gitlab.com/wpetit/goweb/middleware/container"
)
type ListUsersResponse struct {
Users []storage.UserHeader `json:"users"`
}
func ListUsers(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctn := container.Must(ctx)
storageSrv := storage.Must(ctn)
users, err := storageSrv.User().ListUsers(ctx)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
logger.Warn(ctx, "could not list users", logger.E(errors.WithStack(err)))
api.ErrorResponse(w, http.StatusNotFound, api.ErrCodeNotFound, nil)
return
}
logger.Error(ctx, "could not get user", logger.E(errors.WithStack(err)))
api.ErrorResponse(w, http.StatusInternalServerError, api.ErrCodeUnknownError, nil)
return
}
response := ListUsersResponse{
Users: users,
}
api.DataResponse(w, http.StatusOK, response)
}