Skip to content

Commit e53ff73

Browse files
authored
Merge pull request #247 from answerdev/hotfix/1.0.6/answer
Hotfix/1.0.6/answer
2 parents b7437e3 + b00cfbf commit e53ff73

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

internal/controller/answer_controller.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ func (ac *AnswerController) Add(ctx *gin.Context) {
136136
handler.HandleResponse(ctx, nil, nil)
137137
return
138138
}
139+
140+
canList, err := ac.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
141+
permission.AnswerEdit,
142+
permission.AnswerDelete,
143+
})
144+
if err != nil {
145+
handler.HandleResponse(ctx, err, nil)
146+
return
147+
}
148+
149+
objectOwner := ac.rankService.CheckOperationObjectOwner(ctx, req.UserID, info.ID)
150+
req.CanEdit = canList[0] || objectOwner
151+
req.CanDelete = canList[1] || objectOwner
152+
if !can {
153+
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
154+
return
155+
}
156+
info.MemberActions = permission.GetAnswerPermission(ctx, req.UserID, info.UserID, req.CanEdit, req.CanDelete)
139157
handler.HandleResponse(ctx, nil, gin.H{
140158
"info": info,
141159
"question": questionInfo,

internal/schema/answer_schema.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ type AnswerAddReq struct {
2424
Content string `validate:"required,notblank,gte=6,lte=65535" json:"content"`
2525
HTML string `json:"-"`
2626
UserID string `json:"-"`
27+
CanEdit bool `json:"-"`
28+
CanDelete bool `json:"-"`
2729
}
2830

2931
func (req *AnswerAddReq) Check() (errFields []*validator.FormErrorField, err error) {

internal/service/answer_service.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,14 @@ func (as *AnswerService) RemoveAnswer(ctx context.Context, req *schema.RemoveAns
9292
if answerInfo.Accepted == schema.AnswerAcceptedEnable {
9393
return errors.BadRequest(reason.AnswerCannotDeleted)
9494
}
95-
questionInfo, exist, err := as.questionRepo.GetQuestion(ctx, answerInfo.QuestionID)
95+
_, exist, err := as.questionRepo.GetQuestion(ctx, answerInfo.QuestionID)
9696
if err != nil {
9797
return errors.BadRequest(reason.AnswerCannotDeleted)
9898
}
9999
if !exist {
100100
return errors.BadRequest(reason.AnswerCannotDeleted)
101101
}
102-
if questionInfo.AnswerCount > 1 {
103-
return errors.BadRequest(reason.AnswerCannotDeleted)
104-
}
105-
if questionInfo.AcceptedAnswerID != "" {
106-
return errors.BadRequest(reason.AnswerCannotDeleted)
107-
}
102+
108103
}
109104

110105
// user add question count

0 commit comments

Comments
 (0)