🐛 Bug fixes on post freezed data didn't update

This commit is contained in:
LittleSheep 2024-12-16 21:37:19 +08:00
parent ade3dbdeee
commit 710e2dc040
3 changed files with 11 additions and 9 deletions

View File

@ -10,13 +10,15 @@ var AutoMaintainRange = []any{
&models.Category{}, &models.Category{},
&models.Tag{}, &models.Tag{},
&models.Post{}, &models.Post{},
&models.Reaction{},
&models.Subscription{}, &models.Subscription{},
} }
func RunMigration(source *gorm.DB) error { func RunMigration(source *gorm.DB) error {
if err := source.AutoMigrate( if err := source.AutoMigrate(
AutoMaintainRange..., append(
AutoMaintainRange,
&models.Reaction{},
)...,
); err != nil { ); err != nil {
return err return err
} }

View File

@ -22,11 +22,11 @@ func GetAccountWithID(id uint) (models.Publisher, error) {
func ModifyPosterVoteCount(user models.Publisher, isUpvote bool, delta int) error { func ModifyPosterVoteCount(user models.Publisher, isUpvote bool, delta int) error {
if isUpvote { if isUpvote {
user.TotalUpvote += delta user.TotalUpvote += delta
return database.C.Model(&user).Update("total_upvote", user.TotalUpvote).Error
} else { } else {
user.TotalDownvote += delta 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 { func NotifyPosterAccount(pub models.Publisher, post models.Post, title, body, topic string, subtitle ...string) error {

View File

@ -520,10 +520,10 @@ func ReactPost(user authm.Account, reaction models.Reaction) (bool, models.React
if reaction.Attitude == models.AttitudePositive { if reaction.Attitude == models.AttitudePositive {
op.TotalUpvote++ op.TotalUpvote++
database.C.Where("id = ?", op.ID).Update("total_upvote", op.TotalDownvote) database.C.Model(&op).Update("total_upvote", op.TotalDownvote)
} else { } else {
op.TotalDownvote++ 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 { if reaction.Attitude == models.AttitudePositive {
op.TotalUpvote-- op.TotalUpvote--
database.C.Where("id = ?", op.ID).Update("total_upvote", op.TotalDownvote) database.C.Model(&op).Update("total_upvote", op.TotalDownvote)
} else { } else {
op.TotalDownvote-- 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) database.C.Save(&op)
} }
@ -557,7 +557,7 @@ func PinPost(post models.Post) (bool, error) {
post.PinnedAt = lo.ToPtr(time.Now()) 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, err
} }
return post.PinnedAt != nil, nil return post.PinnedAt != nil, nil