MCPcopy
hub / github.com/apache/answer / RemoveQuestion

Method RemoveQuestion

internal/service/content/question_service.go:556–664  ·  view source on GitHub ↗

RemoveQuestion delete question

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

Source from the content-addressed store, hash-verified

554
555// RemoveQuestion delete question
556func (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 {

Callers

nothing calls this directly

Calls 15

StringToInt64Function · 0.92
NewEventFunction · 0.92
GetObjectEntityTagMethod · 0.80
QIDMethod · 0.80
TIDMethod · 0.80
GetQuestionMethod · 0.65
GetUserQuestionCountMethod · 0.65
UpdateQuestionCountMethod · 0.65
GetReviewByObjectMethod · 0.65
UpdateReviewStatusMethod · 0.65

Tested by

no test coverage detected