RemoveQuestion delete question
(ctx context.Context, req *schema.RemoveQuestionReq)
| 554 | |
| 555 | // RemoveQuestion delete question |
| 556 | func (qs *QuestionService) RemoveQuestion(ctx context.Context, req *schema.RemoveQuestionReq) (err error) { |
| 557 | questionInfo, has, err := qs.questionRepo.GetQuestion(ctx, req.ID) |
| 558 | if err != nil { |
| 559 | return err |
| 560 | } |
| 561 | // if the status is deleted, return directly |
| 562 | if questionInfo.Status == entity.QuestionStatusDeleted { |
| 563 | return nil |
| 564 | } |
| 565 | if !has { |
| 566 | return nil |
| 567 | } |
| 568 | if !req.IsAdmin { |
| 569 | if questionInfo.UserID != req.UserID { |
| 570 | return errors.BadRequest(reason.QuestionCannotDeleted) |
| 571 | } |
| 572 | |
| 573 | if questionInfo.AcceptedAnswerID != "0" { |
| 574 | return errors.BadRequest(reason.QuestionCannotDeleted) |
| 575 | } |
| 576 | if questionInfo.AnswerCount > 1 { |
| 577 | return errors.BadRequest(reason.QuestionCannotDeleted) |
| 578 | } |
| 579 | |
| 580 | if questionInfo.AnswerCount == 1 { |
| 581 | answersearch := &entity.AnswerSearch{} |
| 582 | answersearch.QuestionID = req.ID |
| 583 | answerList, _, err := qs.questioncommon.AnswerCommon.Search(ctx, answersearch) |
| 584 | if err != nil { |
| 585 | return err |
| 586 | } |
| 587 | for _, answer := range answerList { |
| 588 | if answer.VoteCount > 0 { |
| 589 | return errors.BadRequest(reason.QuestionCannotDeleted) |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | questionInfo.Status = entity.QuestionStatusDeleted |
| 596 | err = qs.questionRepo.UpdateQuestionStatusWithOutUpdateTime(ctx, questionInfo) |
| 597 | if err != nil { |
| 598 | return err |
| 599 | } |
| 600 | |
| 601 | userQuestionCount, err := qs.questioncommon.GetUserQuestionCount(ctx, questionInfo.UserID) |
| 602 | if err != nil { |
| 603 | log.Error("user GetUserQuestionCount error", err.Error()) |
| 604 | } else { |
| 605 | err = qs.userCommon.UpdateQuestionCount(ctx, questionInfo.UserID, userQuestionCount) |
| 606 | if err != nil { |
| 607 | log.Error("user IncreaseQuestionCount error", err.Error()) |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | // If this question has been reviewed, then delete the review. |
| 612 | reviewInfo, exist, err := qs.reviewRepo.GetReviewByObject(ctx, questionInfo.ID) |
| 613 | if exist && err == nil { |
nothing calls this directly
no test coverage detected