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

46 lines
1.1 KiB
Go
Raw Permalink Normal View History

2023-11-15 20:38:25 +01:00
package api
import (
"net/http"
"forge.cadoles.com/wpetit/hydra-webauthn/internal/storage"
"github.com/go-chi/chi"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/api"
"gitlab.com/wpetit/goweb/logger"
"gitlab.com/wpetit/goweb/middleware/container"
)
type GetUserResponse struct {
User *storage.User `json:"user"`
}
func GetUser(w http.ResponseWriter, r *http.Request) {
userID := chi.URLParam(r, "userID")
ctx := r.Context()
ctn := container.Must(ctx)
storageSrv := storage.Must(ctn)
user, err := storageSrv.User().FindUserByID(ctx, userID)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
logger.Warn(ctx, "could not find user", logger.E(errors.WithStack(err)), logger.F("userID", userID))
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 := GetUserResponse{
User: user,
}
api.DataResponse(w, http.StatusOK, response)
}