feat: get single user endpoint

This commit is contained in:
2025-06-04 19:32:56 +02:00
parent 426b70a1de
commit 0dcef81b59
2 changed files with 24 additions and 0 deletions

View File

@ -33,5 +33,6 @@ func (h *AdminHandler) RegisterRoutes(router chi.Router) {
r.Patch("/api-services/toggle/{id}", h.ToggleApiService) r.Patch("/api-services/toggle/{id}", h.ToggleApiService)
r.Get("/users", h.GetUsers) r.Get("/users", h.GetUsers)
r.Get("/users/{id}", h.GetUser)
}) })
} }

View File

@ -8,6 +8,8 @@ import (
"gitea.local/admin/hspguard/internal/repository" "gitea.local/admin/hspguard/internal/repository"
"gitea.local/admin/hspguard/internal/types" "gitea.local/admin/hspguard/internal/types"
"gitea.local/admin/hspguard/internal/web" "gitea.local/admin/hspguard/internal/web"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
) )
func NewUserDTO(row *repository.User) types.UserDTO { func NewUserDTO(row *repository.User) types.UserDTO {
@ -52,3 +54,24 @@ func (h *AdminHandler) GetUsers(w http.ResponseWriter, r *http.Request) {
web.Error(w, "failed to send response", http.StatusInternalServerError) web.Error(w, "failed to send response", http.StatusInternalServerError)
} }
} }
func (h *AdminHandler) GetUser(w http.ResponseWriter, r *http.Request) {
userId := chi.URLParam(r, "id")
parsed, err := uuid.Parse(userId)
if err != nil {
web.Error(w, "user id provided is not a valid uuid", http.StatusBadRequest)
return
}
user, err := h.repo.FindUserId(r.Context(), parsed)
if err != nil {
web.Error(w, "user with provided id not found", http.StatusNotFound)
return
}
encoder := json.NewEncoder(w)
if err := encoder.Encode(NewUserDTO(&user)); err != nil {
web.Error(w, "failed to encode user dto", http.StatusInternalServerError)
}
}