MCPcopy Index your code
hub / github.com/apache/answer / RemoveAnswer

Method RemoveAnswer

internal/service/content/answer_service.go:120–202  ·  view source on GitHub ↗

RemoveAnswer delete answer

(ctx context.Context, req *schema.RemoveAnswerReq)

Source from the content-addressed store, hash-verified

118
119// RemoveAnswer delete answer
120func (as *AnswerService) RemoveAnswer(ctx context.Context, req *schema.RemoveAnswerReq) (err error) {
121 answerInfo, exist, err := as.answerRepo.GetByID(ctx, req.ID)
122 if err != nil {
123 return err
124 }
125 if !exist {
126 return nil
127 }
128 // if the status is deleted, return directly
129 if answerInfo.Status == entity.AnswerStatusDeleted {
130 return nil
131 }
132 roleID, err := as.roleService.GetUserRole(ctx, req.UserID)
133 if err != nil {
134 return err
135 }
136 if roleID != role.RoleAdminID && roleID != role.RoleModeratorID {
137 if answerInfo.UserID != req.UserID {
138 return errors.BadRequest(reason.AnswerCannotDeleted)
139 }
140 if answerInfo.VoteCount > 0 {
141 return errors.BadRequest(reason.AnswerCannotDeleted)
142 }
143 if answerInfo.Accepted == schema.AnswerAcceptedEnable {
144 return errors.BadRequest(reason.AnswerCannotDeleted)
145 }
146 _, exist, err := as.questionRepo.GetQuestion(ctx, answerInfo.QuestionID)
147 if err != nil {
148 return errors.BadRequest(reason.AnswerCannotDeleted)
149 }
150 if !exist {
151 return errors.BadRequest(reason.AnswerCannotDeleted)
152 }
153 }
154
155 err = as.answerRepo.RemoveAnswer(ctx, req.ID)
156 if err != nil {
157 return err
158 }
159
160 // user add question count
161 err = as.questionCommon.UpdateAnswerCount(ctx, answerInfo.QuestionID)
162 if err != nil {
163 log.Error("IncreaseAnswerCount error", err.Error())
164 }
165 userAnswerCount, err := as.answerRepo.GetCountByUserID(ctx, answerInfo.UserID)
166 if err != nil {
167 log.Error("GetCountByUserID error", err.Error())
168 }
169 err = as.userCommon.UpdateAnswerCount(ctx, answerInfo.UserID, int(userAnswerCount))
170 if err != nil {
171 log.Error("user IncreaseAnswerCount error", err.Error())
172 }
173 err = as.questionRepo.RemoveQuestionLink(ctx, &entity.QuestionLink{
174 FromQuestionID: answerInfo.QuestionID,
175 FromAnswerID: answerInfo.ID,
176 }, &entity.QuestionLink{
177 ToQuestionID: answerInfo.QuestionID,

Callers 1

AdminSetAnswerStatusMethod · 0.95

Calls 12

StringToInt64Function · 0.92
NewEventFunction · 0.92
GetUserRoleMethod · 0.80
AIDMethod · 0.80
TIDMethod · 0.80
GetByIDMethod · 0.65
GetQuestionMethod · 0.65
RemoveAnswerMethod · 0.65
UpdateAnswerCountMethod · 0.65
GetCountByUserIDMethod · 0.65
RemoveQuestionLinkMethod · 0.65
SendMethod · 0.65

Tested by

no test coverage detected