zone.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package handler
  2. import (
  3. "database/sql"
  4. "errors"
  5. "net/http"
  6. "strconv"
  7. "goflare/internal/database/queries"
  8. "github.com/labstack/echo/v4"
  9. )
  10. type ZoneHandler struct {
  11. q *queries.Queries
  12. }
  13. func NewZoneHandler(q *queries.Queries) *ZoneHandler {
  14. return &ZoneHandler{q: q}
  15. }
  16. type zoneRequest struct {
  17. ZoneID string `json:"zone_id"`
  18. Name string `json:"name"`
  19. APIKey string `json:"api_key"`
  20. }
  21. func (h *ZoneHandler) List(c echo.Context) error {
  22. zones, err := h.q.ListZones(c.Request().Context())
  23. if err != nil {
  24. return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to list zones"})
  25. }
  26. if zones == nil {
  27. zones = []queries.Zone{}
  28. }
  29. return c.JSON(http.StatusOK, zones)
  30. }
  31. func (h *ZoneHandler) Get(c echo.Context) error {
  32. id, err := strconv.ParseInt(c.Param("id"), 10, 64)
  33. if err != nil {
  34. return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid id"})
  35. }
  36. zone, err := h.q.GetZone(c.Request().Context(), id)
  37. if errors.Is(err, sql.ErrNoRows) {
  38. return c.JSON(http.StatusNotFound, map[string]string{"error": "zone not found"})
  39. }
  40. if err != nil {
  41. return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to get zone"})
  42. }
  43. return c.JSON(http.StatusOK, zone)
  44. }
  45. func (h *ZoneHandler) Create(c echo.Context) error {
  46. var req zoneRequest
  47. if err := c.Bind(&req); err != nil {
  48. return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid request body"})
  49. }
  50. if req.ZoneID == "" || req.Name == "" || req.APIKey == "" {
  51. return c.JSON(http.StatusBadRequest, map[string]string{"error": "zone_id, name, and api_key required"})
  52. }
  53. zone, err := h.q.CreateZone(c.Request().Context(), queries.CreateZoneParams{
  54. ZoneID: req.ZoneID,
  55. Name: req.Name,
  56. ApiKey: req.APIKey,
  57. })
  58. if err != nil {
  59. return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to create zone"})
  60. }
  61. return c.JSON(http.StatusCreated, zone)
  62. }
  63. func (h *ZoneHandler) Update(c echo.Context) error {
  64. id, err := strconv.ParseInt(c.Param("id"), 10, 64)
  65. if err != nil {
  66. return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid id"})
  67. }
  68. var req zoneRequest
  69. if err := c.Bind(&req); err != nil {
  70. return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid request body"})
  71. }
  72. if req.ZoneID == "" || req.Name == "" || req.APIKey == "" {
  73. return c.JSON(http.StatusBadRequest, map[string]string{"error": "zone_id, name, and api_key required"})
  74. }
  75. zone, err := h.q.UpdateZone(c.Request().Context(), queries.UpdateZoneParams{
  76. ID: id,
  77. ZoneID: req.ZoneID,
  78. Name: req.Name,
  79. ApiKey: req.APIKey,
  80. })
  81. if errors.Is(err, sql.ErrNoRows) {
  82. return c.JSON(http.StatusNotFound, map[string]string{"error": "zone not found"})
  83. }
  84. if err != nil {
  85. return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to update zone"})
  86. }
  87. return c.JSON(http.StatusOK, zone)
  88. }
  89. func (h *ZoneHandler) Delete(c echo.Context) error {
  90. id, err := strconv.ParseInt(c.Param("id"), 10, 64)
  91. if err != nil {
  92. return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid id"})
  93. }
  94. if err := h.q.DeleteZone(c.Request().Context(), id); err != nil {
  95. return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to delete zone"})
  96. }
  97. return c.JSON(http.StatusOK, map[string]string{"message": "zone deleted"})
  98. }