RecoverAnswer recover deleted answer
(ctx context.Context, req *schema.RecoverAnswerReq)
| 203 | |
| 204 | // RecoverAnswer recover deleted answer |
| 205 | func (as *AnswerService) RecoverAnswer(ctx context.Context, req *schema.RecoverAnswerReq) (err error) { |
| 206 | answerInfo, exist, err := as.answerRepo.GetByID(ctx, req.AnswerID) |
| 207 | if err != nil { |
| 208 | return err |
| 209 | } |
| 210 | if !exist { |
| 211 | return errors.BadRequest(reason.AnswerNotFound) |
| 212 | } |
| 213 | if answerInfo.Status != entity.AnswerStatusDeleted { |
| 214 | return nil |
| 215 | } |
| 216 | if err = as.answerRepo.RecoverAnswer(ctx, req.AnswerID); err != nil { |
| 217 | return err |
| 218 | } |
| 219 | if err = as.questionRepo.RecoverQuestionLink(ctx, &entity.QuestionLink{ |
| 220 | FromQuestionID: answerInfo.QuestionID, |
| 221 | FromAnswerID: answerInfo.ID, |
| 222 | }, &entity.QuestionLink{ |
| 223 | ToQuestionID: answerInfo.QuestionID, |
| 224 | ToAnswerID: answerInfo.ID, |
| 225 | }); err != nil { |
| 226 | return err |
| 227 | } |
| 228 | |
| 229 | if err = as.questionCommon.UpdateAnswerCount(ctx, answerInfo.QuestionID); err != nil { |
| 230 | log.Errorf("update answer count failed: %s", err.Error()) |
| 231 | } |
| 232 | userAnswerCount, err := as.answerRepo.GetCountByUserID(ctx, answerInfo.UserID) |
| 233 | if err != nil { |
| 234 | log.Errorf("get user answer count failed: %s", err.Error()) |
| 235 | } else { |
| 236 | err = as.userCommon.UpdateAnswerCount(ctx, answerInfo.UserID, int(userAnswerCount)) |
| 237 | if err != nil { |
| 238 | log.Errorf("update user answer count failed: %s", err.Error()) |
| 239 | } |
| 240 | } |
| 241 | as.activityQueueService.Send(ctx, &schema.ActivityMsg{ |
| 242 | UserID: req.UserID, |
| 243 | TriggerUserID: converter.StringToInt64(req.UserID), |
| 244 | ObjectID: answerInfo.ID, |
| 245 | OriginalObjectID: answerInfo.ID, |
| 246 | ActivityTypeKey: constant.ActAnswerUndeleted, |
| 247 | }) |
| 248 | as.vectorSyncService.Send(ctx, &vector_sync.Task{Action: vector_sync.ActionUpsert, ObjectType: vector_sync.ObjectTypeAnswer, ObjectID: answerInfo.ID}) |
| 249 | as.vectorSyncService.Send(ctx, &vector_sync.Task{Action: vector_sync.ActionUpsert, ObjectType: vector_sync.ObjectTypeQuestion, ObjectID: answerInfo.QuestionID}) |
| 250 | return nil |
| 251 | } |
| 252 | |
| 253 | func (as *AnswerService) Insert(ctx context.Context, req *schema.AnswerAddReq) (string, error) { |
| 254 | questionInfo, exist, err := as.questionRepo.GetQuestion(ctx, req.QuestionID) |
no test coverage detected