public void AddCategory(String categoryName, String categoryDescription, String parentCategory) { String slug = categoryName.Slugify(); if (!String.IsNullOrEmpty(parentCategory)) { //Verify parent category exists. if (!categories.Any(c => c.Name.Equals(parentCategory, StringComparison.OrdinalIgnoreCase))) { throw new ArgumentException("There is not category named " + parentCategory + " to be used as parent", "parentCategory"); } } //Verify no category with the same name in the system if (categories.Any(c => c.Name.Equals(categoryName, StringComparison.OrdinalIgnoreCase))) { throw new ArgumentException("There is already a category named " + categoryName, "categoryName"); } Category parent = null; if (!String.IsNullOrEmpty(parentCategory)) { parent = categories .Where(c => c.Name.Equals(parentCategory, StringComparison.OrdinalIgnoreCase)) .Single(); } String path = parent == null ? categoryName : parent.Path + "/" + categoryName; String parentName = parent == null ? String.Empty : parent.Name; CategoryAdded evt = new CategoryAdded(categoryName, parentName, slug, categoryDescription, path); RaiseEvent(evt); }
private void Apply(CategoryAdded evt) { Category newCategory = new Category() { Name = evt.Name, Description = evt.Description, Slug = evt.Slug, Parent = categories.Single(c => c.Name.Equals(evt.Parent, StringComparison.OrdinalIgnoreCase)), Path = evt.Path, }; categories.Add(newCategory); }