コード例 #1
0
        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
                    });
                }
            }
        }