Ejemplo n.º 1
0
        public ActionResult Index(CreateQuestionInputModel inputModel)
        {
            if (inputModel != null && this.ModelState.IsValid)
            {
                this.ValidateAnswers(inputModel.Answers);

                if (this.ModelState.IsValid)
                {
                    var context = new VotingSystemEntities();

                    var newQuestion = new Question {
                        Content = inputModel.Content, RequireNames = inputModel.RequireNames
                    };

                    this.AddQuestionAnswers(inputModel.Answers, newQuestion);

                    this.GenerateQuestionUrlId(newQuestion);

                    context.Questions.Add(newQuestion);
                    context.SaveChanges();

                    return(Redirect("/Vote/Index/" + newQuestion.UrlId));
                }
            }

            return(this.View("~/Views/Home/Index.cshtml", inputModel ?? new CreateQuestionInputModel()));
        }
Ejemplo n.º 2
0
        public ActionResult SubmitVote(SubmitVoteInputModel inputModel)
        {
            if (inputModel == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            if (string.IsNullOrWhiteSpace(inputModel.QuestionUrlId))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            var pollInfo = GetPollByUrlId(inputModel.QuestionUrlId);

            if (pollInfo.QuestionContent == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.NotFound));
            }
            if (inputModel.AnswerPicked >= pollInfo.Answers.Count || inputModel.AnswerPicked < 0)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            if (pollInfo.NamesRequired)
            {
                ValidateName(inputModel.FullName);
            }
            if (this.ModelState.IsValid)
            {
                var uidCookie = Request.Cookies["uid"];
                if (uidCookie == null)
                {
                    uidCookie = new HttpCookie("uid")
                    {
                        Expires = DateTime.Now.AddDays(90),
                        Value   = Guid.NewGuid().ToString()
                    };
                    this.Response.Cookies.Add(uidCookie);
                }


                var cookieID     = uidCookie.Value;
                var ip           = Request.UserHostAddress.ToString();
                var checkIfVoted = from votes in db.Votes
                                   where votes.QuestionId == pollInfo.QuestionId && (votes.Ip == ip || votes.SecretKey == cookieID)
                                   select votes;
                if (checkIfVoted.Any())
                {
                    pollInfo.AlrdyVoted = true;
                    return(View("~/Views/Vote/Index.cshtml", pollInfo));
                }
                var poll = from questions in db.Questions
                           join answers in db.Answers on questions.Id equals answers.QuestionId
                           where questions.UrlId == inputModel.QuestionUrlId
                           select questions;

                Vote v = new Vote();
                v.AnswerId   = poll.First().Answers.ElementAt(inputModel.AnswerPicked).Id;;
                v.FullName   = inputModel.FullName;
                v.Ip         = ip;
                v.QuestionId = poll.First().Id;
                v.SecretKey  = cookieID;
                db.Votes.Add(v);
                db.SaveChanges();
                return(Redirect("/Result/Index/" + inputModel.QuestionUrlId));
            }
            return(View("~/Views/Vote/Index.cshtml", pollInfo));
        }
 public void CreateTopic(Topic_Recording topic)
 {
     _context.Topic_Recording.Add(topic);
     _context.SaveChanges();
 }