Example #1
0
 public ActionResult UnlikeC(int? comment, int? user)
 {
     RedisUserRepository redisUser = new RedisUserRepository();
     RedisCommentRepository redisComment = new RedisCommentRepository();
     RedisOreRepository redisOre = new RedisOreRepository();
     if (comment == null || user == null)
     {
         return RedirectToAction("Index", "Ore");
     }
     if (Context.Session.GetString("user") != null)
     {
         User inQuestion = JsonConvert.DeserializeObject<User>(Context.Session.GetString("user"));
         if (inQuestion.PasswordHash != redisUser.ReadUser((int)user).PasswordHash)
         {
             return RedirectToAction("Index", "Ore");
         }
     }
     else
     {
         return RedirectToAction("Login", "Community");
     }
     User author = redisUser.ReadUser((int)user);
     Comment target = redisComment.ReadComment((int)comment);
     if (author.CommentsLiked.Contains((int)comment))
     {
         target.Likes -= 1;
         redisComment.CreateComment(target); // Will overwrite the comment already in the db
         author.CommentsLiked.Remove((int)comment);
         redisUser.UpdateUser(author);
     }
     ViewData["commentSuccess"] = false;
     Context.Session.SetString("user", JsonConvert.SerializeObject(author));
     return RedirectToAction("Info", "Ore", new { oreName = redisOre.ReadName(target.ParentOre).Name });
 }
Example #2
0
        public ActionResult Comment(string comment, int id)
        {
            RedisUserRepository redisUser = new RedisUserRepository();
            RedisOreRepository redisOre = new RedisOreRepository();
            RedisCommentRepository redisComment = new RedisCommentRepository();
            Ore parentOre = redisOre.ReadOre(id);

            if (Context.Session.GetString("user") == null)
            {
                return RedirectToAction("Login", "Community");
            }
            User author = JsonConvert.DeserializeObject<User>(Context.Session.GetString("user"));

            List<string> validation = new List<string>();
            if (comment == null)
            {
                validation.Add("Please enter your comment");
            }
            if (id == 0)
            {
                return RedirectToAction("Index", "Ore");
            }
            ViewData["validationSummary"] = validation;

            if(!validation.Any())
            {
                Comment target = new Comment();
                target.Content = comment;
                target.Author = author.UserName;
                target.ParentOre = parentOre.Name;
                redisComment.CreateComment(target);

                if(author.CommentsAuthored != null)
                {
                    author.CommentsAuthored.Add(target.Id);
                    redisUser.UpdateUser(author);
                }
                else
                {
                    List<int> commentsAuthored = new List<int>();
                    commentsAuthored.Add(target.Id);
                    author.CommentsAuthored = commentsAuthored;
                    redisUser.UpdateUser(author);
                }

                if(parentOre.Comments != null)
                {
                    parentOre.Comments.Add(target.Id);
                    redisOre.UpdateOre(parentOre);
                }
                else
                {
                    List<int> comments = new List<int>();
                    comments.Add(target.Id);
                    parentOre.Comments = comments;
                    redisOre.UpdateOre(parentOre);
                }
            }

            ViewData["commentSuccess"] = true;
            return View("Info", parentOre);
        }