Exemplo n.º 1
0
        public void SaveBump(Bump bump)
        {
            if (bump == null)
                return;

            //if bump id is 0, it means this is a new bump obj
            if (bump.BumpID == 0)
            {
                bump.DateCreated = DateTime.Now;
                context.Bumps.Add(bump);
            }
            else
            {
                bump.DateModified = DateTime.Now;
            }

            context.SaveChanges();
        }
Exemplo n.º 2
0
        public ActionResult Bump(int qid, string direction)
        {
            //make sure 'direction'is valid
            if (direction == "up" || direction == "down" || direction == "reset")
            {

                Guid currentUserId = _membershipService.GetCurrentUserId();

                //check whether question exists
                Question question = _questionRepo.Questions.Where(q => q.QuestionID == qid).FirstOrDefault();
                if (question != null)
                {

                    //check whether the user has already bumped this question
                    Bump alreadyBumped = _bumpRepo.Bumps.Where(b => b.QuestionID == qid && b.UserID == currentUserId).FirstOrDefault();
                    if (alreadyBumped == null && direction != "reset")
                    {
                        if (question != null)
                        {
                            //hasnt been bumped, so record the bump
                            Bump newBump = new Bump();
                            newBump.QuestionID = qid;
                            newBump.UserID = currentUserId;

                            if (direction == "up")
                            {
                                newBump.BumpUpValue = 1;
                                //update question value as well
                                question.BumpUpValue += 1;
                            }
                            else
                            {
                                newBump.BumpDownValue = 1;
                                //update question value as well
                                question.BumpDownValue += 1;
                            }

                            _bumpRepo.SaveBump(newBump);
                            _questionRepo.SaveQuestion(question);

                        }
                    }
                    else
                    {
                        //check what the previous bump value was, if user already bumped up, then do nothing since already been bumped up
                        //if user has bumped it down before, then update the bump to a bump up
                        if (direction == "up")
                        {
                            if (alreadyBumped.BumpDownValue > 0)
                            {
                                alreadyBumped.BumpDownValue = 0;
                                alreadyBumped.BumpUpValue = 1;
                                _bumpRepo.SaveBump(alreadyBumped);

                                question.BumpDownValue -= 1; //remove 1 bump down since user changed their bump to a bump up
                                question.BumpUpValue += 1;
                            }
                            else if (alreadyBumped.BumpUpValue == 0)
                            {
                                //this case checks whether the user may have 'reset'their bump so now it's back to zero
                                alreadyBumped.BumpDownValue = 0;
                                alreadyBumped.BumpUpValue = 1;
                                _bumpRepo.SaveBump(alreadyBumped);

                                question.BumpUpValue += 1;
                            }
                        }
                        else if (direction == "down")
                        {
                            //check whether user had previously bumped it up, if so, we'll need to subtract 1 from the dump value
                            if (alreadyBumped.BumpUpValue > 0)
                            {
                                alreadyBumped.BumpUpValue = 0;
                                alreadyBumped.BumpDownValue = 1;
                                _bumpRepo.SaveBump(alreadyBumped);

                                question.BumpUpValue -= 1;
                                question.BumpDownValue += 1;
                            }
                            else if (alreadyBumped.BumpDownValue == 0)
                            {
                                //this case checks whether the user may have 'reset'their dump so now it's back to zero
                                alreadyBumped.BumpUpValue = 0;
                                alreadyBumped.BumpDownValue = 1;
                                _bumpRepo.SaveBump(alreadyBumped);

                                question.BumpDownValue += 1;
                            }
                        }
                        else
                        {
                            if (alreadyBumped.BumpUpValue > 0)
                            {
                                question.BumpUpValue -= 1;
                            }
                            else if(alreadyBumped.BumpDownValue > 0)
                            {
                                question.BumpDownValue -= 1;
                            }

                            alreadyBumped.BumpUpValue = alreadyBumped.BumpDownValue = 0;
                            _bumpRepo.SaveBump(alreadyBumped);
                        }
                        _questionRepo.SaveQuestion(question);
                    }

                    return Json("success", JsonRequestBehavior.AllowGet);
                }
            }

            return Json("failed", JsonRequestBehavior.AllowGet);
        }