From 710e2dc0408ed13a97453b1e7a53579137c0cadc Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 16 Dec 2024 21:37:19 +0800 Subject: [PATCH] :bug: Bug fixes on post freezed data didn't update --- pkg/internal/database/migrator.go | 6 ++++-- pkg/internal/services/accounts.go | 4 ++-- pkg/internal/services/posts.go | 10 +++++----- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/pkg/internal/database/migrator.go b/pkg/internal/database/migrator.go index 9358b65..0930c3c 100644 --- a/pkg/internal/database/migrator.go +++ b/pkg/internal/database/migrator.go @@ -10,13 +10,15 @@ var AutoMaintainRange = []any{ &models.Category{}, &models.Tag{}, &models.Post{}, - &models.Reaction{}, &models.Subscription{}, } func RunMigration(source *gorm.DB) error { if err := source.AutoMigrate( - AutoMaintainRange..., + append( + AutoMaintainRange, + &models.Reaction{}, + )..., ); err != nil { return err } diff --git a/pkg/internal/services/accounts.go b/pkg/internal/services/accounts.go index 2f9372d..938583e 100644 --- a/pkg/internal/services/accounts.go +++ b/pkg/internal/services/accounts.go @@ -22,11 +22,11 @@ func GetAccountWithID(id uint) (models.Publisher, error) { func ModifyPosterVoteCount(user models.Publisher, isUpvote bool, delta int) error { if isUpvote { user.TotalUpvote += delta + return database.C.Model(&user).Update("total_upvote", user.TotalUpvote).Error } else { user.TotalDownvote += delta + return database.C.Model(&user).Update("total_downvote", user.TotalDownvote).Error } - - return database.C.Save(&user).Error } func NotifyPosterAccount(pub models.Publisher, post models.Post, title, body, topic string, subtitle ...string) error { diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index df4d58e..abfc61b 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -520,10 +520,10 @@ func ReactPost(user authm.Account, reaction models.Reaction) (bool, models.React if reaction.Attitude == models.AttitudePositive { op.TotalUpvote++ - database.C.Where("id = ?", op.ID).Update("total_upvote", op.TotalDownvote) + database.C.Model(&op).Update("total_upvote", op.TotalDownvote) } else { op.TotalDownvote++ - database.C.Where("id = ?", op.ID).Update("total_downvote", op.TotalDownvote) + database.C.Model(&op).Update("total_downvote", op.TotalDownvote) } } @@ -538,10 +538,10 @@ func ReactPost(user authm.Account, reaction models.Reaction) (bool, models.React if reaction.Attitude == models.AttitudePositive { op.TotalUpvote-- - database.C.Where("id = ?", op.ID).Update("total_upvote", op.TotalDownvote) + database.C.Model(&op).Update("total_upvote", op.TotalDownvote) } else { op.TotalDownvote-- - database.C.Where("id = ?", op.ID).Update("total_downvote", op.TotalDownvote) + database.C.Model(&op).Update("total_downvote", op.TotalDownvote) } database.C.Save(&op) } @@ -557,7 +557,7 @@ func PinPost(post models.Post) (bool, error) { post.PinnedAt = lo.ToPtr(time.Now()) } - if err := database.C.Where("id = ?", post.ID).Update("pinned_at", post.PinnedAt).Error; err != nil { + if err := database.C.Model(&post).Update("pinned_at", post.PinnedAt).Error; err != nil { return post.PinnedAt != nil, err } return post.PinnedAt != nil, nil