From 0dcef81b59c19e226807e1d64bbd7dd98e5cf609 Mon Sep 17 00:00:00 2001 From: LandaMm Date: Wed, 4 Jun 2025 19:32:56 +0200 Subject: [PATCH] feat: get single user endpoint --- internal/admin/routes.go | 1 + internal/admin/users.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/internal/admin/routes.go b/internal/admin/routes.go index 8b0e686..106b853 100644 --- a/internal/admin/routes.go +++ b/internal/admin/routes.go @@ -33,5 +33,6 @@ func (h *AdminHandler) RegisterRoutes(router chi.Router) { r.Patch("/api-services/toggle/{id}", h.ToggleApiService) r.Get("/users", h.GetUsers) + r.Get("/users/{id}", h.GetUser) }) } diff --git a/internal/admin/users.go b/internal/admin/users.go index 4af7b4c..9372da4 100644 --- a/internal/admin/users.go +++ b/internal/admin/users.go @@ -8,6 +8,8 @@ import ( "gitea.local/admin/hspguard/internal/repository" "gitea.local/admin/hspguard/internal/types" "gitea.local/admin/hspguard/internal/web" + "github.com/go-chi/chi/v5" + "github.com/google/uuid" ) 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) } } + +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) + } +}