protected int GetLikes(Article item) { if (item.Likes.Count > 0) { return item.Likes.Sum(l => l.Value); } return 0; }
protected int GetCurrentUserVote(Article item) { string userID = User.Identity.GetUserId(); Like like = item.Likes.FirstOrDefault(l => l.UserID == userID); if (like == null) { return 0; } return like.Value; }
protected bool? GetUserVote(Article item) { string userID = this.User.Identity.GetUserId(); var like = item.Likes.FirstOrDefault(l => l.UserID == userID); if (like == null) { return null; } return like.Value; }
public void ListViewArticles_InsertItem() { var item = new NewsSite.Models.Article(); item.DateCreated = DateTime.Now; item.AuthorID = User.Identity.GetUserId(); TryUpdateModel(item); if (ModelState.IsValid) { this.dbContext.Articles.Add(item); this.dbContext.SaveChanges(); } }
public void ListViewArticles_InsertItem() { var item = new Article(); TryUpdateModel(item); item.DateCreated = DateTime.Now; item.AuthorId = db.Users.Where(u => u.UserName == this.Context.User.Identity.Name).FirstOrDefault().Id; if (ModelState.IsValid) { db.Articles.Add(item); db.SaveChanges(); } }
// The id parameter name should match the DataKeyNames value set on the control public void ListViewArticles_UpdateItem(int id) { NewsSite.Models.Article item = this.dbContext.Articles.Find(id); if (item == null) { // The item wasn't found ModelState.AddModelError("", String.Format("Item with id {0} was not found", id)); return; } TryUpdateModel(item); if (ModelState.IsValid) { this.dbContext.SaveChanges(); } }
protected string GetTitle(NewsSite.Models.Article article) { return(string.Format("<strong>{0}</strong> <i> by {1}</i>", Server.HtmlEncode(article.Title), Server.HtmlEncode(article.Author.UserName))); }
protected int GetLikesValue(Article item) { int likesCount = item.Likes.Count(l => l.Value == true); int hatesCount = item.Likes.Count(l => l.Value == false); return likesCount - hatesCount; }
// The id parameter name should match the DataKeyNames value set on the control public void ListViewArticles_DeleteItem(int id) { NewsSite.Models.Article item = this.dbContext.Articles.Find(id); this.dbContext.Articles.Remove(item); this.dbContext.SaveChanges(); }