示例#1
0
        /// <summary>
        /// 加入投票
        /// </summary>
        /// <param name="pollModel"></param>
        /// <returns></returns>
        public bool AddPoll(AddPollViewModel pollModel)
        {
            try
            {
                var answers = pollModel.Answer.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var answer in answers)
                {
                    PollOption option = new PollOption();

                    string sql = "select * from PollOption where PollID=1 and Answers='" + answer + "'";
                    var    queryCurrentItem = _connection.Query <PollOption>(sql);
                    if (queryCurrentItem.Any())
                    {
                        option       = queryCurrentItem.First();
                        option.Vote += 1;
                        _connection.Update(option);
                    }
                    else
                    {
                        option.PollID  = 1;                       //poll.PollID;
                        option.Answers = answer;
                        option.Vote    = 1;
                        _connection.Insert(option);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(true);
        }
        public bool AddPoll(AddPollViewModel pollModel)
        {
            using (var dbContextTransaction = _db.Database.BeginTransaction())
            {
                try
                {
                    var  answers = pollModel.Answer.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                    Poll poll    = new Poll();
                    poll.Question = pollModel.Question;
                    poll.Active   = true;
                    _db.Poll.Add(poll);
                    _db.SaveChanges();

                    foreach (var answer in answers)
                    {
                        PollOption option = new PollOption();
                        option.PollId  = poll.PollId;
                        option.Answers = answer;
                        option.Vote    = 0;
                        _db.PollOption.Add(option);
                        _db.SaveChanges();
                    }

                    dbContextTransaction.Commit();
                }
                catch
                {
                    //TO DO: log error here
                    dbContextTransaction.Rollback();
                }
            }

            return(true);
        }
示例#3
0
        public async Task <JsonResult> Post([FromBody] AddPollViewModel vm)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var newPoll = Mapper.Map <Poll>(Mapper.Map <PollViewModel>(vm));
                    var user    = await _userManager.FindByNameAsync(User.Identity.Name);

                    newPoll.User = user;

                    // Save to the Database
                    _logger.LogInformation("Attempting to save a new poll");
                    _repository.AddPoll(newPoll);

                    if (_repository.SaveAll())
                    {
                        Response.StatusCode = (int)HttpStatusCode.Created;
                        return(Json(Mapper.Map <PollViewModel>(newPoll)));
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError("Failed to save new poll", ex);
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(Json(new { Message = ex.Message }));
                }
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json(new { Message = "Failed", ModelState = ModelState }));
        }
 public IActionResult AddPoll(AddPollViewModel poll)
 {
     if (ModelState.IsValid)
     {
         if (_pollManager.AddPoll(poll))
         {
             ViewBag.Message = "Poll added successfully!";
             ASPNETCoreSignalRDemo.Hubs.PollHub.FetchPoll();
         }
     }
     return(View(poll));
 }
        public IActionResult Create(AddPollViewModel model)
        {
            if (ModelState.IsValid)
            {
                var activePoll = _context.PollRepositoryUW.Get(r => r.Active == true);

                if (activePoll.Count() > 0)
                {
                    ViewBag.message = "از قبل یک نظرسنجی فعال وجود دارد.";
                    return(View());
                }



                using (var transaction = _context.BeginTransaction())
                {
                    try
                    {
                        //ثبت نظرسنجی
                        Poll poll = new Poll();
                        poll.Question      = model.Question;
                        poll.Active        = true;
                        poll.PollStartDate = PublicClass.DateAndTimeShamsi.DateShamsi();
                        _context.PollRepositoryUW.Create(poll);
                        _context.Save();
                        //ثبت پاسخهای نظرسنجی
                        var answerList = model.Answer.Split(new string[] { Environment.NewLine },
                                                            StringSplitOptions.RemoveEmptyEntries);

                        foreach (var answer in answerList)
                        {
                            PollOption pollopt = new PollOption();
                            pollopt.Answer     = answer;
                            pollopt.VouteCount = 0;
                            pollopt.PollID     = poll.PollId;

                            _context.PollOptionRepositoryUW.Create(pollopt);
                            _context.Save();
                        }

                        transaction.Commit();
                        return(RedirectToAction(nameof(Index)));
                    }
                    catch
                    {
                        transaction.RollBack();
                    }
                }
            }
            return(View(model));
        }
        public IActionResult AddPoll(AddPollViewModel poll)
        {
            if (ModelState.IsValid)
            {
                if (_pollManager.AddPoll(poll))
                {
                    ViewBag.Message = "Poll added successfully!";

                    _hubContext.Clients.All.SendAsync("PollItem");
                    _hubContext.Clients.All.SendAsync("GetVoteResults");
                }
            }
            return(View(poll));
        }
示例#7
0
 public IActionResult AddPoll(AddPollViewModel model)
 {
     try
     {
         var poll = _mapper.Map <Poll>(model);
         _pollsRepository.InsertPoll(poll);
         _unitOfWork.Commit();
         return(View(nameof(this.ManagePolls)));
     }
     catch
     {
         return(View());
     }
 }