Ejemplo n.º 1
0
 public ActionResult Create()
 {
     var pollDetailsViewModel = new PollDetailsViewModel
     {
         StartDate = new DateTimeEditor
         {
             ShowDate = true,
             ShowTime = false
         },
         EndDate = new DateTimeEditor
         {
             ShowDate = true,
             ShowTime = false
         }
     };
     pollDetailsViewModel.Answers.Add(new PollAnswerViewModel());
     return View(pollDetailsViewModel);
 }
Ejemplo n.º 2
0
 public ActionResult Edit(int id)
 {
     PollRecord poll = _pollService.Get(id);
     var pollDetailsViewModel = new PollDetailsViewModel
     {
         Id = poll.Id,
         MaxAnswers = poll.MaxAnswers,
         PollState = poll.PollState,
         Question = poll.Question,
         StartDate = new DateTimeEditor
         {
             Date = _dateLocalizationServices.ConvertToLocalizedDateString(poll.StartDate),
             ShowDate = true,
             ShowTime = false
         },
         EndDate = new DateTimeEditor
         {
             Date = _dateLocalizationServices.ConvertToLocalizedDateString(poll.EndDate),
             ShowDate = true,
             ShowTime = false
         }
     };
     foreach (var answer in poll.Answers)
     {
         var answerModel = new PollAnswerViewModel
         {
             Id = answer.Id,
             Answer = answer.Answer,
             Votes = answer.Votes
         };
         pollDetailsViewModel.Answers.Add(answerModel);
     }
     return View(pollDetailsViewModel);
 }
Ejemplo n.º 3
0
        private bool ProcessPollPersistence(PollDetailsViewModel poll)
        {
            if (ModelState.IsValid)
            {
                var pollRecord = _pollService.Get(poll.Id) ?? new PollRecord();
                pollRecord.MaxAnswers = poll.MaxAnswers;
                pollRecord.PollState = poll.PollState;
                pollRecord.Question = poll.Question;
                if (String.IsNullOrWhiteSpace(poll.StartDate.Date))
                {
                    pollRecord.StartDate = null;
                }
                else
                {
                    poll.StartDate.Time = "00:00:00";
                    try
                    {
                        pollRecord.StartDate = _dateLocalizationServices.ConvertFromLocalizedString(poll.StartDate.Date + " " + poll.StartDate.Time);
                    }
                    catch (FormatException)
                    {
                        ModelState.AddModelError("InvalidStart", "Invalid start date");
                        return false;
                    }
                }

                if (String.IsNullOrWhiteSpace(poll.EndDate.Date))
                {
                    pollRecord.EndDate = null;
                }
                else
                {
                    poll.EndDate.Time = "23:59:59";
                    try
                    {
                        pollRecord.EndDate = _dateLocalizationServices.ConvertFromLocalizedString(poll.EndDate.Date + " " + poll.EndDate.Time);
                    }
                    catch (FormatException)
                    {
                        ModelState.AddModelError("InvalidStart", "Invalid end date");

                        return false;
                    }
                }

                var idsToDlete = pollRecord.Answers.Select(a => a.Id).Except(poll.Answers.Select(a => a.Id)).ToList();
                foreach (var i in idsToDlete)
                {
                    var answ = pollRecord.Answers.First(a => a.Id == i);
                    pollRecord.Answers.Remove(answ);
                }

                foreach (var answer in poll.Answers)
                {
                    var existentAnswer = pollRecord.Answers.Where(a => a.Id > 0).FirstOrDefault(a => a.Id == answer.Id);
                    if (existentAnswer == null) //add new
                    {
                        var pollAnswer = new PollAnswerRecord
                        {
                            Id = answer.Id,
                            Answer = answer.Answer,
                            Votes = answer.Votes
                        };
                        pollRecord.Answers.Add(pollAnswer);
                    }
                    else
                    { //update
                        existentAnswer.Answer = answer.Answer;
                        existentAnswer.Votes = answer.Votes;
                    }
                }
                _pollService.SaveOrUpdate(pollRecord);
            }
            return ModelState.IsValid;
        }
Ejemplo n.º 4
0
        public ActionResult Create(PollDetailsViewModel poll)
        {
            if (ProcessPollPersistence(poll))
            {
                return RedirectToAction("Index");
            }
            poll.StartDate.ShowDate = true;
            poll.StartDate.ShowTime = false;
            poll.EndDate.ShowDate = true;
            poll.EndDate.ShowTime = false;

            return View(poll);
        }