Пример #1
0
        public async Task <IActionResult> Publish([FromBody] PollPublishRequest pollPublishRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            //var pollInDb = await _pollRepository.Select(pollPublishRequest.PollId);

            //if (!pollInDb.IsPublished)
            //{
            await _pollRepository.Update(pollPublishRequest);

            int pollPk = await _pollRepository.SelectPollPk(pollPublishRequest.PollId);

            await _pollOptionRepository.Insert(pollPublishRequest.Options, pollPk);

            //}
            //else
            //{
            //return BadRequest();
            //}
            //

            return(Ok());
        }
        public async Task <IActionResult> Publish([FromBody] PollPublishRequest pollPublishRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var pollDto = await _pollRepository.Select(pollPublishRequest.PollId);

            if (!pollDto.IsPublished)
            {
                pollDto.IsPublished    = true;
                pollDto.Title          = pollPublishRequest.Title;
                pollDto.IsSingleOption = pollPublishRequest.IsSingleOption;
                pollDto.Options        = pollPublishRequest.Options;
                await _pollRepository.Update(pollDto);

                await _pollOptionRepository.Insert(pollPublishRequest.Options, pollDto.PrimaryKey);
            }
            else
            {
                return(BadRequest());
            }


            return(Ok());
        }
        public async Task Publish_PollRepositoriesReceivingCallsIfModelInDataBase()
        {
            string tokenString = "tokenString";

            _tokenService.GetRandomToken(Arg.Any <int>()).Returns(tokenString);
            PollDto pollDto = new PollDto()
            {
                PollId      = tokenString,
                IsPublished = false
            };

            _pollRepository.Select(tokenString).Returns(pollDto);

            var pollPublishRequest = new PollPublishRequest()
            {
                EditorToken    = tokenString,
                Options        = new List <PollOptionDto>(),
                IsSingleOption = true,
                PollId         = tokenString,
                Title          = "test"
            };
            await _pollBuilderController.Publish(pollPublishRequest);

            var result = await _pollRepository.Select(pollDto.PollId);

            if (!pollDto.IsPublished)
            {
                await _pollRepository.Received().Update(pollDto);

                await _pollOptionRepository.Received().Insert(pollPublishRequest.Options, pollDto.PrimaryKey);
            }
        }
Пример #4
0
        public async Task Update(PollPublishRequest poll)
        {
            using (var command = await CreateCommand())
            {
                try
                {
                    StringBuilder sqlText = new StringBuilder();
                    sqlText.Append(
                        "Update ct_poll SET title = @title,  is_single_option = @is_single_option, is_published = @is_published, poll_id = @poll_id, editor_token = @editor_token ");
                    sqlText.Append("Where poll_id = @poll_id");

                    command.CommandType = CommandType.Text;
                    command.CommandText = sqlText.ToString();
                    command.Parameters.AddWithValue("@title", poll.Title);
                    command.Parameters.AddWithValue("@is_single_option", poll.IsSingleOption);
                    command.Parameters.AddWithValue("@is_published", poll.IsPublished);
                    command.Parameters.AddWithValue("@poll_id", poll.PollId);
                    command.Parameters.AddWithValue("@editor_token", poll.EditorToken);
                    await command.ExecuteNonQueryAsync();

                    command.Transaction.Commit();
                }
                catch (SqlException)
                {
                    command.Transaction.Rollback();
                }
            }
        }
Пример #5
0
        public async Task Insert(PollPublishRequest poll)
        {
            using (var command = await CreateCommand())
            {
                try
                {
                    StringBuilder sqlText = new StringBuilder();
                    sqlText.Append(
                        "Insert Into ct_poll (title, is_single_option, is_published, poll_id, editor_token) ");
                    sqlText.Append(
                        "Values (@title,@is_single_option,@is_published,@poll_id,@editor_token) ");

                    command.CommandType = CommandType.Text;
                    command.CommandText = sqlText.ToString();
                    SqlParameter titleParameter = new SqlParameter
                    {
                        ParameterName = "@title",
                        SqlDbType     = SqlDbType.NVarChar,
                        IsNullable    = true,
                        Direction     = ParameterDirection.Input,
                        Value         = (object)poll.Title ?? DBNull.Value
                    };
                    SqlParameter isSingleOptionParameter = new SqlParameter
                    {
                        ParameterName = "@is_single_option",
                        SqlDbType     = SqlDbType.Bit,
                        IsNullable    = true,
                        Direction     = ParameterDirection.Input,
                        Value         = (object)poll.IsSingleOption ?? DBNull.Value
                    };
                    command.Parameters.Add(titleParameter);
                    command.Parameters.Add(isSingleOptionParameter);
                    command.Parameters.AddWithValue("@is_published", poll.IsPublished);
                    command.Parameters.AddWithValue("@poll_id", poll.PollId);
                    command.Parameters.AddWithValue("@editor_token", poll.EditorToken);
                    await command.ExecuteNonQueryAsync();

                    command.Transaction.Commit();
                }
                catch (SqlException)
                {
                    command.Transaction.Rollback();
                }
            }
        }