Exemplo n.º 1
0
        public ActionResult GetPoll(int?id)
        {
//			id = id.GetValueOrDefault(54);

/*
 *                      if (!id.HasValue) {
 *                              id = PollService.FirstOrDefault(x => x.IsActive, x => x.PollID);
 *                              if (id == 0) {
 *                                      return null;
 *                              }
 *                      }
 */
            PollService.LoadWith(x => x.PollOptions);
            var activePolls = ActivePollIds();
            var poll        = id > 0 ? PollService.GetByPK(id.Value): PollService.GetByPK(activePolls.FirstOrDefault());

            if (poll == null)
            {
                return(null);
            }
            var index  = activePolls.IndexOf(poll.PollID);
            var nextId = index < 0 || index == (activePolls.Count - 1) ? 0 : activePolls[index + 1];
            var model  = new PollVM {
                Poll       = poll,
                NextPollId = nextId
            };

            return(View(PartialViewNames.PollBlock, model));
        }
Exemplo n.º 2
0
        public PollVM Create(string dictaatName, [FromBody] PollVM poll)
        {
            if (!AuthorizeResrouce(dictaatName))
            {
                return(null);
            }

            return(_pollRepository.CreatePoll(dictaatName, poll));
        }
Exemplo n.º 3
0
        public PollVM Update(string dictaatName, int pollId, [FromBody] PollVM poll)
        {
            if (!AuthorizeResource(dictaatName))
            {
                return(null);
            }

            return(_pollRepository.UpdatePoll(dictaatName, pollId, poll));
        }
Exemplo n.º 4
0
        public PollVM CreatePoll(string dictaatName, PollVM poll)
        {
            Poll p = new Poll();

            p.Question    = poll.Question;
            p.DictaatName = dictaatName;
            p.Options     = poll.Options.Select(o => new PollOption()
            {
                Text = o.Text
            }).ToList();

            _context.Polls.Add(p);
            _context.SaveChanges();
            return(new PollVM(p));
        }
Exemplo n.º 5
0
        public PollVM UpdatePoll(string dictaatName, int pollId, PollVM newPoll)
        {
            Poll oldPoll = _context.Polls
                           .Include("Options")
                           .FirstOrDefault(q => q.Id == pollId && q.DictaatName == dictaatName);

            if (oldPoll == null)
            {
                return(null);
            }

            oldPoll.Question = newPoll.Question;

            var options = oldPoll.Options;

            //remove
            oldPoll.Options.ToList().ForEach(o =>
            {
                if (!newPoll.Options.Any(npo => npo.Id == o.Id))
                {
                    options.Remove(o);
                }
            });


            //add op update
            newPoll.Options.ToList().ForEach(no =>
            {
                if (no.Id != 0)
                {
                    options.FirstOrDefault(o => o.Id == no.Id).Text = no.Text;
                }
                else
                {
                    options.Add(new PollOption()
                    {
                        Text = no.Text
                    });
                }
            });

            oldPoll.Options = options;
            _context.SaveChanges();
            return(new PollVM(oldPoll));
        }