🐛 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.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
}

View File

@ -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 {

View File

@ -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