Merge pull request #400 from alimy/pr-update-user-metric

fixed update user metric incorrect when no user record
pull/408/head
北野 - Michael Li 1 year ago committed by GitHub
commit f5afcfbbc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,6 +4,10 @@ All notable changes to paopao-ce are documented in this file.
## 0.6.0+dev ([`dev`](https://github.com/rocboss/paopao-ce/tree/dev)) ## 0.6.0+dev ([`dev`](https://github.com/rocboss/paopao-ce/tree/dev))
TODO; TODO;
## 0.5.1
### Fixed
- fixed update user metric incorrect when no user record.
## 0.5.0 ## 0.5.0
### Added ### Added
- add `LoggerOpenObserve` feature use OpenObserve to collect log.[#370](https://github.com/rocboss/paopao-ce/pull/370) - add `LoggerOpenObserve` feature use OpenObserve to collect log.[#370](https://github.com/rocboss/paopao-ce/pull/370)

@ -26,13 +26,11 @@ type userMetricSrvA struct {
} }
func (s *tweetMetricSrvA) UpdateTweetMetric(metric *cs.TweetMetric) error { func (s *tweetMetricSrvA) UpdateTweetMetric(metric *cs.TweetMetric) error {
return s.db.Transaction(func(tx *gorm.DB) (err error) { return s.db.Transaction(func(tx *gorm.DB) error {
postMetric := &dbr.PostMetric{PostId: metric.PostId} postMetric := &dbr.PostMetric{PostId: metric.PostId}
db := s.db.Model(postMetric).Where("post_id=?", metric.PostId) tx.Model(postMetric).Where("post_id=?", metric.PostId).First(postMetric)
db.First(postMetric)
postMetric.RankScore = metric.RankScore(postMetric.MotivationFactor) postMetric.RankScore = metric.RankScore(postMetric.MotivationFactor)
err = db.Save(postMetric).Error return tx.Save(postMetric).Error
return
}) })
} }
@ -46,13 +44,11 @@ func (s *tweetMetricSrvA) DeleteTweetMetric(postId int64) (err error) {
} }
func (s *commentMetricSrvA) UpdateCommentMetric(metric *cs.CommentMetric) error { func (s *commentMetricSrvA) UpdateCommentMetric(metric *cs.CommentMetric) error {
return s.db.Transaction(func(tx *gorm.DB) (err error) { return s.db.Transaction(func(tx *gorm.DB) error {
commentMetric := &dbr.CommentMetric{CommentId: metric.CommentId} commentMetric := &dbr.CommentMetric{CommentId: metric.CommentId}
db := s.db.Model(commentMetric).Where("comment_id=?", metric.CommentId) tx.Model(commentMetric).Where("comment_id=?", metric.CommentId).First(commentMetric)
db.First(commentMetric)
commentMetric.RankScore = metric.RankScore(commentMetric.MotivationFactor) commentMetric.RankScore = metric.RankScore(commentMetric.MotivationFactor)
err = db.Save(commentMetric).Error return tx.Save(commentMetric).Error
return
}) })
} }
@ -65,14 +61,9 @@ func (s *commentMetricSrvA) DeleteCommentMetric(commentId int64) (err error) {
return (&dbr.CommentMetric{CommentId: commentId}).Delete(s.db) return (&dbr.CommentMetric{CommentId: commentId}).Delete(s.db)
} }
func (s *userMetricSrvA) UpdateUserMetric(userId int64, action uint8) (err error) { func (s *userMetricSrvA) UpdateUserMetric(userId int64, action uint8) error {
metric := &dbr.UserMetric{} metric := &dbr.UserMetric{UserId: userId}
db := s.db.Model(metric) s.db.Model(metric).Where("user_id=?", userId).First(metric)
if err = db.Where("user_id=?", userId).First(metric).Error; err != nil {
metric = &dbr.UserMetric{
UserId: userId,
}
}
metric.LatestTrendsOn = time.Now().Unix() metric.LatestTrendsOn = time.Now().Unix()
switch action { switch action {
case cs.MetricActionCreateTweet: case cs.MetricActionCreateTweet:
@ -82,7 +73,7 @@ func (s *userMetricSrvA) UpdateUserMetric(userId int64, action uint8) (err error
metric.TweetsCount-- metric.TweetsCount--
} }
} }
return db.Save(metric).Error return s.db.Save(metric).Error
} }
func (s *userMetricSrvA) AddUserMetric(userId int64) (err error) { func (s *userMetricSrvA) AddUserMetric(userId int64) (err error) {

Loading…
Cancel
Save