public async Task <bool> HandleAsync(CreateItemCategoryCommand command, CancellationToken cancellationToken) { if (command is null) { throw new ArgumentNullException(nameof(command)); } var model = itemCategoryFactory.Create(new ItemCategoryId(0), command.Name, false); await itemCategoryRepository.StoreAsync(model, cancellationToken); return(true); }
public async Task <bool> HandleAsync(DeleteItemCategoryCommand command, CancellationToken cancellationToken) { if (command is null) { throw new System.ArgumentNullException(nameof(command)); } var category = await itemCategoryRepository.FindByAsync(command.ItemCategoryId, cancellationToken); if (category == null) { throw new DomainException(new ItemCategoryNotFoundReason(command.ItemCategoryId)); } category.Delete(); var storeItems = (await itemRepository.FindActiveByAsync(command.ItemCategoryId, cancellationToken)) .ToList(); using var transaction = await transactionGenerator.GenerateAsync(cancellationToken); foreach (var item in storeItems) { var lists = await shoppingListRepository.FindActiveByAsync(item.Id, cancellationToken); foreach (var list in lists) { list.RemoveItem(item.Id); await shoppingListRepository.StoreAsync(list, cancellationToken); } item.Delete(); await itemRepository.StoreAsync(item, cancellationToken); } await itemCategoryRepository.StoreAsync(category, cancellationToken); await transaction.CommitAsync(cancellationToken); return(true); }