From 4168b2ee1ea9d2bfab7a3a926b87c726c3d00bec Mon Sep 17 00:00:00 2001 From: Michael Li Date: Mon, 18 Sep 2023 15:06:12 +0800 Subject: [PATCH] fixed not create post metric item after create tweet error --- internal/dao/jinzhu/metrics.go | 8 +++----- internal/dao/jinzhu/tweets.go | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/internal/dao/jinzhu/metrics.go b/internal/dao/jinzhu/metrics.go index 129439e9..038bf1ac 100644 --- a/internal/dao/jinzhu/metrics.go +++ b/internal/dao/jinzhu/metrics.go @@ -17,11 +17,9 @@ type tweetMetricSrvA struct { func (s *tweetMetricSrvA) UpdateRankScore(metric *cs.TweetMetric) error { return s.db.Transaction(func(tx *gorm.DB) (err error) { - postMetric := &dbr.PostMetric{} + postMetric := &dbr.PostMetric{PostId: metric.PostId} db := s.db.Model(postMetric).Where("post_id=?", metric.PostId) - if err = db.First(postMetric).Error; err != nil { - return - } + db.First(postMetric) postMetric.RankScore = metric.RankScore(postMetric.MotivationFactor) err = db.Save(postMetric).Error return @@ -30,7 +28,7 @@ func (s *tweetMetricSrvA) UpdateRankScore(metric *cs.TweetMetric) error { } func (s *tweetMetricSrvA) AddTweetMetric(postId int64) (err error) { - _, err = (&dbr.PostMetric{}).Create(s.db) + _, err = (&dbr.PostMetric{PostId: postId}).Create(s.db) return } diff --git a/internal/dao/jinzhu/tweets.go b/internal/dao/jinzhu/tweets.go index c06f388d..1a67bc2b 100644 --- a/internal/dao/jinzhu/tweets.go +++ b/internal/dao/jinzhu/tweets.go @@ -438,7 +438,7 @@ func (s *tweetSrv) ListIndexNewestTweets(limit, offset int) (res []*ms.Post, tot } func (s *tweetSrv) ListIndexHotsTweets(limit, offset int) (res []*ms.Post, total int64, err error) { - db := s.db.Table(_post_).Joins(fmt.Sprintf("JOIN %s metric ON %s.id=metric.post_id", _post_metric_, _post_)).Where("visibility >= ?", cs.TweetVisitPublic) + db := s.db.Table(_post_).Joins(fmt.Sprintf("LEFT JOIN %s metric ON %s.id=metric.post_id", _post_metric_, _post_)).Where("visibility >= ?", cs.TweetVisitPublic) if err = db.Count(&total).Error; err != nil { return }