init
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
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 {
|
||||
@@ -26,26 +29,102 @@ func (h *ChannelHandler) RegisterRoutes(rg *gin.RouterGroup) {
|
||||
}
|
||||
|
||||
func (h *ChannelHandler) getChannels(c *gin.Context) {
|
||||
var users []models.User
|
||||
h.DB.Find(&users)
|
||||
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) {
|
||||
var user models.User
|
||||
h.DB.Find(&user)
|
||||
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) {
|
||||
var user models.User
|
||||
h.DB.Find(&user)
|
||||
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) {
|
||||
var user models.User
|
||||
h.DB.Find(&user)
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user