| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- 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"})
- }
|