private Post DoLikeOrComment(long postId) { Post post = db.Posts.Where(p => p.Id == postId).First(); string userId = User.Identity.GetUserId(); string like_com = Request.Params["like_com"]; if (like_com == "Like") { if (db.LikeActivities.Where(activity => activity.PostId == postId && activity.ApplicationUserId == userId).Count() == 0) { LikeActivity act = new LikeActivity(); act = new LikeActivity(); act.ApplicationUserId = userId; act.PostId = postId; act.Like = true; db.LikeActivities.Add(act); post.TotalLikes++; } else { LikeActivity likeAct = db.LikeActivities.Where(activity => activity.PostId == postId && activity.ApplicationUserId == userId).First(); if (!likeAct.Like) { post.TotalLikes++; } likeAct.Like = true; ViewBag.Like = "UnLike"; } db.SaveChanges(); } else if (like_com == "UnLike") { LikeActivity likeAct = db.LikeActivities.Where(activity => activity.PostId == postId && activity.ApplicationUserId == userId).First(); likeAct.Like = false; ViewBag.Like = "Like"; post.TotalLikes--; db.SaveChanges(); } else if (like_com == "Comment") { string comment = Request.Params["UserComment"]; if (!string.IsNullOrWhiteSpace(comment)) { CommentActivity act = new CommentActivity(); act.ApplicationUserId = userId; act.PostId = postId; act.Comment = comment; act.User = db.Users.Where(us => us.Id == userId).First(); db.CommentActivities.Add(act); post.TotalComments++; db.SaveChanges(); } } return(post); }
public ActionResult LikeComment() { string userId = Request.Params["userId"]; string sortOrder = Request.Params["sortOrder"]; long commentId = long.Parse(Request.Params["commentId"]); long postId = long.Parse(Request.Params["postId"]); CommentActivity comment = db.CommentActivities.Where(c => c.CommentId == commentId).First(); //comment.User = db.Users.Where(us => us.Id == comment.ApplicationUserId).First(); if (comment.LikeActivities.Where(c => c.CommentId == commentId).Count() == 0) { LikeActivity act = new LikeActivity(); act = new LikeActivity(); //act.Id = db.LikeActivities.Count() + 1; act.ApplicationUserId = userId; act.PostId = postId; act.CommentId = commentId; act.Like = true; //db.LikeActivities.Add(act); comment.LikeActivities.Add(act); comment.TotalLikes++; } else { LikeActivity act = comment.LikeActivities.Where(c => c.CommentId == commentId).First(); if (!act.Like) { comment.TotalLikes++; } act.CommentId = commentId; act.Like = true; // ViewBag.LikeComment = "UnLike"; } try { db.SaveChanges(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } return(RedirectToAction("Index", "Home")); }
public ActionResult UnLikeComment() { //string userId = Request.Params["userId"]; string sortOrder = Request.Params["sortOrder"]; long commentId = long.Parse(Request.Params["commentId"]); long postId = long.Parse(Request.Params["postId"]); CommentActivity comment = db.CommentActivities.Where(c => c.CommentId == commentId).First(); LikeActivity likeAct = comment.LikeActivities.Where(c => c.CommentId == commentId).First(); likeAct.Like = false; //ViewBag.LikeComment = "Like"; comment.TotalLikes--; db.SaveChanges(); return(RedirectToAction("Index", "Home")); }
public ActionResult LikeOrComment(string returnUrl) { //long postId = long.Parse(Request.Form["postId"]); //string sortOrder = Request.Form["sortOrder"]; //string pageNo = Request.Form["pageNo"]; //Post post = db.Posts.Where(p => p.Id == postId).First(); //string userId = User.Identity.GetUserId(); //string like_com = Request.Form["like_com"]; long postId = long.Parse(Request.Params["postId"]); string sortOrder = Request.Params["sortOrder"]; string pageNo = Request.Params["pageNo"]; Post post = db.Posts.Where(p => p.Id == postId).First(); string userId = User.Identity.GetUserId(); string like_com = Request.Params["like_com"]; if (like_com == "Like") { if (db.LikeActivities.Where(activity => activity.PostId == postId && activity.ApplicationUserId == userId).Count() == 0) { LikeActivity act = new LikeActivity(); act = new LikeActivity(); //act.Id = db.LikeActivities.Count() + 1; act.ApplicationUserId = userId; act.PostId = postId; act.Like = true; db.LikeActivities.Add(act); post.TotalLikes++; } else { LikeActivity likeAct = db.LikeActivities.Where(activity => activity.PostId == postId && activity.ApplicationUserId == userId).First(); if (!likeAct.Like) { post.TotalLikes++; } likeAct.Like = true; ViewBag.Like = "UnLike"; } db.SaveChanges(); } else if (like_com == "UnLike") { LikeActivity likeAct = db.LikeActivities.Where(activity => activity.PostId == postId && activity.ApplicationUserId == userId).First(); likeAct.Like = false; ViewBag.Like = "Like"; post.TotalLikes--; db.SaveChanges(); } else if (like_com == "Comment") { string comment = Request.Params["UserComment"]; if (!string.IsNullOrWhiteSpace(comment)) { CommentActivity act = new CommentActivity(); act.ApplicationUserId = userId; act.PostId = postId; act.Comment = comment; act.User = db.Users.Where(us => us.Id == User.Identity.GetUserId()).First(); db.CommentActivities.Add(act); post.TotalComments++; db.SaveChanges(); } } return(RedirectToAction("Index", "Post", new { sortOrder = "date", page = 1 })); }