Ejemplo n.º 1
0
        public async Task <bool> HandleAsync(DeleteMenu command)
        {
            var menu = await repository.GetByIdAsync(command.MenuId);

            if (menu == null)
            {
                MenuDoesNotExistException.Raise(command, command.MenuId);
            }

            // TODO: Check if the user owns the resource before any operation
            // if(command.User.TenantId != menu.TenantId)
            // {
            //     throw NotAuthorizedException()
            // }

            var successful = await repository.DeleteAsync(command.MenuId);

            if (!successful)
            {
                OperationFailedException.Raise(command, command.MenuId, "Unable to delete menu");
            }

            await applicationEventPublisher.PublishAsync(
                new MenuDeleted(command, command.MenuId)
                );

            return(successful);
        }
        public async Task <TResult> HandleAsync(TCommand command)
        {
            TResult result = default(TResult);

            var menu = await repository.GetByIdAsync(command.MenuId);

            if (menu == null)
            {
                MenuDoesNotExistException.Raise(command, command.MenuId);
            }

            // TODO: Check if the user owns the resource before any operation
            // if(command.User.TenantId != menu.TenantId)
            // {
            //     throw NotAuthorizedException()
            // }

            try
            {
                result = await HandleCommandAsync(menu, command);

                var issuccessful = await repository.SaveAsync(menu);

                if (!issuccessful)
                {
                    OperationFailedException.Raise(command, command.MenuId,
                                                   $"Unable to handle command {typeof(ICommand).Name}");
                }

                foreach (var appEvent in RaiseApplicationEvents(menu, command))
                {
                    await applicationEventPublisher.PublishAsync(appEvent);
                }
            }
            catch (DomainExceptionBase ex)
            {
                DomainRuleViolationException.Raise(command, command.MenuId, ex);
            }
            catch (ApplicationExceptionBase)
            {
                // TODO: handle applicaiton exception handling
                // possible failures is missing data or information, validations, and so on
                throw;
            }
            catch (InfrastructureExceptionBase)
            {
                //TODO: handle  infrastructure exception handling
                //possible failures is calling database, queue or any other dependency
                throw;
            }
            catch (Exception ex)
            {
                //TODO: Enrich the exception details with context information to track in the logs
                ex.Data["OperationCode"] = command.OperationCode;
                ex.Data["CorrelationId"] = command.CorrelationId;
                ex.Data["MenuId"]        = command.MenuId;

                throw;
            }

            return(result);
        }