Пример #1
0
        public async Task <bool> SaveSurveyInDB(string json)
        {
            Console.WriteLine("     [.]Deserializing and saving in the db!");
            bool check = false;

            try
            {
                Survey survey = JsonConvert.DeserializeObject <Survey>(json);

                string surveyID = await surveyDAL.NewSurvey(survey.Owner, survey.Name, survey.Description);

                int counter = 0;
                foreach (var question in survey.QuestionsMultipleChoice)
                {
                    counter++;
                    string answerOne = "", answerTwo = "", answerThree = "", answerFour = "";
                    //because some question fields are optional
                    switch (question.AnswersField.Count)
                    {
                    case 2:
                        answerOne = question.AnswersField[0];
                        answerTwo = question.AnswersField[1];
                        break;

                    case 3:
                        answerOne   = question.AnswersField[0];
                        answerTwo   = question.AnswersField[1];
                        answerThree = question.AnswersField[2];
                        break;

                    case 4:
                        answerOne   = question.AnswersField[0];
                        answerTwo   = question.AnswersField[1];
                        answerThree = question.AnswersField[2];
                        answerFour  = question.AnswersField[3];
                        break;

                    default:
                        break;
                    }
                    string mQuestionID = await questionDAL.NewQuestionMultiple(surveyID, question.QuestionField, answerOne, answerTwo, answerThree, answerFour, counter);

                    //if at least 1 question is saved we'd consider it a success
                    if (mQuestionID != null || mQuestionID != "")
                    {
                        check = true;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Could not save and deserialize! - {ex.ToString()}");
                check = false;
            }
            return(check);
        }