2025-12-08 20:35:56 +08:00
|
|
|
package video
|
2025-12-07 16:45:00 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type VideoHandler struct {
|
2025-12-08 20:35:56 +08:00
|
|
|
service *VideoService
|
2025-12-07 16:45:00 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-08 20:35:56 +08:00
|
|
|
func NewVideoHandler(service *VideoService) *VideoHandler {
|
2025-12-07 16:45:00 +08:00
|
|
|
return &VideoHandler{service: service}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type PublishVideoRequest struct {
|
|
|
|
|
Title string `json:"title"`
|
|
|
|
|
Description string `json:"description"`
|
|
|
|
|
PlayURL string `json:"play_url"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ListByAuthorIDRequest struct {
|
|
|
|
|
AuthorID uint `json:"author_id"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type GetDetailRequest struct {
|
|
|
|
|
ID uint `json:"id"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (vh *VideoHandler) PublishVideo(c *gin.Context) {
|
|
|
|
|
var req PublishVideoRequest
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
c.JSON(400, gin.H{"error": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-12-09 13:53:36 +08:00
|
|
|
|
2025-12-07 17:39:03 +08:00
|
|
|
uidValue, exists := c.Get("accountID")
|
2025-12-07 16:45:00 +08:00
|
|
|
if !exists {
|
2025-12-07 17:39:03 +08:00
|
|
|
c.JSON(400, gin.H{"error": "accountID not found"})
|
2025-12-07 16:45:00 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
authorID, ok := uidValue.(uint)
|
|
|
|
|
if !ok {
|
2025-12-07 17:39:03 +08:00
|
|
|
c.JSON(400, gin.H{"error": "accountID has invalid type"})
|
2025-12-07 16:45:00 +08:00
|
|
|
return
|
|
|
|
|
}
|
2025-12-08 20:35:56 +08:00
|
|
|
video := &Video{
|
2025-12-07 16:45:00 +08:00
|
|
|
AuthorID: authorID,
|
|
|
|
|
Title: req.Title,
|
|
|
|
|
Description: req.Description,
|
|
|
|
|
PlayURL: req.PlayURL,
|
|
|
|
|
CreateTime: time.Now(),
|
|
|
|
|
}
|
2025-12-09 13:53:36 +08:00
|
|
|
if err := vh.service.Publish(c.Request.Context(), video); err != nil {
|
2025-12-07 16:45:00 +08:00
|
|
|
c.JSON(400, gin.H{"error": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(200, video)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (vh *VideoHandler) ListByAuthorID(c *gin.Context) {
|
|
|
|
|
var req ListByAuthorIDRequest
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
c.JSON(400, gin.H{"error": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-12-09 13:53:36 +08:00
|
|
|
videos, err := vh.service.ListByAuthorID(c.Request.Context(), req.AuthorID)
|
2025-12-07 16:45:00 +08:00
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(400, gin.H{"error": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(200, videos)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (vh *VideoHandler) GetDetail(c *gin.Context) {
|
|
|
|
|
var req GetDetailRequest
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
c.JSON(400, gin.H{"error": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-12-09 13:53:36 +08:00
|
|
|
video, err := vh.service.GetDetail(c.Request.Context(), req.ID)
|
2025-12-07 16:45:00 +08:00
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(400, gin.H{"error": err.Error()})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.JSON(200, video)
|
|
|
|
|
}
|