From 30399be71808f26a30d4c28f21c9b30bc67219ac Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 26 Jul 2024 21:09:53 +0800 Subject: [PATCH] :sparkles: Each post has total down/upvote count --- pkg/internal/models/posts.go | 3 +++ pkg/internal/services/posts.go | 14 ++++++++++++++ 2 files changed, 17 insertions(+) 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