public bool Edit(UserVoteModel model) { using (var scope = new TransactionScope()) { try { var oldOptions = _option.GetOptionList(model.UserId, model.PollId); foreach (var option in model.Options) { if (!oldOptions.Contains(option)) { var userVote = new UserPollOptionModel() { UserId = model.UserId, OptionId = option, PollId = model.PollId }; if (_userVote.IsAlreadyVoted(userVote)) { if (model.Options.Count == 1) { scope.Dispose(); throw new ReturnExceptionModel(new CustomExceptionModel() { StatusCode = HttpStatusCode.BadRequest, Message = "User already voted" }); } } else { _userVote.Add(userVote); } } } foreach (var option in oldOptions) { if (!model.Options.Contains(option)) { _option.Delete(option); } } scope.Complete(); return(true); } catch (Exception ex) { scope.Dispose(); JavaScriptSerializer js = new JavaScriptSerializer(); string json = js.Serialize(model); Log.Error("BL-Voting - Edit" + json, ex); throw new ReturnExceptionModel(new CustomExceptionModel() { StatusCode = HttpStatusCode.BadRequest, Message = ex.Message }); } } }
public void Delete_Option_Not_Found_Error() { var options = new DbContextOptionsBuilder <BitcoinShowDBContext>() .UseInMemoryDatabase(System.Guid.NewGuid().ToString()) .Options; var context = new BitcoinShowDBContext(options); OptionRepository repository = new OptionRepository(context); Exception ex = Assert.Throws <DbUpdateException>(() => repository.Delete(1)); Assert.NotNull(ex); Assert.Equal("The current option does not exists.", ex.Message); }
public void Delete_Option_Success() { var options = new DbContextOptionsBuilder <BitcoinShowDBContext>() .UseInMemoryDatabase(System.Guid.NewGuid().ToString()) .Options; var context = new BitcoinShowDBContext(options); Option deleteOption = new Option(); deleteOption.Text = "delete"; context.Options.Add(deleteOption); context.SaveChanges(); OptionRepository repository = new OptionRepository(context); repository.Delete(deleteOption.Id); }
public bool EditWithOptions(PollWithOptionsModel model) { using (var scope = new TransactionScope()) { try { model.Poll.Image = Cdn.Base64ToImageUrl(model.Poll.Image); if (_poll.IsActive(model.Poll.PollId)) { throw new InvalidOperationException("Voting is in progress, can not be edited"); } _poll.Edit(model.Poll); var oldOptionList = _option.GetAllOptionId(model.Poll.PollId); var newOptionList = model.Options.Where(x => x.OptionId > 0).Select(x => x.OptionId).ToList(); foreach (var option in model.Options) { if (option.OptionId > 0) { _option.Edit(option); } else { option.PollId = model.Poll.PollId; option.Image = Cdn.Base64ToImageUrl(option.Image); _option.Add(option); } } foreach (var item in oldOptionList) { if (!newOptionList.Contains(item)) { _option.Delete(item); } } var oldGroupList = _entityPoll.GetEntityIdList(model.Poll.PollId, (int)Lookups.EntityTypes.Group); foreach (var item in model.Groups) { if (!oldGroupList.Contains(item)) { _entityPoll.Add(new EntityPollModel() { EntityId = item, PollId = model.Poll.PollId, EntityTypeId = (int)Lookups.EntityTypes.Group }); } } foreach (var item in oldGroupList) { if (!model.Groups.Contains(item)) { _entityPoll.Delete(model.Poll.PollId, (int)Lookups.EntityTypes.Group, item); } } scope.Complete(); return(true); } catch (Exception ex) { scope.Dispose(); JavaScriptSerializer js = new JavaScriptSerializer(); string json = js.Serialize(model); Log.Error("BL- Poll - EditWithOptions" + json, ex); throw new ReturnExceptionModel(new CustomExceptionModel() { StatusCode = HttpStatusCode.BadRequest, Message = ex.Message }); } } }
public ActionResult Delete(int id) { var result = _optionRepository.Delete(id); return(RedirectToAction("Index")); }
public async Task Delete(int id) { await _optionRepository.Delete(id); }