upvotes
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/promote/production Build is passing

This commit is contained in:
2024-04-23 19:13:48 +03:00
parent a1df10c37e
commit ed403c2c72
11 changed files with 147 additions and 17 deletions

View File

@@ -36,7 +36,7 @@ func (f *FeedbackService) GetByProjectID(projectID string) ([]*models.Feedback,
return f.db.Feedback.GetByProjectID(projectID)
}
func (f *FeedbackService) Upvote(sessionID, feedbackUUID string) error {
func (f *FeedbackService) Vote(sessionID, feedbackUUID, action string) error {
feedback, err := f.db.Feedback.GetByUUID(feedbackUUID)
if err != nil {
return err
@@ -44,15 +44,27 @@ func (f *FeedbackService) Upvote(sessionID, feedbackUUID string) error {
// check if project type is feature
if feedback.Type != models.FeedbackTypeFeature {
return errors.New("only feature tickets can be upvoted")
return errors.New("only feature tickets can be voted")
}
// check if user has already upvoted
for _, upvote := range feedback.Upvote {
if upvote.SessionUUID == sessionID {
return errors.New("user has already upvoted")
switch action {
case "upvote":
// check if user has already upvoted
for _, upvote := range feedback.Upvote {
if upvote.SessionUUID == sessionID {
return errors.New("user has already upvoted")
}
}
return f.db.Feedback.CreateUpvote(models.NewUpvote(sessionID, feedbackUUID))
case "downvote":
// check if user has already upvoted
for _, upvote := range feedback.Upvote {
if upvote.SessionUUID == sessionID {
return f.db.Feedback.DeleteUpvoteBySessionIDAndFeedbackUUID(sessionID, feedbackUUID)
}
}
}
return f.db.Feedback.CreateUpvote(models.NewUpvote(sessionID, feedbackUUID))
return nil
}