diff --git a/pkg/internal/models/posts.go b/pkg/internal/models/posts.go index 0b2ccad..15d28c1 100644 --- a/pkg/internal/models/posts.go +++ b/pkg/internal/models/posts.go @@ -34,6 +34,9 @@ type Post struct { PublishedAt *time.Time `json:"published_at"` PublishedUntil *time.Time `json:"published_until"` + TotalUpvote int `json:"total_upvote"` + TotalDownvote int `json:"total_downvote"` + AuthorID uint `json:"author_id"` Author Account `json:"author"` diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index 857e679..605b141 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -295,6 +295,13 @@ func ReactPost(user models.Account, reaction models.Reaction) (bool, models.Reac err = database.C.Save(&reaction).Error if err == nil { _ = ModifyPosterVoteCount(op.Author, reaction.Attitude == models.AttitudePositive, 1) + + if reaction.Attitude == models.AttitudePositive { + op.TotalUpvote++ + } else { + op.TotalDownvote++ + } + database.C.Save(&op) } return true, reaction, err @@ -305,6 +312,13 @@ func ReactPost(user models.Account, reaction models.Reaction) (bool, models.Reac err = database.C.Delete(&reaction).Error if err == nil { _ = ModifyPosterVoteCount(op.Author, reaction.Attitude == models.AttitudePositive, -1) + + if reaction.Attitude == models.AttitudePositive { + op.TotalUpvote-- + } else { + op.TotalDownvote-- + } + database.C.Save(&op) } return false, reaction, err