From ef47d5e2defc42dfb17d289fe00905fb0679f180 Mon Sep 17 00:00:00 2001 From: Leon <147289645+LeoninCS@users.noreply.github.com> Date: Tue, 16 Dec 2025 18:51:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E8=A7=86=E9=A2=91=E5=8A=9F=E8=83=BD=E5=B9=B6=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=BA=86=E4=B8=80=E4=BA=9B=E8=BE=B9=E7=95=8C=E6=A3=80?= =?UTF-8?q?=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/video/video_entity.go | 20 +++++++++++-------- internal/video/video_handler.go | 34 +++++++++++++++++++++++---------- internal/video/video_repo.go | 18 +++++++++++++++++ internal/video/video_service.go | 14 ++++++++++++++ 4 files changed, 68 insertions(+), 18 deletions(-) diff --git a/internal/video/video_entity.go b/internal/video/video_entity.go index ef9e03a..d3b19b0 100644 --- a/internal/video/video_entity.go +++ b/internal/video/video_entity.go @@ -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"` } diff --git a/internal/video/video_handler.go b/internal/video/video_handler.go index 894d240..f128501 100644 --- a/internal/video/video_handler.go +++ b/internal/video/video_handler.go @@ -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 { diff --git a/internal/video/video_repo.go b/internal/video/video_repo.go index 725fc84..c5ffa7b 100644 --- a/internal/video/video_repo.go +++ b/internal/video/video_repo.go @@ -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 +} diff --git a/internal/video/video_service.go b/internal/video/video_service.go index ee25d6e..2d6eb29 100644 --- a/internal/video/video_service.go +++ b/internal/video/video_service.go @@ -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 {