Exemple #1
0
        public void CreateSurvey_()
        {
            //Arrange
            DBContext context = new DBContext(@"Data Source=(localdb)\ProjectsV13;Initial Catalog=DynamicSurvey;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");

            SurveysRepository repository = new SurveysRepository(context);

            List <Tuple <string, int> > testList = new List <Tuple <string, int> >();

            testList.Add(new Tuple <string, int>("UnitTestQuestion", 1));

            testList.Add(new Tuple <string, int>("UnitTestQuestion2", 1));

            Survey testSurvey = new Survey(new CreateSurveyDto {
                CreatorId = 1, IsAnonymous = true, Questions = testList, Title = "UnitTestTitle"
            });

            //Act
            Task <CreateSurveyResponseDto> responseTask = repository.CreateSurveyAsync(testSurvey);

            CreateSurveyResponseDto response = responseTask.Result;


            //Assert
            Assert.AreEqual(testSurvey.Title, response.ResponseMessage.Split(':')[1].Split('.')[0].Substring(1));
            Assert.AreEqual(testSurvey.Questions[0].QuestionText, response.ResponseMessage.Split(':')[2].Substring(1));
        }
        public async Task <CreateSurveyResponseDto> CreateSurveyAsync(Survey survey)
        {
            CreateSurveyResponseDto repsonse = new CreateSurveyResponseDto();

            using (SqlConnection connection = await _dbContext.OpenConnectionAsync())
            {
                using (SqlCommand command = new SqlCommand(CreateSurveyStatement, connection))
                {
                    command.Parameters.AddWithValue("@CreatorID", survey.CreatorId);
                    command.Parameters.AddWithValue("@Title", survey.Title);
                    command.Parameters.AddWithValue("@IsAnonymous", survey.IsAnonymous);
                    command.Parameters.AddWithValue("@Questions", JsonConvert.SerializeObject(survey.Questions));



                    int rowsAffected = await command.ExecuteNonQueryAsync();

                    if (rowsAffected == 1)
                    {
                        repsonse.ResponseMessage = $"Survey was successfully created with the following Title: {survey.Title}. The first question of the survey is: {survey.Questions[0].QuestionText}";
                    }
                }
            }

            return(repsonse);
        }
        public async Task <CreateSurveyResponseDto> CreateSurveyAsync([FromBody] CreateSurveyDto surveyInformation)
        {
            Survey tempSurvey = new Survey(surveyInformation);

            CreateSurveyResponseDto repsonse = await _repository.CreateSurveyAsync(tempSurvey);

            return(repsonse);
        }