ip_provider.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 IpProviderHandler struct {
  11. q *queries.Queries
  12. }
  13. func NewIpProviderHandler(q *queries.Queries) *IpProviderHandler {
  14. return &IpProviderHandler{q: q}
  15. }
  16. type ipProviderRequest struct {
  17. URL string `json:"url"`
  18. Name string `json:"name"`
  19. Enabled int64 `json:"enabled"`
  20. Priority int64 `json:"priority"`
  21. }
  22. func (h *IpProviderHandler) List(c echo.Context) error {
  23. providers, err := h.q.ListIpProviders(c.Request().Context())
  24. if err != nil {
  25. return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to list IP providers"})
  26. }
  27. if providers == nil {
  28. providers = []queries.IpProvider{}
  29. }
  30. return c.JSON(http.StatusOK, providers)
  31. }
  32. func (h *IpProviderHandler) Get(c echo.Context) error {
  33. id, err := strconv.ParseInt(c.Param("id"), 10, 64)
  34. if err != nil {
  35. return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid id"})
  36. }
  37. provider, err := h.q.GetIpProvider(c.Request().Context(), id)
  38. if errors.Is(err, sql.ErrNoRows) {
  39. return c.JSON(http.StatusNotFound, map[string]string{"error": "IP provider not found"})
  40. }
  41. if err != nil {
  42. return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to get IP provider"})
  43. }
  44. return c.JSON(http.StatusOK, provider)
  45. }
  46. func (h *IpProviderHandler) Create(c echo.Context) error {
  47. var req ipProviderRequest
  48. if err := c.Bind(&req); err != nil {
  49. return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid request body"})
  50. }
  51. if req.URL == "" {
  52. return c.JSON(http.StatusBadRequest, map[string]string{"error": "url is required"})
  53. }
  54. provider, err := h.q.CreateIpProvider(c.Request().Context(), queries.CreateIpProviderParams{
  55. Url: req.URL,
  56. Name: req.Name,
  57. Enabled: req.Enabled,
  58. Priority: req.Priority,
  59. })
  60. if err != nil {
  61. return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to create IP provider"})
  62. }
  63. return c.JSON(http.StatusCreated, provider)
  64. }
  65. func (h *IpProviderHandler) Update(c echo.Context) error {
  66. id, err := strconv.ParseInt(c.Param("id"), 10, 64)
  67. if err != nil {
  68. return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid id"})
  69. }
  70. var req ipProviderRequest
  71. if err := c.Bind(&req); err != nil {
  72. return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid request body"})
  73. }
  74. if req.URL == "" {
  75. return c.JSON(http.StatusBadRequest, map[string]string{"error": "url is required"})
  76. }
  77. provider, err := h.q.UpdateIpProvider(c.Request().Context(), queries.UpdateIpProviderParams{
  78. ID: id,
  79. Url: req.URL,
  80. Name: req.Name,
  81. Enabled: req.Enabled,
  82. Priority: req.Priority,
  83. })
  84. if errors.Is(err, sql.ErrNoRows) {
  85. return c.JSON(http.StatusNotFound, map[string]string{"error": "IP provider not found"})
  86. }
  87. if err != nil {
  88. return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to update IP provider"})
  89. }
  90. return c.JSON(http.StatusOK, provider)
  91. }
  92. func (h *IpProviderHandler) Delete(c echo.Context) error {
  93. id, err := strconv.ParseInt(c.Param("id"), 10, 64)
  94. if err != nil {
  95. return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid id"})
  96. }
  97. if err := h.q.DeleteIpProvider(c.Request().Context(), id); err != nil {
  98. return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to delete IP provider"})
  99. }
  100. return c.JSON(http.StatusOK, map[string]string{"message": "IP provider deleted"})
  101. }
  102. type reorderRequest struct {
  103. IDs []int64 `json:"ids"`
  104. }
  105. func (h *IpProviderHandler) Reorder(c echo.Context) error {
  106. var req reorderRequest
  107. if err := c.Bind(&req); err != nil {
  108. return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid request body"})
  109. }
  110. if len(req.IDs) == 0 {
  111. return c.JSON(http.StatusBadRequest, map[string]string{"error": "ids is required"})
  112. }
  113. ctx := c.Request().Context()
  114. for i, id := range req.IDs {
  115. provider, err := h.q.GetIpProvider(ctx, id)
  116. if err != nil {
  117. return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to reorder IP providers"})
  118. }
  119. _, err = h.q.UpdateIpProvider(ctx, queries.UpdateIpProviderParams{
  120. ID: id,
  121. Url: provider.Url,
  122. Name: provider.Name,
  123. Enabled: provider.Enabled,
  124. Priority: int64(i),
  125. })
  126. if err != nil {
  127. return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to reorder IP providers"})
  128. }
  129. }
  130. return c.JSON(http.StatusOK, map[string]string{"message": "IP providers reordered"})
  131. }