Ejemplo n.º 1
0
        public bool AnswerQuestion(int QuestionID, int AnswerID, string uniqueID)
        {
            if (BCryptHelper.CheckPassword("amazinglyunique", uniqueID))
            {
                if (!db.AnsweredBy.Any(x => x.AnswerID == AnswerID && x.BrowserKey == uniqueID && x.QuestionID == QuestionID) && db.Answers.Any(x=>x.AnswerID ==AnswerID) && db.Questions.Any(x => x.QuestionID == QuestionID))
                {
                    AnsweredBy ab = new AnsweredBy();
                    ab.AnswerID = AnswerID;
                    ab.BrowserKey = uniqueID;
                    ab.QuestionID = QuestionID;
                    db.AnsweredBy.Add(ab);
                    db.SaveChanges();

                    Answer a = new Answer();

                    a = db.Answers.First(x => x.AnswerID == AnswerID);
                    a.Count = a.Count + 1;

                    db.SaveChanges();

                    return true;
                }

                return false;
            }

            return false;
        }
Ejemplo n.º 2
0
        public ActionResult Index([Bind(Include = "Question, Answers, ShortURL")] CreatePollViewModel cpvm)
        {
            var currUserID = -1;

            if (User.Identity.IsAuthenticated)
            {
                currUserID = db.Users.First(x => x.Email == User.Identity.Name).UserID;
            }

            if (db.Questions.Any(q => q.ShortURL == cpvm.ShortURL && q.CreatedByUserID != currUserID))
            {
                ModelState.AddModelError("ShortURL", "ShortURL in use..Maybe you forgot to login?");
            }

            if (ModelState.IsValid)
            {
                Question newQuestion = new Question();

                newQuestion.QuestionText = cpvm.Question.ToString().Replace(System.Environment.NewLine, "<br />");

                Random rnd = new Random();

                if(cpvm.ShortURL == null)
                {
                    newQuestion.ShortURL = generateRandom(6, rnd);
                }
                else
                {
                    newQuestion.ShortURL = cpvm.ShortURL;
                }

                newQuestion.Password = generateRandom(6, rnd);

                newQuestion.CreatedDateTime = DateTime.Now;

                if(User.Identity.IsAuthenticated)
                {
                    User u = db.Users.First(x => x.Email == User.Identity.Name);
                    newQuestion.CreatedByUserID = u.UserID;
                }

                newQuestion.Active = true;
                db.Questions.Add(newQuestion);

                db.SaveChanges();

                //int id = newQuestion.Id;

                cpvm.Answers = cpvm.Answers.Where(x => !string.IsNullOrEmpty(x.Value)).ToArray();

                Answer[] newAnswer = new Answer[cpvm.Answers.Length];

                for (int ndx = 0; ndx < cpvm.Answers.Length; ndx++)
                {
                    newAnswer[ndx] = new Answer();

                    newAnswer[ndx].QuestionID = newQuestion.QuestionID;
                    newAnswer[ndx].AnswerText = cpvm.Answers[ndx].Value;

                    db.Answers.Add(newAnswer[ndx]);
                }

                db.SaveChanges();

                //PollViewModel pvm = new PollViewModel();
                //pvm.Question = cpvm.Question;
                //pvm.Answers = cpvm.Answers.Select(x => x.Value).ToArray();
                //return RedirectToAction("AdminPoll", new { password = newQuestion.Password });

                if(User.Identity.IsAuthenticated)
                {
                    return RedirectToAction("ManagePolls");
                }
                else
                {
                    return RedirectToAction("AdminPoll", new { password = newQuestion.Password });
                }

            }

            return View();
        }