protected void Up_Click(object sender, EventArgs e) { var like = new Like() { ArticleId = int.Parse(this.ArticleId), ReaderId = this.ReaderId, Value = 1 }; var current = db.Articles.Find(this.ArticleId); if (current.Likes.All(l => l.ReaderId != this.ReaderId)) { current.Likes.Add(new Like()); db.SaveChanges(); } }
protected void ButtonDislike_Click(object sender, EventArgs e) { var articleIdAsString = this.Request.QueryString["id"]; var newLike = new Like() { Value = false, ArticleId = int.Parse(articleIdAsString), AuthorId = Page.User.Identity.GetUserId() }; NewsSystemDbContext db = new NewsSystemDbContext(); db.Likes.Add(newLike); db.SaveChanges(); Response.Redirect(Request.RawUrl); }
protected void LikeControl_Like(object sender, LikeEventArgs e) { Article article = this.dbContext.Articles.Find(Convert.ToInt32(e.DataID)); string userID = this.User.Identity.GetUserId(); Like like = article.Likes.FirstOrDefault(l => l.UserID == userID); if (like == null) { like = new Like() { UserID = userID, ArticleID = Convert.ToInt32(e.DataID) }; article.Likes.Add(like); } like.Value = e.LikeValue; this.dbContext.SaveChanges(); //LikeControl ctrl = sender as LikeControl; DataBind(); }