package handler import ( "database/sql" "errors" "net/http" "strconv" "goflare/internal/database/queries" "github.com/labstack/echo/v4" ) type ZoneHandler struct { q *queries.Queries } func NewZoneHandler(q *queries.Queries) *ZoneHandler { return &ZoneHandler{q: q} } type zoneRequest struct { ZoneID string `json:"zone_id"` Name string `json:"name"` APIKey string `json:"api_key"` } func (h *ZoneHandler) List(c echo.Context) error { zones, err := h.q.ListZones(c.Request().Context()) if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to list zones"}) } if zones == nil { zones = []queries.Zone{} } return c.JSON(http.StatusOK, zones) } func (h *ZoneHandler) 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"}) } zone, err := h.q.GetZone(c.Request().Context(), id) if errors.Is(err, sql.ErrNoRows) { return c.JSON(http.StatusNotFound, map[string]string{"error": "zone not found"}) } if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to get zone"}) } return c.JSON(http.StatusOK, zone) } func (h *ZoneHandler) Create(c echo.Context) error { var req zoneRequest if err := c.Bind(&req); err != nil { return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid request body"}) } if req.ZoneID == "" || req.Name == "" || req.APIKey == "" { return c.JSON(http.StatusBadRequest, map[string]string{"error": "zone_id, name, and api_key required"}) } zone, err := h.q.CreateZone(c.Request().Context(), queries.CreateZoneParams{ ZoneID: req.ZoneID, Name: req.Name, ApiKey: req.APIKey, }) if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to create zone"}) } return c.JSON(http.StatusCreated, zone) } func (h *ZoneHandler) 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 zoneRequest if err := c.Bind(&req); err != nil { return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid request body"}) } if req.ZoneID == "" || req.Name == "" || req.APIKey == "" { return c.JSON(http.StatusBadRequest, map[string]string{"error": "zone_id, name, and api_key required"}) } zone, err := h.q.UpdateZone(c.Request().Context(), queries.UpdateZoneParams{ ID: id, ZoneID: req.ZoneID, Name: req.Name, ApiKey: req.APIKey, }) if errors.Is(err, sql.ErrNoRows) { return c.JSON(http.StatusNotFound, map[string]string{"error": "zone not found"}) } if err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to update zone"}) } return c.JSON(http.StatusOK, zone) } func (h *ZoneHandler) 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.DeleteZone(c.Request().Context(), id); err != nil { return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to delete zone"}) } return c.JSON(http.StatusOK, map[string]string{"message": "zone deleted"}) }