protected void ButtonAddCategory_Click(object sender, EventArgs e) { if (!CanAddCategory()) { ErrorSuccessNotifier.AddWarningMessage("Only admins can add categories"); return; } var context = new ApplicationDbContext(); string newCatName = this.TextBoxNewCategory.Text; if (string.IsNullOrWhiteSpace(newCatName)) { ErrorSuccessNotifier.AddErrorMessage("Please enter category name"); return; } else if (context.Categories.FirstOrDefault(c => c.Name == newCatName) != null) { ErrorSuccessNotifier.AddErrorMessage("Category with this name already exist"); return; } else { Models.Category cat = new Models.Category() { Name = newCatName }; context.Categories.Add(cat); context.SaveChanges(); ErrorSuccessNotifier.AddSuccessMessage("Category added"); } }
protected void ButtonVoteDown_Click(object sender, EventArgs e) { if (this.Context.User.Identity.IsAuthenticated) { string currentUserId = this.GetCurrentUserId(); var currentArticle = this.GetCurrentArticle(); if (currentArticle != null) { bool hasVoted = this.HasVoted(currentArticle, currentUserId); if (hasVoted) { ErrorSuccessNotifier.AddWarningMessage("You cannot vote twice on one article."); } else { var like = new NewsSite.Web.Models.Like { Value = -1, UserId = currentUserId }; currentArticle.Likes.Add(like); this.db.SaveChanges(); this.CalculateLikeValue(currentArticle); } } else { Response.Redirect("~/"); } } }