feat: 添加了删除视频功能并添加了一些边界检查

This commit is contained in:
Leon
2025-12-16 18:51:17 +08:00
parent 7883a7427d
commit ef47d5e2de
4 changed files with 68 additions and 18 deletions

View File

@@ -4,6 +4,7 @@ import (
"time"
"feedsystem_video_go/internal/account"
"feedsystem_video_go/internal/middleware"
"github.com/gin-gonic/gin"
)
@@ -24,23 +25,18 @@ func (vh *VideoHandler) PublishVideo(c *gin.Context) {
return
}
uidValue, exists := c.Get("accountID")
if !exists {
c.JSON(400, gin.H{"error": "accountID not found"})
authorId, err := middleware.GetAccountID(c)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
authorID, ok := uidValue.(uint)
if !ok {
c.JSON(400, gin.H{"error": "accountID has invalid type"})
return
}
user, err := vh.accountService.FindByID(c.Request.Context(), authorID)
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,
AuthorID: authorId,
Username: user.Username,
Title: req.Title,
Description: req.Description,
@@ -55,6 +51,24 @@ func (vh *VideoHandler) PublishVideo(c *gin.Context) {
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 {