package handler import ( "database/sql" "errors" "net/http" "strconv" "goflare/internal/database/queries" "github.com/labstack/echo/v4" ) type IpProviderHandler struct { q *queries.Queries } func NewIpProviderHandler(q *queries.Queries) *IpProviderHandler { return &IpProviderHandler{q: q} } type ipProviderRequest struct { URL string `json:"url"` Name string `json:"name"` Enabled int64 `json:"enabled"` Priority int64 `json:"priority"` } func (h *IpProviderHandler) List(c echo.Context) error { providers, err := h.q.ListIpProviders(c.Request().Context()) if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to list IP providers"}) } if providers == nil { providers = []queries.IpProvider{} } return c.JSON(http.StatusOK, providers) } func (h *IpProviderHandler) Get(c echo.Context) error { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid id"}) } provider, err := h.q.GetIpProvider(c.Request().Context(), id) if errors.Is(err, sql.ErrNoRows) { return c.JSON(http.StatusNotFound, map[string]string{"error": "IP provider not found"}) } if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to get IP provider"}) } return c.JSON(http.StatusOK, provider) } func (h *IpProviderHandler) Create(c echo.Context) error { var req ipProviderRequest if err := c.Bind(&req); err != nil { return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid request body"}) } if req.URL == "" { return c.JSON(http.StatusBadRequest, map[string]string{"error": "url is required"}) } provider, err := h.q.CreateIpProvider(c.Request().Context(), queries.CreateIpProviderParams{ Url: req.URL, Name: req.Name, Enabled: req.Enabled, Priority: req.Priority, }) if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to create IP provider"}) } return c.JSON(http.StatusCreated, provider) } func (h *IpProviderHandler) Update(c echo.Context) error { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid id"}) } var req ipProviderRequest if err := c.Bind(&req); err != nil { return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid request body"}) } if req.URL == "" { return c.JSON(http.StatusBadRequest, map[string]string{"error": "url is required"}) } provider, err := h.q.UpdateIpProvider(c.Request().Context(), queries.UpdateIpProviderParams{ ID: id, Url: req.URL, Name: req.Name, Enabled: req.Enabled, Priority: req.Priority, }) if errors.Is(err, sql.ErrNoRows) { return c.JSON(http.StatusNotFound, map[string]string{"error": "IP provider not found"}) } if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to update IP provider"}) } return c.JSON(http.StatusOK, provider) } func (h *IpProviderHandler) Delete(c echo.Context) error { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid id"}) } if err := h.q.DeleteIpProvider(c.Request().Context(), id); err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to delete IP provider"}) } return c.JSON(http.StatusOK, map[string]string{"message": "IP provider deleted"}) }