Files
go_oxspeak_server/network/http/web/api/channel.go
2025-11-17 01:06:03 +01:00

130 lines
3.1 KiB
Go

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) {
rg.GET("/", h.getChannels)
rg.GET("/:id/", h.getChannel)
rg.POST("/", h.addChannel)
rg.PUT("/:id/", h.updateChannel)
rg.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)
}