Пример #1
0
        private async Task <bool> SaveGuard(UpdateCategoryProcessStartedEvent categoryChangedEvent)
        {
            var existingCategoryResult = await _categorySearchHandler.GetById(new GetCategoryByIdQuery(categoryChangedEvent.Id));

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

                _logger.LogProgress(OperationStatus.Failed, $"Category({categoryChangedEvent.Id}) cannot be found", categoryChangedEvent.CorrelationId);
                return(false);
            }

            var existingCategoryWithSameNameResult = await _categorySearchHandler.GetByName(new GetCategoryByNameQuery(categoryChangedEvent.Name));

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

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

            return(true);
        }
        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);
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, HttpMethods.Get, Route = "category/name/{name}")] HttpRequest req,
            string name)
        {
            var categoryResult = await _categorySearchHandler.GetByName(new GetCategoryByNameQuery(name));

            if (!categoryResult.Success)
            {
                return(new NotFoundResult());
            }

            return(new OkObjectResult(categoryResult.Value));
        }
        public async Task Handle(CategoryUsedEvent categoryUsedEvent)
        {
            _logger.LogInformation($"{nameof(CategoryUsedEvent)} with name: {categoryUsedEvent.Name}, has been used in {categoryUsedEvent.UsedIn}");

            var categoryResult = await _categorySearchHandler.GetByName(new GetCategoryByNameQuery(categoryUsedEvent.Name));

            if (!categoryResult.Success)
            {
                _logger.LogInformation($"{nameof(CategoryUsedEvent)} with name: {categoryUsedEvent.Name} cannot be find");
                return;
            }

            await _usedCategoryRepository.CreateOrUpdate(categoryResult.Value.Id, categoryUsedEvent.UsedIn);

            _logger.LogProgress(OperationStatus.Finished, string.Empty, categoryUsedEvent.CorrelationId);
        }