コード例 #1
0
ファイル: Poll.cs プロジェクト: venkyvcareers/BashBook_Api
        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
                    });
                }
            }
        }