public async Task Should_remove_app_from_index_on_archive()
        {
            var context =
                new CommandContext(new ArchiveApp {
                AppId = appId
            }, commandBus)
                .Complete();

            await sut.HandleAsync(context);

            A.CallTo(() => index.RemoveAppAsync(appId))
            .MustHaveHappened();
        }
        public async Task HandleAsync(CommandContext context, Func <Task> next)
        {
            var createApp = context.Command as CreateApp;

            var isReserved = false;

            try
            {
                if (createApp != null)
                {
                    isReserved = await index.ReserveAppAsync(createApp.AppId, createApp.Name);

                    if (!isReserved)
                    {
                        var error = new ValidationError("An app with the same name already exists.", nameof(createApp.Name));

                        throw new ValidationException("Cannot create app.", error);
                    }
                }

                await next();

                if (context.IsCompleted)
                {
                    if (createApp != null)
                    {
                        await index.AddAppAsync(createApp.AppId, createApp.Name);
                    }
                    else if (context.Command is ArchiveApp archiveApp)
                    {
                        await index.RemoveAppAsync(archiveApp.AppId);
                    }
                }
            }
            finally
            {
                if (isReserved)
                {
                    await index.RemoveReservationAsync(createApp.AppId, createApp.Name);
                }
            }
        }
        public async Task HandleAsync(CommandContext context, Func <Task> next)
        {
            if (context.IsCompleted)
            {
                switch (context.Command)
                {
                case CreateApp createApp:
                    await index.AddAppAsync(createApp.AppId, createApp.Name);

                    break;

                case ArchiveApp archiveApp:
                    await index.RemoveAppAsync(archiveApp.AppId);

                    break;
                }
            }

            await next();
        }