Esempio n. 1
0
        public async Task Handle(UpdateCategoryProcessStartedEvent categoryChangedEvent)
        {
            var canSave = await this.SaveGuard(categoryChangedEvent);

            if (!canSave)
            {
                return;
            }

            var category = new CategoryDto(
                categoryChangedEvent.Id,
                categoryChangedEvent.Name,
                categoryChangedEvent.Description,
                categoryChangedEvent.SortOrder);

            var saveResult = await _categoryIndexer.Index(category);

            if (!saveResult.Success)
            {
                var failedEvent = new UpdatingCategoryFailedEvent(categoryChangedEvent.Id, CategoryModificationStatus.UnexpectedProblem, categoryChangedEvent.CorrelationId);
                await this.SaveAndDispatchEvent(categoryChangedEvent.Id, failedEvent);

                _logger.LogProgress(OperationStatus.Failed, "Problem occured while saving the category", categoryChangedEvent.CorrelationId);
                return;
            }

            var successEvent = this.GetSuccessEvent(categoryChangedEvent);

            _logger.LogProgress(OperationStatus.Finished, string.Empty, categoryChangedEvent.CorrelationId);
            await this.SaveAndDispatchEvent(categoryChangedEvent.Id, successEvent);
        }
        public async Task Handle(CreateCategoryProcessStartedEvent categoryChangedEvent)
        {
            var existingCategoryWithName = await _categorySearchHandler.GetByName(new GetCategoryByNameQuery(categoryChangedEvent.Name));

            if (existingCategoryWithName.Success)
            {
                var failedEvent = new CreatingCategoryFailedEvent(categoryChangedEvent.Id, CategoryModificationStatus.DuplicatedName, categoryChangedEvent.CorrelationId);
                await this.SaveAndDispatchEvent(categoryChangedEvent.Id, failedEvent);

                _logger.LogProgress(OperationStatus.Failed, $"Cannot save the category: {categoryChangedEvent.Id}. Duplicated name", categoryChangedEvent.CorrelationId);
                return;
            }

            var category = new CategoryDto(
                categoryChangedEvent.Id,
                categoryChangedEvent.Name,
                categoryChangedEvent.Description,
                categoryChangedEvent.SortOrder);

            var saveResult = await _categoryIndexer.Index(category);

            if (!saveResult.Success)
            {
                var failedEvent = new CreatingCategoryFailedEvent(category.Id, CategoryModificationStatus.UnexpectedProblem, categoryChangedEvent.CorrelationId);
                await this.SaveAndDispatchEvent(categoryChangedEvent.Id, failedEvent);

                _logger.LogProgress(OperationStatus.Failed, $"Problem occured while saving the category: {categoryChangedEvent.Id}", categoryChangedEvent.CorrelationId);
                return;
            }

            var successEvent = this.GetSuccessEvent(categoryChangedEvent);

            _logger.LogProgress(OperationStatus.Finished, string.Empty, categoryChangedEvent.CorrelationId);
            await this.SaveAndDispatchEvent(categoryChangedEvent.Id, successEvent);
        }