feat(video): add likescount and updatelikescount

This commit is contained in:
Leon
2025-12-09 21:11:29 +08:00
parent d1c0f049d6
commit 8e51de9410
6 changed files with 68 additions and 6 deletions

View File

@@ -46,7 +46,7 @@ func SetRouter(db *gorm.DB) *gin.Engine {
}
// like
likeRepository := video.NewLikeRepository(db)
likeService := video.NewLikeService(likeRepository)
likeService := video.NewLikeService(likeRepository, videoRepository)
likeHandler := video.NewLikeHandler(likeService)
likeGroup := r.Group("/like")
{

View File

@@ -8,10 +8,11 @@ import (
type LikeService struct {
repo *LikeRepository
VideoRepo *VideoRepository
}
func NewLikeService(repo *LikeRepository) *LikeService {
return &LikeService{repo: repo}
func NewLikeService(repo *LikeRepository, videoRepo *VideoRepository) *LikeService {
return &LikeService{repo: repo, VideoRepo: videoRepo}
}
func (s *LikeService) Like(ctx context.Context, like *Like) error {
@@ -19,14 +20,34 @@ func (s *LikeService) Like(ctx context.Context, like *Like) error {
return errors.New("user has liked this video")
}
like.CreatedAt = time.Now()
return s.repo.Like(ctx, like)
if err := s.repo.Like(ctx, like); err != nil {
return err
}
likesCount, err := s.GetLikesCount(ctx, like.VideoID)
if err != nil {
return err
}
if err := s.VideoRepo.UpdateLikesCount(ctx, like.VideoID, likesCount); err != nil {
return err
}
return nil
}
func (s *LikeService) Unlike(ctx context.Context, like *Like) error {
if isLiked, err := s.IsLiked(ctx, like.VideoID, like.AccountID); err == nil && !isLiked {
return errors.New("user has not liked this video")
}
return s.repo.Unlike(ctx, like)
if err := s.repo.Unlike(ctx, like); err != nil {
return err
}
likesCount, err := s.GetLikesCount(ctx, like.VideoID)
if err != nil {
return err
}
if err := s.VideoRepo.UpdateLikesCount(ctx, like.VideoID, likesCount); err != nil {
return err
}
return nil
}
func (s *LikeService) IsLiked(ctx context.Context, videoID, accountID uint) (bool, error) {

View File

@@ -8,5 +8,7 @@ type Video struct {
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"`
LikesCount int64 `gorm:"column:likes_count;not null;default:0" json:"likes_count"`
}

View File

@@ -18,6 +18,7 @@ type PublishVideoRequest struct {
Title string `json:"title"`
Description string `json:"description"`
PlayURL string `json:"play_url"`
CoverURL string `json:"cover_url"`
}
type ListByAuthorIDRequest struct {
@@ -28,6 +29,11 @@ type GetDetailRequest struct {
ID uint `json:"id"`
}
type UpdateLikesCountRequest struct {
ID uint `json:"id"`
LikesCount int64 `json:"likes_count"`
}
func (vh *VideoHandler) PublishVideo(c *gin.Context) {
var req PublishVideoRequest
if err := c.ShouldBindJSON(&req); err != nil {
@@ -50,6 +56,7 @@ func (vh *VideoHandler) PublishVideo(c *gin.Context) {
Title: req.Title,
Description: req.Description,
PlayURL: req.PlayURL,
CoverURL: req.CoverURL,
CreateTime: time.Now(),
}
if err := vh.service.Publish(c.Request.Context(), video); err != nil {
@@ -86,3 +93,16 @@ func (vh *VideoHandler) GetDetail(c *gin.Context) {
}
c.JSON(200, video)
}
func (vh *VideoHandler) UpdateLikesCount(c *gin.Context) {
var req UpdateLikesCountRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
if err := vh.service.UpdateLikesCount(c.Request.Context(), req.ID, req.LikesCount); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
c.JSON(200, gin.H{"message": "likes count updated"})
}

View File

@@ -40,3 +40,12 @@ func (vr *VideoRepository) GetByID(ctx context.Context, id uint) (*Video, error)
}
return &video, nil
}
func (vr *VideoRepository) UpdateLikesCount(ctx context.Context, id uint, likesCount int64) error {
if err := vr.db.WithContext(ctx).Model(&Video{}).
Where("id = ?", id).
Update("likes_count", likesCount).Error; err != nil {
return err
}
return nil
}

View File

@@ -20,6 +20,9 @@ func (vs *VideoService) Publish(ctx context.Context, video *Video) error {
if video.PlayURL == "" {
return errors.New("play url is required")
}
if video.CoverURL == "" {
return errors.New("cover url is required")
}
if err := vs.repo.CreateVideo(ctx, video); err != nil {
return err
}
@@ -41,3 +44,10 @@ func (vs *VideoService) GetDetail(ctx context.Context, id uint) (*Video, error)
}
return video, nil
}
func (vs *VideoService) UpdateLikesCount(ctx context.Context, id uint, likesCount int64) error {
if err := vs.repo.UpdateLikesCount(ctx, id, likesCount); err != nil {
return err
}
return nil
}