coai/channel/controller.go
2023-12-02 20:54:35 +08:00

92 lines
1.8 KiB
Go

package channel
import (
"chat/utils"
"github.com/gin-gonic/gin"
"net/http"
)
func DeleteChannel(c *gin.Context) {
id := c.Param("id")
state := ManagerInstance.DeleteChannel(utils.ParseInt(id))
c.JSON(http.StatusOK, gin.H{
"status": state == nil,
"error": utils.GetError(state),
})
}
func ActivateChannel(c *gin.Context) {
id := c.Param("id")
state := ManagerInstance.ActivateChannel(utils.ParseInt(id))
c.JSON(http.StatusOK, gin.H{
"status": state == nil,
"error": utils.GetError(state),
})
}
func DeactivateChannel(c *gin.Context) {
id := c.Param("id")
state := ManagerInstance.DeactivateChannel(utils.ParseInt(id))
c.JSON(http.StatusOK, gin.H{
"status": state == nil,
"error": utils.GetError(state),
})
}
func GetChannelList(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"status": true,
"data": ManagerInstance.Sequence,
})
}
func GetChannel(c *gin.Context) {
id := c.Param("id")
channel := ManagerInstance.Sequence.GetChannelById(utils.ParseInt(id))
c.JSON(http.StatusOK, gin.H{
"status": channel != nil,
"data": channel,
})
}
func CreateChannel(c *gin.Context) {
var channel Channel
if err := c.ShouldBindJSON(&channel); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": false,
"error": err.Error(),
})
return
}
state := ManagerInstance.CreateChannel(&channel)
c.JSON(http.StatusOK, gin.H{
"status": state == nil,
"error": utils.GetError(state),
})
}
func UpdateChannel(c *gin.Context) {
var channel Channel
if err := c.ShouldBindJSON(&channel); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": false,
"error": err.Error(),
})
return
}
id := c.Param("id")
channel.Id = utils.ParseInt(id)
state := ManagerInstance.UpdateChannel(channel.Id, &channel)
c.JSON(http.StatusOK, gin.H{
"status": state == nil,
"error": utils.GetError(state),
})
}