Example #1
0
        public ActionResult Create(Guid QuestionId, string UserId, string Title, string Slug, string Content, string data)
        {
            if (ModelState.IsValid)
            {
                Answere answere = new Answere();
                answere.Id = Guid.NewGuid();
                answere.Title = Title;
                answere.Slug = Slug;
                answere.Content = Content;
                answere.QuestionId = QuestionId;
                answere.UserId = UserId;
                _dbContext.Answers.Add(answere);

                var tags = _dbContext.Tags;
                CreateNSaveTag(data);


                var deserialiseList = JsonConvert.DeserializeObject<List<Tag>>(data);
                foreach (var tag in deserialiseList)
                {
                    Tag retriveTag = tags.Where(t => t.Name == tag.Name).FirstOrDefault();
                    answere.Tags.Add(retriveTag);
                }

                _dbContext.SaveChanges();
                return RedirectToAction("Index", "Questions");
            }

            return View();
        }
        public ActionResult MakeAVote(bool isUpvote, int?questionId, int?answereId)
        {
            int    voteAmount = 0;
            string msg        = null;

            if (answereId != null)
            {
                Answere answere = db.Answeres.Find(answereId);

                if (answere.ApplicationUserId == User.Identity.GetUserId())
                {
                    msg = "You can't vote yourself!";
                }

                if (answere.ApplicationUserId != User.Identity.GetUserId() &&
                    answere.Votes.FirstOrDefault(vote => vote.ApplicationUserId == User.Identity.GetUserId()) == null)
                {
                    if (isUpvote)
                    {
                        answere.VotesNum++;
                    }
                    else
                    {
                        answere.VotesNum--;
                    }

                    answere.Votes.Add(new Vote {
                        ApplicationUserId = User.Identity.GetUserId()
                    });
                }

                voteAmount = answere.VotesNum;
                answere.ApplicationUser.ReputationValue = voteAmount * 5;
            }
            else
            {
                Question question = db.Questions.Find(questionId);


                if (question.ApplicationUserId == User.Identity.GetUserId())
                {
                    msg = "You can't vote yourself!";
                }

                if (question.ApplicationUserId != User.Identity.GetUserId() &&
                    question.Votes.FirstOrDefault(vote => vote.ApplicationUserId == User.Identity.GetUserId()) == null)
                {
                    if (isUpvote)
                    {
                        question.VotesNum++;
                    }
                    else
                    {
                        question.VotesNum--;
                    }
                    question.Votes.Add(new Vote {
                        ApplicationUserId = User.Identity.GetUserId()
                    });
                }

                voteAmount = question.VotesNum;
                question.ApplicationUser.ReputationValue = voteAmount * 5;
            }
            db.SaveChanges();

            return(Json(new { voteAmount, msg }, JsonRequestBehavior.AllowGet));
        }
Example #3
0
        public ActionResult Create(AnswereViewModel viewModel, params string[] rolesSelectedOnView)
        {
            if (ModelState.IsValid)
            {
                //  string html2 = Regex.Matches(viewModel.Content, @"(<img\/?[^>]+>)", @"", RegexOptions.IgnoreCase);
                //HtmlDocument doc = new HtmlDocument();
                //doc.LoadHtml(viewModel.Content);
                //HtmlNodeCollection linkNodes = doc.DocumentNode.SelectNodes("//img/@src");                               
                //if (linkNodes != null)
                //{
                //    foreach (HtmlNode linkNode in linkNodes)
                //    {
                //        HtmlAttribute attrib = linkNode.Attributes["src"];

                //        Base64Decode(attrib.Value);
                //    }
                //}

                //  SaveByteArrayAsImage(path, viewModel.Content);

                Answere answere = new Answere();
                answere.Id = Guid.NewGuid();
                answere.Title = viewModel.Title;
                answere.Slug = viewModel.Slug;
                answere.Content = viewModel.Content;
                answere.QuestionId = viewModel.QuestionId;
                answere.UserId = viewModel.UserId;
                _dbContext.Answers.Add(answere);

                var tags = _dbContext.Tags;
                // CreateNSaveTag(data);
                if(rolesSelectedOnView != null)
                {
                    foreach (var tag in rolesSelectedOnView)
                    {
                        Tag retriveTag = tags.Where(t => t.Name == tag).FirstOrDefault();
                        answere.Tags.Add(retriveTag);
                    }
                }
                //var deserialiseList = JsonConvert.DeserializeObject<List<Tag>>(data);             

                _dbContext.SaveChanges();
                return RedirectToAction("Index", "Questions");
            }

            return View();
        }