76 lines
2.2 KiB
Go
76 lines
2.2 KiB
Go
package modules
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/lineup/app-server/internal/wkapi"
|
|
)
|
|
|
|
// ChannelModule handles channel creation and query.
|
|
type ChannelModule struct {
|
|
wk *wkapi.Client
|
|
}
|
|
|
|
// NewChannelModule creates a new ChannelModule.
|
|
func NewChannelModule(wk *wkapi.Client) *ChannelModule {
|
|
return &ChannelModule{wk: wk}
|
|
}
|
|
|
|
// RegisterRoutes registers channel routes.
|
|
func (m *ChannelModule) RegisterRoutes(r *gin.Engine) {
|
|
r.POST("/channels", m.handleCreate)
|
|
r.GET("/channels/:channel_id", m.handleGet)
|
|
r.POST("/channels/subscriber_add", m.handleSubscriberAdd)
|
|
}
|
|
|
|
type createChannelRequest struct {
|
|
ChannelID string `json:"channel_id" binding:"required"`
|
|
ChannelType uint8 `json:"channel_type" binding:"required"`
|
|
Subscribers []string `json:"subscribers"`
|
|
}
|
|
|
|
func (m *ChannelModule) handleCreate(c *gin.Context) {
|
|
var req createChannelRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误: channel_id和channel_type必填"})
|
|
return
|
|
}
|
|
if err := m.wk.UpsertChannel(req.ChannelID, req.ChannelType, req.Subscribers); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "创建频道失败: " + err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"channel_id": req.ChannelID,
|
|
"channel_type": req.ChannelType,
|
|
})
|
|
}
|
|
|
|
func (m *ChannelModule) handleGet(c *gin.Context) {
|
|
channelID := c.Param("channel_id")
|
|
_ = channelID
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"channel_id": channelID,
|
|
"channel_type": 2,
|
|
})
|
|
}
|
|
|
|
type subscriberAddRequest struct {
|
|
ChannelID string `json:"channel_id" binding:"required"`
|
|
ChannelType uint8 `json:"channel_type" binding:"required"`
|
|
UIDs []string `json:"uids" binding:"required"`
|
|
}
|
|
|
|
func (m *ChannelModule) handleSubscriberAdd(c *gin.Context) {
|
|
var req subscriberAddRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误: channel_id, channel_type, uids必填"})
|
|
return
|
|
}
|
|
if err := m.wk.SubscriberAdd(req.ChannelID, req.ChannelType, req.UIDs); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "添加订阅者失败: " + err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
|
}
|