mirror of
https://github.com/hanzo-fi/webhooks.git
synced 2026-10-30 20:32:55 +00:00
60 lines
2.3 KiB
Go
60 lines
2.3 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/hanzo-fi/go-libs/v2/api"
|
|
"github.com/hanzo-fi/go-libs/v2/logging"
|
|
webhooks "github.com/hanzo-fi/webhooks/pkg"
|
|
"github.com/hanzo-fi/webhooks/pkg/server/apierrors"
|
|
"github.com/hanzo-fi/webhooks/pkg/storage"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func (h *serverHandler) activateOneConfigHandle(w http.ResponseWriter, r *http.Request) {
|
|
id := chi.URLParam(r, PathParamId)
|
|
c, err := h.store.UpdateOneConfigActivation(r.Context(), id, true)
|
|
if err == nil || errors.Is(err, storage.ErrConfigNotModified) {
|
|
logging.FromContext(r.Context()).Debugf("PUT %s/%s%s", PathConfigs, id, PathActivate)
|
|
resp := api.BaseResponse[webhooks.Config]{
|
|
Data: &c,
|
|
}
|
|
if err := json.NewEncoder(w).Encode(resp); err != nil {
|
|
logging.FromContext(r.Context()).Errorf("json.Encoder.Encode: %s", err)
|
|
apierrors.ResponseError(w, r, err)
|
|
return
|
|
}
|
|
} else if errors.Is(err, storage.ErrConfigNotFound) {
|
|
logging.FromContext(r.Context()).Debugf("PUT %s/%s%s: %s", PathConfigs, id, PathActivate, storage.ErrConfigNotFound)
|
|
apierrors.ResponseError(w, r, apierrors.NewNotFoundError(storage.ErrConfigNotFound.Error()))
|
|
} else {
|
|
logging.FromContext(r.Context()).Errorf("PUT %s/%s%s: %s", PathConfigs, id, PathActivate, err)
|
|
apierrors.ResponseError(w, r, err)
|
|
}
|
|
}
|
|
|
|
func (h *serverHandler) deactivateOneConfigHandle(w http.ResponseWriter, r *http.Request) {
|
|
id := chi.URLParam(r, PathParamId)
|
|
c, err := h.store.UpdateOneConfigActivation(r.Context(), id, false)
|
|
if err == nil || errors.Is(err, storage.ErrConfigNotModified) {
|
|
logging.FromContext(r.Context()).Debugf("PUT %s/%s%s", PathConfigs, id, PathDeactivate)
|
|
resp := api.BaseResponse[webhooks.Config]{
|
|
Data: &c,
|
|
}
|
|
if err := json.NewEncoder(w).Encode(resp); err != nil {
|
|
logging.FromContext(r.Context()).Errorf("json.Encoder.Encode: %s", err)
|
|
apierrors.ResponseError(w, r, err)
|
|
return
|
|
}
|
|
} else if errors.Is(err, storage.ErrConfigNotFound) {
|
|
logging.FromContext(r.Context()).Debugf("PUT %s/%s%s: %s", PathConfigs, id, PathDeactivate, storage.ErrConfigNotFound)
|
|
apierrors.ResponseError(w, r, apierrors.NewNotFoundError(storage.ErrConfigNotFound.Error()))
|
|
} else {
|
|
logging.FromContext(r.Context()).Errorf("PUT %s/%s%s: %s", PathConfigs, id, PathDeactivate, err)
|
|
apierrors.ResponseError(w, r, err)
|
|
}
|
|
}
|