AcceptAnswer accept answer
(ctx context.Context, req *schema.AcceptAnswerReq)
| 446 | |
| 447 | // AcceptAnswer accept answer |
| 448 | func (as *AnswerService) AcceptAnswer(ctx context.Context, req *schema.AcceptAnswerReq) (err error) { |
| 449 | // find question |
| 450 | questionInfo, exist, err := as.questionRepo.GetQuestion(ctx, req.QuestionID) |
| 451 | if err != nil { |
| 452 | return err |
| 453 | } |
| 454 | if !exist { |
| 455 | return errors.BadRequest(reason.QuestionNotFound) |
| 456 | } |
| 457 | questionInfo.ID = uid.DeShortID(questionInfo.ID) |
| 458 | if questionInfo.AcceptedAnswerID == req.AnswerID { |
| 459 | return nil |
| 460 | } |
| 461 | |
| 462 | // find answer |
| 463 | var acceptedAnswerInfo *entity.Answer |
| 464 | if len(req.AnswerID) > 1 { |
| 465 | acceptedAnswerInfo, exist, err = as.answerRepo.GetByID(ctx, req.AnswerID) |
| 466 | if err != nil { |
| 467 | return err |
| 468 | } |
| 469 | if !exist { |
| 470 | return errors.BadRequest(reason.AnswerNotFound) |
| 471 | } |
| 472 | |
| 473 | // check answer belong to question |
| 474 | if acceptedAnswerInfo.QuestionID != req.QuestionID { |
| 475 | return errors.BadRequest(reason.AnswerNotFound) |
| 476 | } |
| 477 | acceptedAnswerInfo.ID = uid.DeShortID(acceptedAnswerInfo.ID) |
| 478 | } |
| 479 | |
| 480 | // update answers status |
| 481 | if err = as.answerRepo.UpdateAcceptedStatus(ctx, req.AnswerID, req.QuestionID); err != nil { |
| 482 | return err |
| 483 | } |
| 484 | |
| 485 | // update question status |
| 486 | err = as.questionCommon.UpdateAccepted(ctx, req.QuestionID, req.AnswerID) |
| 487 | if err != nil { |
| 488 | log.Error("UpdateLastAnswer error", err.Error()) |
| 489 | } |
| 490 | |
| 491 | var oldAnswerInfo *entity.Answer |
| 492 | if len(questionInfo.AcceptedAnswerID) > 1 { |
| 493 | oldAnswerInfo, _, err = as.answerRepo.GetByID(ctx, questionInfo.AcceptedAnswerID) |
| 494 | if err != nil { |
| 495 | return err |
| 496 | } |
| 497 | oldAnswerInfo.ID = uid.DeShortID(oldAnswerInfo.ID) |
| 498 | } |
| 499 | |
| 500 | if acceptedAnswerInfo != nil { |
| 501 | as.eventQueueService.Send(ctx, schema.NewEvent(constant.EventQuestionAccept, req.UserID).TID(acceptedAnswerInfo.ID). |
| 502 | QID(questionInfo.ID, questionInfo.UserID).AID(acceptedAnswerInfo.ID, acceptedAnswerInfo.UserID)) |
| 503 | } |
| 504 | |
| 505 | as.updateAnswerRank(ctx, req.UserID, questionInfo, acceptedAnswerInfo, oldAnswerInfo) |
no test coverage detected