LearnLanguages.Study.ViewModels.StudyPhraseTimedQuestionAnswerViewModel.SaveQuestion C# (CSharp) Method

SaveQuestion() public method

public SaveQuestion ( ) : System.Threading.Tasks.Task
return System.Threading.Tasks.Task
    public async Task SaveQuestion()
    {
      QuestionIsReadOnly = true;
      SaveQuestionButtonIsEnabled = false;
      #region Thinking (try..)
      var targetId = Guid.NewGuid();
      History.Events.ThinkingAboutTargetEvent.Publish(targetId);
      DisableNavigationRequestedEventMessage.Publish();
      try
      {
      #endregion

        #region Get the DB version of question, then modify it and save

        //STORE OUR MODIFIED TEXT
        _ModifiedQuestionText = Question.Text;

        //PREPARE QUESTION TO BE SEARCHED FOR IN DB
        Question.Text = _InitialQuestionText;

        #region SEARCH IN DB FOR QUESTION (WITH INITIAL TEXT)
        PhrasesByTextAndLanguageRetriever retriever = null;
        History.Events.ThinkingAboutTargetEvent.Publish(System.Guid.Empty);
        {
          retriever = await Business.PhrasesByTextAndLanguageRetriever.CreateNewAsync(Question);
        }
        History.Events.ThinkingAboutTargetEvent.Publish(System.Guid.Empty);

        var dbQuestion = retriever.RetrievedSinglePhrase;
        if (dbQuestion == null)
        {
          //QUESTION NOT IN DB, BUT QUESTION IS A CHILD
          if (Question.IsChild)
          {
            //CANNOT SAVE THE QUESTION DIRECTLY BECAUSE IT IS A CHILD
            #region CREATE NEW PHRASE EDIT, LOAD FROM QUESTION DTO, SAVE QUESTION
            var newQuestionObj = await PhraseEdit.NewPhraseEditAsync();

            Question.Text = _ModifiedQuestionText;
            var dto = Question.CreateDto();
            dto.Id = Guid.NewGuid();

            newQuestionObj.LoadFromDtoBypassPropertyChecks(dto);
            Question = newQuestionObj;

            #region Save Question
            Question = await Question.SaveAsync();
            _InitialQuestionText = Question.Text;
            _ModifiedQuestionText = "";
            SaveQuestionButtonIsEnabled = true;
            QuestionIsReadOnly = true;
            UpdateEditQuestionButtonVisibilities();
            #endregion
            #endregion
          }
          //QUESTION NOT IN DB, BUT QUESTION IS NOT A CHILD
          else
          {
            #region SAVE THE QUESTION DIRECTLY, B/C IT IS NOT A CHILD
            Question = await Question.SaveAsync();
            _InitialQuestionText = Question.Text;
            _ModifiedQuestionText = "";
            SaveQuestionButtonIsEnabled = true;
            QuestionIsReadOnly = true;
            UpdateEditQuestionButtonVisibilities();
            #endregion
          }
        }
        //QUESTION WAS FOUND IN DB
        else
        {
          #region REASSIGN THE QUESTION WITH THE DBQUESTION, SAVE WITH THE MODIFIED TEXT
          Question = dbQuestion;
          Question.Text = _ModifiedQuestionText;
          Question = await Question.SaveAsync();
          _InitialQuestionText = Question.Text;
          _ModifiedQuestionText = "";
          SaveQuestionButtonIsEnabled = true;
          QuestionIsReadOnly = true;
          UpdateEditQuestionButtonVisibilities();
          #endregion
        }

        #endregion

        #endregion
        #region (...finally) Thinked
      }
      finally
      {
        EnableNavigationRequestedEventMessage.Publish();
        History.Events.ThinkedAboutTargetEvent.Publish(targetId);
      }
        #endregion
    }