Files
VLoop/internal/video/video_handler.go

89 lines
1.9 KiB
Go

package video
import (
"time"
"github.com/gin-gonic/gin"
)
type VideoHandler struct {
service *VideoService
}
func NewVideoHandler(service *VideoService) *VideoHandler {
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
}
uidValue, exists := c.Get("accountID")
if !exists {
c.JSON(400, gin.H{"error": "accountID not found"})
return
}
authorID, ok := uidValue.(uint)
if !ok {
c.JSON(400, gin.H{"error": "accountID has invalid type"})
return
}
video := &Video{
AuthorID: authorID,
Title: req.Title,
Description: req.Description,
PlayURL: req.PlayURL,
CreateTime: time.Now(),
}
if err := vh.service.Publish(c.Request.Context(), video); err != nil {
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
}
videos, err := vh.service.ListByAuthorID(c.Request.Context(), req.AuthorID)
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
}
video, err := vh.service.GetDetail(c.Request.Context(), req.ID)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
c.JSON(200, video)
}