public void ListViewCategories_UpdateItem(int categoryId) { var category = this.context.Categories.Find(categoryId); if (category == null) { this.ModelState.AddModelError("", String.Format("Item with id {0} was not found", categoryId)); return; } this.TryUpdateModel(category); if (string.IsNullOrEmpty(category.Name)) { ErrorSuccessNotifier.AddMessage(new NotificationMessage() { Text = "Cannot create category with invalid name.", AutoHide = false, Type = MessageType.Warning }); return; } var categoryNameToLower = category.Name.ToLower(); var hasCategoryAlready = this.context.Categories.Any(c => c.Name.ToLower() == categoryNameToLower); if (hasCategoryAlready) { ErrorSuccessNotifier.AddMessage(new NotificationMessage() { Text = string.Format(@"Category with name ""{0}"" already exists!", category.Name), AutoHide = false, Type = MessageType.Warning }); return; } if (this.ModelState.IsValid) { this.TryUpdateOrShowMessage(new NotificationMessage() { Text = "Category was updated successfully.", AutoHide = false, Type = MessageType.Success }); } }
private void TryUpdateOrShowMessage(NotificationMessage msg) { try { this.context.SaveChanges(); ErrorSuccessNotifier.AddMessage(msg); } catch (Exception) { ErrorSuccessNotifier.AddMessage(new NotificationMessage() { Text = "Unhandled exception: something bad happened.", AutoHide = false, Type = MessageType.Error }); } }
public void ListViewArticles_InsertItem() { var articleProjection = new ArticleProjection(); this.TryUpdateModel(articleProjection); var categoryId = int.Parse(articleProjection.CategoryName); if (string.IsNullOrEmpty(articleProjection.Title) || string.IsNullOrEmpty(articleProjection.Content)) { ErrorSuccessNotifier.AddMessage(new NotificationMessage() { Text = "Cannot create article with emptry title/content.", AutoHide = false, Type = MessageType.Warning }); return; } var article = new Article() { Title = articleProjection.Title, Content = articleProjection.Content, AuthorId = this.userIdProvider.GetUserId(), DateCreated = DateTime.Now, CategoryId = categoryId }; if (this.ModelState.IsValid) { this.context.Articles.Add(article); this.TryUpdateOrShowMessage(new NotificationMessage() { Text = "Article was created successfully.", AutoHide = false, Type = MessageType.Success }); } }