web
This commit is contained in:
243
server/internal/handler/livestream.go
Normal file
243
server/internal/handler/livestream.go
Normal file
@@ -0,0 +1,243 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"dianshang/internal/model"
|
||||
"dianshang/internal/service"
|
||||
"dianshang/pkg/response"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type LiveStreamHandler struct {
|
||||
liveStreamService service.LiveStreamService
|
||||
}
|
||||
|
||||
func NewLiveStreamHandler(liveStreamService service.LiveStreamService) *LiveStreamHandler {
|
||||
return &LiveStreamHandler{
|
||||
liveStreamService: liveStreamService,
|
||||
}
|
||||
}
|
||||
|
||||
// GetLiveStreamList 获取投流源列表
|
||||
func (h *LiveStreamHandler) GetLiveStreamList(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||||
title := c.Query("title")
|
||||
platform := c.Query("platform")
|
||||
|
||||
var status *int
|
||||
if statusStr := c.Query("status"); statusStr != "" {
|
||||
statusVal, err := strconv.Atoi(statusStr)
|
||||
if err == nil {
|
||||
status = &statusVal
|
||||
}
|
||||
}
|
||||
|
||||
streams, total, err := h.liveStreamService.GetLiveStreamList(page, pageSize, title, platform, status)
|
||||
if err != nil {
|
||||
response.Error(c, response.ERROR)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, gin.H{
|
||||
"list": streams,
|
||||
"total": total,
|
||||
"page": page,
|
||||
"size": pageSize,
|
||||
})
|
||||
}
|
||||
|
||||
// GetLiveStreamDetail 获取投流源详情
|
||||
func (h *LiveStreamHandler) GetLiveStreamDetail(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, "投流源ID格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
stream, err := h.liveStreamService.GetLiveStreamByID(uint(id))
|
||||
if err != nil {
|
||||
response.Error(c, response.ERROR)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, stream)
|
||||
}
|
||||
|
||||
// GetActiveLiveStreams 获取启用的投流源(前台接口)
|
||||
func (h *LiveStreamHandler) GetActiveLiveStreams(c *gin.Context) {
|
||||
streams, err := h.liveStreamService.GetActiveLiveStreams()
|
||||
if err != nil {
|
||||
response.Error(c, response.ERROR)
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, streams)
|
||||
}
|
||||
|
||||
// CreateLiveStream 创建投流源
|
||||
func (h *LiveStreamHandler) CreateLiveStream(c *gin.Context) {
|
||||
var req struct {
|
||||
Title string `json:"title" binding:"required"`
|
||||
Platform string `json:"platform" binding:"required"`
|
||||
StreamURL string `json:"stream_url" binding:"required"`
|
||||
CoverImage string `json:"cover_image"`
|
||||
Description string `json:"description"`
|
||||
Status int `json:"status"`
|
||||
Sort int `json:"sort"`
|
||||
StartTime *time.Time `json:"start_time"`
|
||||
EndTime *time.Time `json:"end_time"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
stream := &model.LiveStream{
|
||||
Title: req.Title,
|
||||
Platform: req.Platform,
|
||||
StreamURL: req.StreamURL,
|
||||
CoverImage: req.CoverImage,
|
||||
Description: req.Description,
|
||||
Status: req.Status,
|
||||
Sort: req.Sort,
|
||||
StartTime: req.StartTime,
|
||||
EndTime: req.EndTime,
|
||||
}
|
||||
|
||||
if err := h.liveStreamService.CreateLiveStream(stream); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, stream)
|
||||
}
|
||||
|
||||
// UpdateLiveStream 更新投流源
|
||||
func (h *LiveStreamHandler) UpdateLiveStream(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, "投流源ID格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Title string `json:"title"`
|
||||
Platform string `json:"platform"`
|
||||
StreamURL string `json:"stream_url"`
|
||||
CoverImage string `json:"cover_image"`
|
||||
Description string `json:"description"`
|
||||
Status int `json:"status"`
|
||||
Sort int `json:"sort"`
|
||||
StartTime *time.Time `json:"start_time"`
|
||||
EndTime *time.Time `json:"end_time"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
stream := &model.LiveStream{
|
||||
Title: req.Title,
|
||||
Platform: req.Platform,
|
||||
StreamURL: req.StreamURL,
|
||||
CoverImage: req.CoverImage,
|
||||
Description: req.Description,
|
||||
Status: req.Status,
|
||||
Sort: req.Sort,
|
||||
StartTime: req.StartTime,
|
||||
EndTime: req.EndTime,
|
||||
}
|
||||
|
||||
if err := h.liveStreamService.UpdateLiveStream(uint(id), stream); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, stream)
|
||||
}
|
||||
|
||||
// UpdateLiveStreamStatus 更新投流源状态
|
||||
func (h *LiveStreamHandler) UpdateLiveStreamStatus(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, "投流源ID格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Status int `json:"status" binding:"required"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.liveStreamService.UpdateLiveStreamStatus(uint(id), req.Status); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
// DeleteLiveStream 删除投流源
|
||||
func (h *LiveStreamHandler) DeleteLiveStream(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, "投流源ID格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.liveStreamService.DeleteLiveStream(uint(id)); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
// BatchDeleteLiveStreams 批量删除投流源
|
||||
func (h *LiveStreamHandler) BatchDeleteLiveStreams(c *gin.Context) {
|
||||
var req struct {
|
||||
IDs []uint `json:"ids" binding:"required"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.liveStreamService.BatchDeleteLiveStreams(req.IDs); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, nil)
|
||||
}
|
||||
|
||||
// IncrementViewCount 增加观看次数
|
||||
func (h *LiveStreamHandler) IncrementViewCount(c *gin.Context) {
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 32)
|
||||
if err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR_INVALID_PARAMS, "投流源ID格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.liveStreamService.IncrementViewCount(uint(id)); err != nil {
|
||||
response.ErrorWithMessage(c, response.ERROR, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, nil)
|
||||
}
|
||||
Reference in New Issue
Block a user