From dfc5587608c3d8de11b3c88c204912efcc1c1245 Mon Sep 17 00:00:00 2001 From: LandaMm Date: Sat, 31 May 2025 17:58:31 +0200 Subject: [PATCH] feat: get single api service route --- internal/admin/routes.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/internal/admin/routes.go b/internal/admin/routes.go index b2f153d..8c6cb6b 100644 --- a/internal/admin/routes.go +++ b/internal/admin/routes.go @@ -11,6 +11,7 @@ import ( "gitea.local/admin/hspguard/internal/util" "gitea.local/admin/hspguard/internal/web" "github.com/go-chi/chi/v5" + "github.com/google/uuid" "github.com/jackc/pgx/v5/pgtype" ) @@ -33,6 +34,7 @@ func (h *AdminHandler) RegisterRoutes(router chi.Router) { r.Use(authMiddleware.Runner, adminMiddleware.Runner) r.Get("/api-services", h.GetApiServices) + r.Get("/api-services/{id}", h.GetApiService) r.Post("/api-services", h.AddApiService) }) } @@ -156,3 +158,24 @@ func (h *AdminHandler) AddApiService(w http.ResponseWriter, r *http.Request) { web.Error(w, "failed to encode response", http.StatusInternalServerError) } } + +func (h *AdminHandler) GetApiService(w http.ResponseWriter, r *http.Request) { + serviceId := chi.URLParam(r, "id") + parsed, err := uuid.Parse(serviceId) + if err != nil { + web.Error(w, "service id provided is not a valid uuid", http.StatusBadRequest) + return + } + + service, err := h.repo.GetApiServiceId(r.Context(), parsed) + if err != nil { + web.Error(w, "service with provided id not found", http.StatusNotFound) + return + } + + encoder := json.NewEncoder(w) + + if err := encoder.Encode(NewApiServiceDTO(service)); err != nil { + web.Error(w, "failed to encode response", http.StatusInternalServerError) + } +}