feat: toggle api service endpoint
This commit is contained in:
@ -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)
|
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)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user