feat(video): add likescount and updatelikescount
This commit is contained in:
@@ -46,7 +46,7 @@ func SetRouter(db *gorm.DB) *gin.Engine {
|
|||||||
}
|
}
|
||||||
// like
|
// like
|
||||||
likeRepository := video.NewLikeRepository(db)
|
likeRepository := video.NewLikeRepository(db)
|
||||||
likeService := video.NewLikeService(likeRepository)
|
likeService := video.NewLikeService(likeRepository, videoRepository)
|
||||||
likeHandler := video.NewLikeHandler(likeService)
|
likeHandler := video.NewLikeHandler(likeService)
|
||||||
likeGroup := r.Group("/like")
|
likeGroup := r.Group("/like")
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7,11 +7,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type LikeService struct {
|
type LikeService struct {
|
||||||
repo *LikeRepository
|
repo *LikeRepository
|
||||||
|
VideoRepo *VideoRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLikeService(repo *LikeRepository) *LikeService {
|
func NewLikeService(repo *LikeRepository, videoRepo *VideoRepository) *LikeService {
|
||||||
return &LikeService{repo: repo}
|
return &LikeService{repo: repo, VideoRepo: videoRepo}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *LikeService) Like(ctx context.Context, like *Like) error {
|
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")
|
return errors.New("user has liked this video")
|
||||||
}
|
}
|
||||||
like.CreatedAt = time.Now()
|
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 {
|
func (s *LikeService) Unlike(ctx context.Context, like *Like) error {
|
||||||
if isLiked, err := s.IsLiked(ctx, like.VideoID, like.AccountID); err == nil && !isLiked {
|
if isLiked, err := s.IsLiked(ctx, like.VideoID, like.AccountID); err == nil && !isLiked {
|
||||||
return errors.New("user has not liked this video")
|
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) {
|
func (s *LikeService) IsLiked(ctx context.Context, videoID, accountID uint) (bool, error) {
|
||||||
|
|||||||
@@ -8,5 +8,7 @@ type Video struct {
|
|||||||
Title string `gorm:"type:varchar(255);not null"`
|
Title string `gorm:"type:varchar(255);not null"`
|
||||||
Description string `gorm:"type:varchar(255);"`
|
Description string `gorm:"type:varchar(255);"`
|
||||||
PlayURL string `gorm:"type:varchar(255);not null"`
|
PlayURL string `gorm:"type:varchar(255);not null"`
|
||||||
|
CoverURL string `gorm:"type:varchar(255);not null"`
|
||||||
CreateTime time.Time `gorm:"autoCreateTime"`
|
CreateTime time.Time `gorm:"autoCreateTime"`
|
||||||
|
LikesCount int64 `gorm:"column:likes_count;not null;default:0" json:"likes_count"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ type PublishVideoRequest struct {
|
|||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
PlayURL string `json:"play_url"`
|
PlayURL string `json:"play_url"`
|
||||||
|
CoverURL string `json:"cover_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ListByAuthorIDRequest struct {
|
type ListByAuthorIDRequest struct {
|
||||||
@@ -28,6 +29,11 @@ type GetDetailRequest struct {
|
|||||||
ID uint `json:"id"`
|
ID uint `json:"id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UpdateLikesCountRequest struct {
|
||||||
|
ID uint `json:"id"`
|
||||||
|
LikesCount int64 `json:"likes_count"`
|
||||||
|
}
|
||||||
|
|
||||||
func (vh *VideoHandler) PublishVideo(c *gin.Context) {
|
func (vh *VideoHandler) PublishVideo(c *gin.Context) {
|
||||||
var req PublishVideoRequest
|
var req PublishVideoRequest
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
@@ -50,6 +56,7 @@ func (vh *VideoHandler) PublishVideo(c *gin.Context) {
|
|||||||
Title: req.Title,
|
Title: req.Title,
|
||||||
Description: req.Description,
|
Description: req.Description,
|
||||||
PlayURL: req.PlayURL,
|
PlayURL: req.PlayURL,
|
||||||
|
CoverURL: req.CoverURL,
|
||||||
CreateTime: time.Now(),
|
CreateTime: time.Now(),
|
||||||
}
|
}
|
||||||
if err := vh.service.Publish(c.Request.Context(), video); err != nil {
|
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)
|
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"})
|
||||||
|
}
|
||||||
|
|||||||
@@ -40,3 +40,12 @@ func (vr *VideoRepository) GetByID(ctx context.Context, id uint) (*Video, error)
|
|||||||
}
|
}
|
||||||
return &video, nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -20,6 +20,9 @@ func (vs *VideoService) Publish(ctx context.Context, video *Video) error {
|
|||||||
if video.PlayURL == "" {
|
if video.PlayURL == "" {
|
||||||
return errors.New("play url is required")
|
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 {
|
if err := vs.repo.CreateVideo(ctx, video); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -41,3 +44,10 @@ func (vs *VideoService) GetDetail(ctx context.Context, id uint) (*Video, error)
|
|||||||
}
|
}
|
||||||
return video, nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user