Files
VLoop/backend/internal/video/video_handler.go
2025-12-25 22:40:20 +08:00

112 lines
2.8 KiB
Go

package video
import (
"time"
"feedsystem_video_go/internal/account"
"feedsystem_video_go/internal/middleware"
"github.com/gin-gonic/gin"
)
type VideoHandler struct {
service *VideoService
accountService *account.AccountService
}
func NewVideoHandler(service *VideoService, accountService *account.AccountService) *VideoHandler {
return &VideoHandler{service: service, accountService: accountService}
}
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
}
authorId, err := middleware.GetAccountID(c)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
user, err := vh.accountService.FindByID(c.Request.Context(), authorId)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
video := &Video{
AuthorID: authorId,
Username: user.Username,
Title: req.Title,
Description: req.Description,
PlayURL: req.PlayURL,
CoverURL: req.CoverURL,
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) DeleteVideo(c *gin.Context) {
var req DeleteVideoRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
authorId, err := middleware.GetAccountID(c)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if err := vh.service.Delete(c.Request.Context(), req.ID, authorId); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
c.JSON(200, gin.H{"message": "video deleted"})
}
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)
}
func (vh *VideoHandler) UpdateLikesCount(c *gin.Context) {
var req UpdateLikesCountRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if err := vh.service.UpdateLikesCount(c.Request.Context(), req.ID, req.LikesCount); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
c.JSON(200, gin.H{"message": "likes count updated"})
}