Each post has total down/upvote count

This commit is contained in:
LittleSheep 2024-07-26 21:09:53 +08:00
parent 37aa69e6ca
commit 30399be718
2 changed files with 17 additions and 0 deletions

View File

@ -34,6 +34,9 @@ type Post struct {
PublishedAt *time.Time `json:"published_at"` PublishedAt *time.Time `json:"published_at"`
PublishedUntil *time.Time `json:"published_until"` PublishedUntil *time.Time `json:"published_until"`
TotalUpvote int `json:"total_upvote"`
TotalDownvote int `json:"total_downvote"`
AuthorID uint `json:"author_id"` AuthorID uint `json:"author_id"`
Author Account `json:"author"` Author Account `json:"author"`

View File

@ -295,6 +295,13 @@ func ReactPost(user models.Account, reaction models.Reaction) (bool, models.Reac
err = database.C.Save(&reaction).Error err = database.C.Save(&reaction).Error
if err == nil { if err == nil {
_ = ModifyPosterVoteCount(op.Author, reaction.Attitude == models.AttitudePositive, 1) _ = 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 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 err = database.C.Delete(&reaction).Error
if err == nil { if err == nil {
_ = ModifyPosterVoteCount(op.Author, reaction.Attitude == models.AttitudePositive, -1) _ = 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 return false, reaction, err