public PartialViewResult SetLike(CommentLikesViewModel model) { if (Session["oAuthUser"] != null) { OAuthUser oAuthUser = (OAuthUser)Session["oAuthUser"]; Like like = _likeRepository.Likes.Where(l => l.CommentId == model.CommentId && l.UserId == oAuthUser.UserId && l.UserProvider == oAuthUser.Provider).FirstOrDefault(); if (like == null) { like = new Like(); like.UserId = oAuthUser.UserId; like.UserProvider = oAuthUser.Provider; like.CommentId = model.CommentId; _likeRepository.Save(like); } else { _likeRepository.Delete(like.Id); } } model.LikesCount = _commentRepository.GetById(model.CommentId).Likes.Count; return PartialView("~/Views/Posts/_Like.cshtml", model); }
public void Save(Like like) { if (like.Id == 0) { _db.Likes.Add(like); } else { Like dbEntry = _db.Likes.Find(like.Id); if (dbEntry != null) { dbEntry.CommentId = like.CommentId; dbEntry.UserId = like.UserId; dbEntry.UserProvider = like.UserProvider; } } _db.SaveChanges(); }