feat: 添加了列出点赞视频的功能

This commit is contained in:
Leon
2025-12-26 18:23:55 +08:00
parent ce0aec6110
commit a21a79a2be
4 changed files with 40 additions and 0 deletions

View File

@@ -14,6 +14,7 @@ import (
func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine {
r := gin.Default()
r.Static("/static", "./.run/uploads")
// account
accountRepository := account.NewAccountRepository(db)
accountService := account.NewAccountService(accountRepository, cache)
@@ -44,6 +45,8 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine {
protectedVideoGroup := videoGroup.Group("")
protectedVideoGroup.Use(middleware.JWTAuth(accountRepository, cache))
{
protectedVideoGroup.POST("/uploadVideo", videoHandler.UploadVideo)
protectedVideoGroup.POST("/uploadCover", videoHandler.UploadCover)
protectedVideoGroup.POST("/publish", videoHandler.PublishVideo)
}
// like
@@ -57,6 +60,7 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine {
protectedLikeGroup.POST("/like", likeHandler.Like)
protectedLikeGroup.POST("/unlike", likeHandler.Unlike)
protectedLikeGroup.POST("/isLiked", likeHandler.IsLiked)
protectedLikeGroup.POST("/listMyLikedVideos", likeHandler.ListMyLikedVideos)
}
// comment
commentRepository := video.NewCommentRepository(db)

View File

@@ -89,3 +89,18 @@ func (lh *LikeHandler) IsLiked(c *gin.Context) {
}
c.JSON(200, gin.H{"is_liked": isLiked})
}
func (lh *LikeHandler) ListMyLikedVideos(c *gin.Context) {
accountID, err := middleware.GetAccountID(c)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
videos, err := lh.service.ListLikedVideos(c.Request.Context(), accountID)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
c.JSON(200, videos)
}

View File

@@ -54,3 +54,20 @@ func (r *LikeRepository) BatchGetLiked(ctx context.Context, videoIDs []uint, acc
}
return likeMap, nil
}
func (r *LikeRepository) ListLikedVideos(ctx context.Context, accountID uint) ([]Video, error) {
var videos []Video
if accountID == 0 {
return videos, nil
}
err := r.db.WithContext(ctx).
Model(&Video{}).
Joins("JOIN likes ON likes.video_id = videos.id").
Where("likes.account_id = ?", accountID).
Order("likes.created_at desc").
Find(&videos).Error
if err != nil {
return nil, err
}
return videos, nil
}

View File

@@ -64,3 +64,7 @@ func (s *LikeService) Unlike(ctx context.Context, like *Like) error {
func (s *LikeService) IsLiked(ctx context.Context, videoID, accountID uint) (bool, error) {
return s.repo.IsLiked(ctx, videoID, accountID)
}
func (s *LikeService) ListLikedVideos(ctx context.Context, accountID uint) ([]Video, error) {
return s.repo.ListLikedVideos(ctx, accountID)
}