From 3bf08c5933cf59034d2daa98ad71b45145f2f678 Mon Sep 17 00:00:00 2001 From: LandaMm Date: Tue, 3 Jun 2025 00:04:36 +0200 Subject: [PATCH] feat: toggle api service endpoint --- internal/admin/apiservices.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/internal/admin/apiservices.go b/internal/admin/apiservices.go index 0ee98cd..fd0bf9e 100644 --- a/internal/admin/apiservices.go +++ b/internal/admin/apiservices.go @@ -283,3 +283,36 @@ func (h *AdminHandler) UpdateApiService(w http.ResponseWriter, r *http.Request) web.Error(w, "failed to send updated api service", http.StatusInternalServerError) } } + +func (h *AdminHandler) ToggleApiService(w http.ResponseWriter, r *http.Request) { + var err error + + serviceId := chi.URLParam(r, "id") + parsed, err := uuid.Parse(serviceId) + if err != nil { + web.Error(w, "provided service id is not valid", 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 + } + + if service.IsActive { + log.Println("INFO: Service is active. Deactivating...") + err = h.repo.DeactivateApiService(r.Context(), service.ClientID) + } else { + log.Println("INFO: Service is inactive. Activating...") + err = h.repo.ActivateApiService(r.Context(), service.ClientID) + } + + if err != nil { + log.Printf("ERR: Failed to toggle api service (cid: %s): %v\n", service.ClientID, err) + web.Error(w, "failed to toggle api service", http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusOK) +}