Skip to content

Commit

Permalink
feat(controllers/comments.Post): reward inviter for first comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Prince213 committed Apr 26, 2024
1 parent 14c97dc commit 2a3fc6f
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion internal/controllers/comments/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"coursebench-backend/pkg/errors"
"coursebench-backend/pkg/models"
"coursebench-backend/pkg/queries"
"time"

"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
"time"
)

type PostRequest struct {
Expand Down Expand Up @@ -117,6 +118,26 @@ func Post(c *fiber.Ctx) (err error) {
if err != nil {
return errors.Wrap(err, errors.DatabaseError)
}

// If this is the first time for the poster to post a comment, the reward the inviter.
var user models.User
err = tx.First(&user, uid).Error
if err != nil {
return errors.Wrap(err, errors.DatabaseError)
}

if user.HasPostedComments {
return nil
}
user.HasPostedComments = true

inviter, err := queries.GetUserByID(tx, user.InvitedByUserID)
if err != nil {
return errors.Wrap(err, errors.DatabaseError)
}
inviter.Reward += 100
tx.Save(inviter)

return nil
})
if err != nil {
Expand Down

0 comments on commit 2a3fc6f

Please sign in to comment.