| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- 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"})
- }
- type reorderRequest struct {
- IDs []int64 `json:"ids"`
- }
- func (h *IpProviderHandler) Reorder(c echo.Context) error {
- var req reorderRequest
- if err := c.Bind(&req); err != nil {
- return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid request body"})
- }
- if len(req.IDs) == 0 {
- return c.JSON(http.StatusBadRequest, map[string]string{"error": "ids is required"})
- }
- ctx := c.Request().Context()
- for i, id := range req.IDs {
- provider, err := h.q.GetIpProvider(ctx, id)
- if err != nil {
- return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to reorder IP providers"})
- }
- _, err = h.q.UpdateIpProvider(ctx, queries.UpdateIpProviderParams{
- ID: id,
- Url: provider.Url,
- Name: provider.Name,
- Enabled: provider.Enabled,
- Priority: int64(i),
- })
- if err != nil {
- return c.JSON(http.StatusInternalServerError, map[string]string{"error": "failed to reorder IP providers"})
- }
- }
- return c.JSON(http.StatusOK, map[string]string{"message": "IP providers reordered"})
- }
|