Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calculate the reward #16

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions internal/controllers/comments/user.go
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 计算 reward 的代码不应该放在 GenerateResponse 方法内(功能和方法名字不符)。我的建议是写在 comments/Post 方法内。
  2. 对 User 示例修改后需要调用 Save 保存,否则修改不会写入数据库。
  3. 获得 reward 的应该是邀请该用户的用户,而不是发表评论的用户自身
  4. 整个修改过程应该用事务包起来,并且应该和发表评论作为同一个事务,建议是写在 comments/post.go:106 中 :
// 插入评论作为一个事务,若插入失败则回滚
err = db.Transaction(func(tx *gorm.DB) error {
	...
}

Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ func GenerateResponse(comments []models.Comment, uid uint, likeResult []CommentL
} else if !showAnonymous {
continue
}

user,err:=queries.GetUserByID(nil,v.User.ID)
if err==nil{
if user.InvCommitReward == true{
user.Reward += 100
user.InvCommitReward = false
}
}

for _, t := range v.CourseGroup.Teachers {
c.Group.Teachers = append(c.Group.Teachers, struct {
ID uint `json:"id"`
Expand Down
1 change: 1 addition & 0 deletions pkg/models/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Comment struct {
CoverTitle string
CoverContent string
CoverReason string
Reward int
}

type CommentLike struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type User struct {
IsAdmin bool `gorm:"default:false"`
IsCommunityAdmin bool `gorm:"default:false"`
InvitationCode string
Reward int
InvCommitReward bool `gorm:"default:false"`
}

func init() {
Expand Down
23 changes: 23 additions & 0 deletions pkg/queries/user.go
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move these lines up to the invitation code check block.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这部分代码更适合放在 register_active 的逻辑中

此外这部分也应该使用事务包起来

Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,29 @@ func Register(db *gorm.DB, u *models.User, invitation_code string) error {
return errors.Wrap(err, errors.DatabaseError)
}




//若该用户通过邀请码注册
if u.InvitationCode != ""{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

u.InvitationCode is the invitation code generated in the current function for the registered user, not someone else's invitation code that the new user used to register. Use the invitation_code parameter of this function.

//寻找邀请人,找到后邀请人加一元
userInvite:= &models.User{}
resultInvite := db.Where("invitation_code = ?", invitation_code).Take(userInvite)
if err:=resultInvite.Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return errors.Wrap(err, errors.DatabaseError)
}
if result.RowsAffected > 0 {
if user.IsActive {
return errors.New(errors.UserEmailDuplicated)
} else {
db.Delete(user)
}
}
userInvite.Reward += 100
//将此字段标记为true,表示当被邀请人发表评论时可获得奖励
u.InvCommitReward = true
}

body := fmt.Sprintf(`<html><body>
<h1>欢迎注册%s</h1> <p>我们已经接收到您的电子邮箱验证申请,请点击以下链接完成注册。</p>
<p>验证完成后,您将能够即刻发布课程评价,并与其他用户互动。</p>
Expand Down
Loading