Exemplo n.º 1
0
 public int Add(OptionModel model)
 {
     if (!_poll.IsActive(model.PollId))
     {
         throw new ReturnExceptionModel(new CustomExceptionModel()
         {
             StatusCode = HttpStatusCode.BadRequest, Message = "Voting is in progress, can not be edited"
         });
     }
     model.Image = Cdn.Base64ToImageUrl(model.Image);
     return(_option.Add(model));
 }
Exemplo n.º 2
0
        public void Add_Option_Without_Text_Error()
        {
            var options = new DbContextOptionsBuilder <BitcoinShowDBContext>()
                          .UseInMemoryDatabase(System.Guid.NewGuid().ToString())
                          .Options;
            var context = new BitcoinShowDBContext(options);
            OptionRepository repository = new OptionRepository(context);

            Option option            = new Option();
            ArgumentNullException ex = Assert.Throws <ArgumentNullException>(() => repository.Add(option));

            Assert.NotNull(ex);
            Assert.Equal(nameof(option.Text), ex.ParamName);
        }
Exemplo n.º 3
0
        public void Add_Option_Success()
        {
            string text     = "Add_Option_Success";
            var    options2 = new DbContextOptionsBuilder <BitcoinShowDBContext>()
                              .UseInMemoryDatabase("asd")
                              .Options;
            var context2 = new BitcoinShowDBContext(options2);
            OptionRepository repository = new OptionRepository(context2);

            Option actual = new Option();

            actual.Text = text;

            repository.Add(actual);
            Assert.NotNull(actual);
            Assert.True(0 < actual.Id);
            Assert.Equal(text, actual.Text);
            Assert.Equal(1, context2.Options.Count());
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Add(OptionToExistingSurveyInput toExistingSurveyInput)
        {
            var token = HttpContext.Request.Headers["Authorization"].Last().Split(" ").Last();
            var roles = new List <string>()
            {
                "User", "SchoolAdmin", "Admin"
            };

            if (RoleService.CheckRoles(token, roles, _userManager))
            {
                var survey = _surveyRepository.GetById(toExistingSurveyInput.SurveyId);
                var option = OptionInputConverter.Convert(toExistingSurveyInput);

                _optionRepository.Add(option);
                survey.AddOption(option);
                return(CreatedAtAction("Add", option));
            }

            return(Unauthorized("Only User, SchoolAdmin, Admin can access this controller."));
        }
Exemplo n.º 5
0
        public int AddWithOptions(PollWithOptionsModel model)
        {
            using (var scope = new TransactionScope())
            {
                try
                {
                    // Validations
                    if (model.Options.Count == 0)
                    {
                        throw new InvalidOperationException("Poll do not have any options");
                    }

                    foreach (var option in model.Options)
                    {
                        if (string.IsNullOrEmpty(option.Text) && model.Poll.OptionTypeId != (int)Lookups.PollOptionTypes.Image)
                        {
                            throw new InvalidOperationException("Poll option text is empty");
                        }
                    }

                    model.Poll.Image             = Cdn.Base64ToImageUrl(model.Poll.Image);
                    model.Poll.IsActive          = true;
                    model.Poll.IsVotingCompleted = false;
                    int pollId = _poll.Add(model.Poll);

                    foreach (var option in model.Options)
                    {
                        if (!string.IsNullOrEmpty(option.Text))
                        {
                            option.Image  = Cdn.Base64ToImageUrl(option.Image);
                            option.PollId = pollId;
                            _option.Add(option);
                        }
                    }

                    foreach (var group in model.Groups)
                    {
                        _entityPoll.Add(new EntityPollModel()
                        {
                            EntityId     = group,
                            PollId       = pollId,
                            EntityTypeId = (int)Lookups.EntityTypes.Group
                        });
                    }

                    scope.Complete();

                    return(pollId);
                }
                catch (Exception ex)
                {
                    scope.Dispose();
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    string json             = js.Serialize(model);
                    Log.Error("BL-Poll - AddWithOptions" + json, ex);
                    throw new ReturnExceptionModel(new CustomExceptionModel()
                    {
                        StatusCode = HttpStatusCode.BadRequest, Message = ex.Message
                    });
                }
            }
        }
Exemplo n.º 6
0
        public void Add_Option_With_Text_Greater_Than_Max_Size_Error()
        {
            var options = new DbContextOptionsBuilder <BitcoinShowDBContext>()
                          .UseInMemoryDatabase(System.Guid.NewGuid().ToString())
                          .Options;
            var context = new BitcoinShowDBContext(options);
            OptionRepository repository = new OptionRepository(context);

            Option option = new Option();

            option.Text = new String('A', 201);
            ArgumentOutOfRangeException ex = Assert.Throws <ArgumentOutOfRangeException>(() => repository.Add(option));

            Assert.NotNull(ex);
            Assert.Equal(nameof(option.Text), ex.ParamName);
            Assert.Equal(0, context.Options.Count());
        }