|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/http" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/gin-gonic/gin" |
| 9 | + "gorm.io/gorm" |
| 10 | + |
| 11 | + "github.com/Wikid82/charon/backend/internal/models" |
| 12 | + "github.com/Wikid82/charon/backend/internal/services" |
| 13 | +) |
| 14 | + |
| 15 | +// ProxyGroupHandler handles CRUD operations for proxy groups. |
| 16 | +type ProxyGroupHandler struct { |
| 17 | + service *services.ProxyGroupService |
| 18 | + db *gorm.DB |
| 19 | +} |
| 20 | + |
| 21 | +// NewProxyGroupHandler creates a new ProxyGroupHandler. |
| 22 | +func NewProxyGroupHandler(db *gorm.DB) *ProxyGroupHandler { |
| 23 | + return &ProxyGroupHandler{ |
| 24 | + service: services.NewProxyGroupService(db), |
| 25 | + db: db, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// RegisterRoutes registers proxy group routes on the given router group. |
| 30 | +func (h *ProxyGroupHandler) RegisterRoutes(router *gin.RouterGroup) { |
| 31 | + router.GET("/proxy-groups", h.List) |
| 32 | + router.POST("/proxy-groups", h.Create) |
| 33 | + router.GET("/proxy-groups/:uuid", h.Get) |
| 34 | + router.PUT("/proxy-groups/:uuid", h.Update) |
| 35 | + router.DELETE("/proxy-groups/:uuid", h.Delete) |
| 36 | +} |
| 37 | + |
| 38 | +// List returns all proxy groups ordered by name. |
| 39 | +func (h *ProxyGroupHandler) List(c *gin.Context) { |
| 40 | + groups, err := h.service.List() |
| 41 | + if err != nil { |
| 42 | + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) |
| 43 | + return |
| 44 | + } |
| 45 | + c.JSON(http.StatusOK, groups) |
| 46 | +} |
| 47 | + |
| 48 | +// Create creates a new proxy group. |
| 49 | +func (h *ProxyGroupHandler) Create(c *gin.Context) { |
| 50 | + var payload struct { |
| 51 | + Name string `json:"name"` |
| 52 | + Description string `json:"description"` |
| 53 | + Color string `json:"color"` |
| 54 | + } |
| 55 | + if err := c.ShouldBindJSON(&payload); err != nil { |
| 56 | + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + group := models.ProxyGroup{ |
| 61 | + Name: strings.TrimSpace(payload.Name), |
| 62 | + Description: payload.Description, |
| 63 | + Color: payload.Color, |
| 64 | + } |
| 65 | + if group.Color == "" { |
| 66 | + group.Color = "#6366f1" |
| 67 | + } |
| 68 | + |
| 69 | + if err := h.service.Create(&group); err != nil { |
| 70 | + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 71 | + return |
| 72 | + } |
| 73 | + c.JSON(http.StatusCreated, group) |
| 74 | +} |
| 75 | + |
| 76 | +// Get returns a single proxy group by UUID, including host count. |
| 77 | +func (h *ProxyGroupHandler) Get(c *gin.Context) { |
| 78 | + group, err := h.service.GetByUUID(c.Param("uuid")) |
| 79 | + if err != nil { |
| 80 | + if errors.Is(err, services.ErrProxyGroupNotFound) { |
| 81 | + c.JSON(http.StatusNotFound, gin.H{"error": "proxy group not found"}) |
| 82 | + } else { |
| 83 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to retrieve proxy group"}) |
| 84 | + } |
| 85 | + return |
| 86 | + } |
| 87 | + count, err := h.service.GetHostCount(group.ID) |
| 88 | + if err != nil { |
| 89 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to retrieve host count"}) |
| 90 | + return |
| 91 | + } |
| 92 | + c.JSON(http.StatusOK, gin.H{ |
| 93 | + "uuid": group.UUID, |
| 94 | + "name": group.Name, |
| 95 | + "description": group.Description, |
| 96 | + "color": group.Color, |
| 97 | + "host_count": count, |
| 98 | + "created_at": group.CreatedAt, |
| 99 | + "updated_at": group.UpdatedAt, |
| 100 | + }) |
| 101 | +} |
| 102 | + |
| 103 | +// Update updates an existing proxy group by UUID. |
| 104 | +func (h *ProxyGroupHandler) Update(c *gin.Context) { |
| 105 | + group, err := h.service.GetByUUID(c.Param("uuid")) |
| 106 | + if err != nil { |
| 107 | + if errors.Is(err, services.ErrProxyGroupNotFound) { |
| 108 | + c.JSON(http.StatusNotFound, gin.H{"error": "proxy group not found"}) |
| 109 | + } else { |
| 110 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to retrieve proxy group"}) |
| 111 | + } |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + var payload struct { |
| 116 | + Name *string `json:"name"` |
| 117 | + Description *string `json:"description"` |
| 118 | + Color *string `json:"color"` |
| 119 | + } |
| 120 | + if err := c.ShouldBindJSON(&payload); err != nil { |
| 121 | + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + if payload.Name != nil { |
| 126 | + trimmed := strings.TrimSpace(*payload.Name) |
| 127 | + if trimmed == "" { |
| 128 | + c.JSON(http.StatusBadRequest, gin.H{"error": "name cannot be empty"}) |
| 129 | + return |
| 130 | + } |
| 131 | + group.Name = trimmed |
| 132 | + } |
| 133 | + if payload.Description != nil { |
| 134 | + group.Description = *payload.Description |
| 135 | + } |
| 136 | + if payload.Color != nil { |
| 137 | + group.Color = *payload.Color |
| 138 | + } |
| 139 | + |
| 140 | + if err := h.service.Update(group); err != nil { |
| 141 | + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) |
| 142 | + return |
| 143 | + } |
| 144 | + c.JSON(http.StatusOK, group) |
| 145 | +} |
| 146 | + |
| 147 | +// Delete removes a proxy group by UUID, clearing host assignments. |
| 148 | +func (h *ProxyGroupHandler) Delete(c *gin.Context) { |
| 149 | + group, err := h.service.GetByUUID(c.Param("uuid")) |
| 150 | + if err != nil { |
| 151 | + if errors.Is(err, services.ErrProxyGroupNotFound) { |
| 152 | + c.JSON(http.StatusNotFound, gin.H{"error": "proxy group not found"}) |
| 153 | + } else { |
| 154 | + c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to retrieve proxy group"}) |
| 155 | + } |
| 156 | + return |
| 157 | + } |
| 158 | + if err := h.service.Delete(group.ID); err != nil { |
| 159 | + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) |
| 160 | + return |
| 161 | + } |
| 162 | + c.Status(http.StatusNoContent) |
| 163 | +} |
0 commit comments