feat: 添加了删除视频功能并添加了一些边界检查
This commit is contained in:
@@ -3,14 +3,14 @@ package video
|
||||
import "time"
|
||||
|
||||
type Video struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
AuthorID uint `gorm:"index;not null"`
|
||||
Username string `gorm:"type:varchar(255);not null"`
|
||||
Title string `gorm:"type:varchar(255);not null"`
|
||||
Description string `gorm:"type:varchar(255);"`
|
||||
PlayURL string `gorm:"type:varchar(255);not null"`
|
||||
CoverURL string `gorm:"type:varchar(255);not null"`
|
||||
CreateTime time.Time `gorm:"autoCreateTime"`
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
AuthorID uint `gorm:"index;not null" json:"author_id"`
|
||||
Username string `gorm:"type:varchar(255);not null" json:"username"`
|
||||
Title string `gorm:"type:varchar(255);not null" json:"title"`
|
||||
Description string `gorm:"type:varchar(255);" json:"description,omitempty"`
|
||||
PlayURL string `gorm:"type:varchar(255);not null" json:"play_url"`
|
||||
CoverURL string `gorm:"type:varchar(255);not null" json:"cover_url"`
|
||||
CreateTime time.Time `gorm:"autoCreateTime" json:"create_time"`
|
||||
LikesCount int64 `gorm:"column:likes_count;not null;default:0" json:"likes_count"`
|
||||
}
|
||||
|
||||
@@ -21,6 +21,10 @@ type PublishVideoRequest struct {
|
||||
CoverURL string `json:"cover_url"`
|
||||
}
|
||||
|
||||
type DeleteVideoRequest struct {
|
||||
ID uint `json:"id"`
|
||||
}
|
||||
|
||||
type ListByAuthorIDRequest struct {
|
||||
AuthorID uint `json:"author_id"`
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -21,6 +21,13 @@ func (vr *VideoRepository) CreateVideo(ctx context.Context, video *Video) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func (vr *VideoRepository) DeleteVideo(ctx context.Context, id uint) error {
|
||||
if err := vr.db.WithContext(ctx).Delete(&Video{}, id).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (vr *VideoRepository) ListByAuthorID(ctx context.Context, authorID int64) ([]Video, error) {
|
||||
var videos []Video
|
||||
if err := vr.db.WithContext(ctx).
|
||||
@@ -49,3 +56,14 @@ func (vr *VideoRepository) UpdateLikesCount(ctx context.Context, id uint, likesC
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (vr *VideoRepository) IsExist(ctx context.Context, id uint) (bool, error) {
|
||||
var video Video
|
||||
if err := vr.db.WithContext(ctx).First(&video, id).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
@@ -29,6 +29,20 @@ func (vs *VideoService) Publish(ctx context.Context, video *Video) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (vs *VideoService) Delete(ctx context.Context, id uint, authorID uint) error {
|
||||
video, err := vs.repo.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if video.AuthorID != authorID {
|
||||
return errors.New("unauthorized")
|
||||
}
|
||||
if err := vs.repo.DeleteVideo(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (vs *VideoService) ListByAuthorID(ctx context.Context, authorID uint) ([]Video, error) {
|
||||
videos, err := vs.repo.ListByAuthorID(ctx, int64(authorID))
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user