feat: 添加了列出点赞视频的功能
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
|||||||
|
|
||||||
func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine {
|
func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
r.Static("/static", "./.run/uploads")
|
||||||
// account
|
// account
|
||||||
accountRepository := account.NewAccountRepository(db)
|
accountRepository := account.NewAccountRepository(db)
|
||||||
accountService := account.NewAccountService(accountRepository, cache)
|
accountService := account.NewAccountService(accountRepository, cache)
|
||||||
@@ -44,6 +45,8 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine {
|
|||||||
protectedVideoGroup := videoGroup.Group("")
|
protectedVideoGroup := videoGroup.Group("")
|
||||||
protectedVideoGroup.Use(middleware.JWTAuth(accountRepository, cache))
|
protectedVideoGroup.Use(middleware.JWTAuth(accountRepository, cache))
|
||||||
{
|
{
|
||||||
|
protectedVideoGroup.POST("/uploadVideo", videoHandler.UploadVideo)
|
||||||
|
protectedVideoGroup.POST("/uploadCover", videoHandler.UploadCover)
|
||||||
protectedVideoGroup.POST("/publish", videoHandler.PublishVideo)
|
protectedVideoGroup.POST("/publish", videoHandler.PublishVideo)
|
||||||
}
|
}
|
||||||
// like
|
// like
|
||||||
@@ -57,6 +60,7 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client) *gin.Engine {
|
|||||||
protectedLikeGroup.POST("/like", likeHandler.Like)
|
protectedLikeGroup.POST("/like", likeHandler.Like)
|
||||||
protectedLikeGroup.POST("/unlike", likeHandler.Unlike)
|
protectedLikeGroup.POST("/unlike", likeHandler.Unlike)
|
||||||
protectedLikeGroup.POST("/isLiked", likeHandler.IsLiked)
|
protectedLikeGroup.POST("/isLiked", likeHandler.IsLiked)
|
||||||
|
protectedLikeGroup.POST("/listMyLikedVideos", likeHandler.ListMyLikedVideos)
|
||||||
}
|
}
|
||||||
// comment
|
// comment
|
||||||
commentRepository := video.NewCommentRepository(db)
|
commentRepository := video.NewCommentRepository(db)
|
||||||
|
|||||||
@@ -89,3 +89,18 @@ func (lh *LikeHandler) IsLiked(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
c.JSON(200, gin.H{"is_liked": isLiked})
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -54,3 +54,20 @@ func (r *LikeRepository) BatchGetLiked(ctx context.Context, videoIDs []uint, acc
|
|||||||
}
|
}
|
||||||
return likeMap, nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -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) {
|
func (s *LikeService) IsLiked(ctx context.Context, videoID, accountID uint) (bool, error) {
|
||||||
return s.repo.IsLiked(ctx, videoID, accountID)
|
return s.repo.IsLiked(ctx, videoID, accountID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *LikeService) ListLikedVideos(ctx context.Context, accountID uint) ([]Video, error) {
|
||||||
|
return s.repo.ListLikedVideos(ctx, accountID)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user