package api import ( "errors" "go_oxspeak_server/models" "go_oxspeak_server/network/http/handler" "net/http" "github.com/gin-gonic/gin" "gorm.io/gorm" ) type ChannelHandler struct { *handler.Handler } func NewChannelHandler(h *handler.Handler) *ChannelHandler { return &ChannelHandler{h} } func (h *ChannelHandler) RegisterRoutes(rg *gin.RouterGroup) { channel := rg.Group("/channel") channel.GET("/", h.getChannels) channel.GET("/:id/", h.getChannel) channel.POST("/", h.addChannel) channel.PUT("/:id/", h.updateChannel) channel.DELETE("/:id/", h.deleteChannel) } func (h *ChannelHandler) getChannels(c *gin.Context) { var channels []models.Channel result := h.DB.Find(&channels) if result.Error != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()}) return } c.JSON(http.StatusOK, channels) } func (h *ChannelHandler) getChannel(c *gin.Context) { id := c.Param("id") var channel models.Channel result := h.DB.First(&channel, "id = ?", id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { c.JSON(http.StatusNotFound, gin.H{"error": "Channel not found"}) return } c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()}) return } c.JSON(http.StatusOK, channel) } func (h *ChannelHandler) addChannel(c *gin.Context) { var req CreateChannelRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } channel := models.Channel{ ServerID: req.ServerID, CategoryID: req.CategoryID, Type: req.Type, Name: req.Name, } if req.Position != nil { channel.Position = *req.Position } if err := h.DB.Create(&channel).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusCreated, channel) } func (h *ChannelHandler) updateChannel(c *gin.Context) { id := c.Param("id") var channel models.Channel result := h.DB.First(&channel, "id = ?", id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { c.JSON(http.StatusNotFound, gin.H{"error": "Channel not found"}) return } c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()}) return } var req UpdateChannelRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } if err := h.DB.Model(&channel).Updates(req).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update channel"}) return } c.JSON(http.StatusOK, channel) } func (h *ChannelHandler) deleteChannel(c *gin.Context) { id := c.Param("id") var channel models.Channel result := h.DB.First(&channel, "id = ?", id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { c.JSON(http.StatusNotFound, gin.H{"error": "Channel not found"}) return } c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error()}) return } if err := h.DB.Delete(&channel).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete channel"}) return } c.Status(http.StatusNoContent) }