Skip to content

Commit

Permalink
fix: ReviewVote load vote after vote
Browse files Browse the repository at this point in the history
  • Loading branch information
JingYiJun committed Apr 8, 2024
1 parent d5ff0b8 commit ad48cb4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
4 changes: 4 additions & 0 deletions danke/api/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ func ModifyReviewV1(c *fiber.Ctx) (err error) {
review, err = FindReviewByID(DB, id, FindReviewOption{
PreloadHistory: true,
PreloadAchievement: true,
PreloadVote: true,
UserID: user.ID,
})
if err != nil {
return
Expand Down Expand Up @@ -196,6 +198,8 @@ func VoteForReviewV1(c *fiber.Ctx) (err error) {
review, err = FindReviewByID(DB, reviewID, FindReviewOption{
PreloadHistory: true,
PreloadAchievement: true,
PreloadVote: true,
UserID: user.ID,
})
if err != nil {
return err
Expand Down
15 changes: 12 additions & 3 deletions danke/model/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,30 @@ type Review struct {
DownvoteCount int `json:"downvote_count" gorm:"not null;default:0"`
ModifyCount int `json:"modify_count" gorm:"not null;default:0"`
History ReviewHistoryList `json:"-"`
Vote ReviewVoteList `json:"-"`
Vote ReviewVoteList `json:"-" gorm:"foreignKey:ReviewID;references:ID"`
UserAchievements []*UserAchievement `json:"-" gorm:"foreignKey:UserID;references:ReviewerID"`
}

type FindReviewOption struct {
PreloadHistory bool
PreloadAchievement bool
PreloadVote bool
UserID int
}

func (o FindReviewOption) setQuery(querySet *gorm.DB) *gorm.DB {
if o.PreloadHistory {
querySet.Preload("History")
querySet = querySet.Preload("History")
}
if o.PreloadAchievement {
querySet.Preload("UserAchievements.Achievement")
querySet = querySet.Preload("UserAchievements.Achievement")
}
if o.PreloadVote {
if o.UserID != 0 {
querySet = querySet.Preload("Vote", "user_id = ?", o.UserID)
} else {
querySet = querySet.Preload("Vote")
}
}
return querySet
}
Expand Down
9 changes: 6 additions & 3 deletions danke/schema/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,14 @@ func (r *ReviewV1Response) FromModel(
r.Rank = new(ReviewRankV1).FromModel(review.Rank)
r.Remark = review.UpvoteCount - review.DownvoteCount

for _, vote := range review.Vote {
if vote.UserID == user.ID {
r.Vote = vote.Data
if user != nil {
for _, vote := range review.Vote {
if vote.UserID == user.ID {
r.Vote = vote.Data
}
}
}

r.History = make([]*ReviewHistoryV1Response, 0, len(review.History))
for _, history := range review.History {
r.History = append(r.History, new(ReviewHistoryV1Response).FromModel(review, history, r.Rank))
Expand Down

0 comments on commit ad48cb4

Please sign in to comment.