コード例 #1
0
 public IActionResult answerPollApi(int pId, string answer, string userId)
 {
     if (pId != null && answer != null && userId != null)
     {
         PollAnswers answers = new PollAnswers()
         {
             Answer = answer,
             PollId = pId,
             UserId = userId
         };
         dbContext.pollAnswers.Add(answers);
         dbContext.SaveChanges();
         return(Ok());
     }
     return(BadRequest());
 }
コード例 #2
0
ファイル: PollWidget.cs プロジェクト: levanvunam/EasyCMS
        public PollWidget(Poll poll)
            : this()
        {
            Id           = poll.Id;
            PollQuestion = poll.PollQuestion;
            PollSummary  = poll.PollSummary;
            IsMultiple   = poll.IsMultiple;
            ThankyouText = poll.ThankyouText;
            var pollAnswers = poll.PollAnswers.OrderBy(p => p.RecordOrder).ToList();

            foreach (var pollAnswer in pollAnswers)
            {
                TotalVotes += pollAnswer.Total;
                PollAnswers.Add(new PollAnswerWidget(pollAnswer));
            }
            var cookieName = String.Format(EzCMSContants.VotedUserCookie, poll.Id);
            var pollCookie = HttpContext.Current.Request.Cookies[cookieName];

            IsVotedPoll = pollCookie != null;
        }
コード例 #3
0
        public JsonResult PostPollVote(string select, string pollId, string id)
        {
            string message = string.Empty;

            string _loginUser = User.Identity.Name.ToString();

            int    postId           = Convert.ToInt32(id);
            int    postPollId       = Convert.ToInt32(pollId);
            string postPollAnswerId = select;

            Post _tempPost = db.Posts.Where(x => x.PostId == postId).FirstOrDefault();

            PollPost _tempPollPost = db.PollPosts.Where(x => x.Id == postPollId).FirstOrDefault();

            User _CurrUser = db.Users.Where(x => x.UserName == _loginUser).FirstOrDefault();

            if (!String.IsNullOrEmpty(select) && _tempPost != null && _CurrUser != null)
            {
                try
                {
                    if (_tempPollPost != null)
                    {
                        bool isFound = false;

                        User prePollUser;

                        if (_tempPollPost.PollAnswers.Count() > 0)
                        {
                            var vPollAns = _tempPollPost.PollAnswers.ToList();

                            var vPollUsrs = from ans in vPollAns
                                            from usr in ans.Users.ToList()
                                            select usr;

                            if (vPollUsrs.Count() > 0)
                            {
                                var tempPollUsr = from tempans in vPollAns
                                                  from tempusr in tempans.Users.Where(x => x.UserName == _loginUser)
                                                  select tempusr;

                                prePollUser = tempPollUsr.FirstOrDefault();

                                if (tempPollUsr.Count() > 0)
                                {
                                    isFound = true;

                                    List <User> lstUser = new List <User>();
                                    lstUser.Add(_CurrUser);

                                    var _tempPollAnsForRemove = from tempFrR in vPollAns
                                                                from tempPollAnsFrRemove in tempFrR.Users.Where(x => x.UserName == _loginUser)
                                                                select tempFrR;

                                    PollAnswers _tempPollAnsRemove = _tempPollAnsForRemove.FirstOrDefault();
                                    _tempPollAnsRemove.AnswerCount -= 1;
                                    _tempPollAnsRemove.Users.Remove(_CurrUser);

                                    db.SaveChanges();

                                    var chkPollAns = from tempForTest in vPollAns
                                                     from tempPollAnsForTest in tempForTest.Users.Where(x => x.UserName == _loginUser)
                                                     select tempForTest;

                                    if (chkPollAns.Count() == 0)
                                    {
                                        PollAnswers _tempPollAnswer = _tempPollPost.PollAnswers.Where(x => x.Id == postPollAnswerId).FirstOrDefault();
                                        _tempPollAnswer.AnswerCount += 1;
                                        _tempPollAnswer.Users        = lstUser;

                                        db.PollAnswers.Attach(_tempPollAnswer);
                                        db.Entry(_tempPollAnswer).State = EntityState.Modified;

                                        db.SaveChanges();
                                    }
                                }
                            }
                        }

                        if (isFound == false)
                        {
                            List <User> lstUser = new List <User>();
                            lstUser.Add(_CurrUser);

                            PollAnswers _tempPollAnswer = _tempPollPost.PollAnswers.Where(x => x.Id == postPollAnswerId).FirstOrDefault();
                            _tempPollAnswer.AnswerCount += 1;
                            _tempPollAnswer.Users        = lstUser;

                            db.PollAnswers.Attach(_tempPollAnswer);
                            db.Entry(_tempPollAnswer).State = EntityState.Modified;

                            _tempPollPost.TotalVote += 1;

                            db.PollPosts.Attach(_tempPollPost);
                            db.Entry(_tempPollPost).State = EntityState.Modified;

                            db.SaveChanges();
                        }
                    }
                    else
                    {
                        message = "Do not insert";
                    }
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                }
            }
            else
            {
                message = "Do not insert";
            }

            return(Json(new
            {
                Html = this.RenderPartialView("_PollVoteResult", _tempPollPost),
                Message = message
            }, JsonRequestBehavior.AllowGet));

            //return PartialView("_Post", _post);
        }
コード例 #4
0
        public JsonResult PostPoll(string content, string answers)
        {
            string message = string.Empty;

            Post _post = new Post();

            string _loginUser = User.Identity.Name.ToString();

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            //for item list
            List <string> itemAnswer = serializer.Deserialize <List <string> >(answers);

            //User _user = db.Users.Where(x => x.UserName == _loginUser).FirstOrDefault();

            User _CurrUser = db.Users.Where(x => x.UserName == _loginUser).FirstOrDefault();

            if (!String.IsNullOrEmpty(content))
            {
                string _postContent = content;

                _post.Content = _postContent;
                //_post.CreatedBy = "rasel";
                _post.CreatedDate = DateTime.Now;
                _post.PostTypeId  = 4; //post type for poll type id

                _post.User = _CurrUser;

                try
                {
                    //one to one relational data insert
                    db.Posts.Add(_post);
                    db.SaveChanges();

                    _post.PollPost = new PollPost {
                        PostId = _post.PostId
                    };
                    db.SaveChanges();

                    PollPost _tempPoll = db.PollPosts.Where(x => x.PostId == _post.PostId).FirstOrDefault();

                    if (_tempPoll != null)
                    {
                        int _pAnsId = db.PollAnswers.Count();

                        foreach (var item in itemAnswer.ToList())
                        {
                            _pAnsId++;
                            PollAnswers _pollAnswer = new PollAnswers();
                            _pollAnswer.Id            = "" + _pAnsId;
                            _pollAnswer.AnswerContent = item;
                            _pollAnswer.PollPostId    = _tempPoll.Id;
                            db.PollAnswers.Add(_pollAnswer);
                        }
                        db.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                }
            }
            else
            {
                message = "error!";
            }

            return(Json(new
            {
                Html = this.RenderPartialView("_PostPoll", _post),
                Message = message
            }, JsonRequestBehavior.AllowGet));

            //return PartialView("_Post", _post);
        }
コード例 #5
0
            public void Insert(Guid PollAnswerId, Guid QuestionId, string Answer, Nullable<int> Rank)
            {
                PollAnswers item = new PollAnswers();

                item.PollAnswerId = PollAnswerId;

                item.QuestionId = QuestionId;

                item.Answer = Answer;

                item.Rank = Rank;

                item.Save(UserName);
            }