示例#1
0
 public override async Task CleanupRestoreErrorAsync(Guid appId)
 {
     if (isReserved)
     {
         await appsByNameIndex.RemoveReservationAsync(appId, appName);
     }
 }
        public async Task Should_add_app_to_index_on_create()
        {
            A.CallTo(() => index.ReserveAppAsync(appId, "my-app"))
            .Returns(true);

            var context =
                new CommandContext(new CreateApp {
                AppId = appId, Name = "my-app"
            }, commandBus)
                .Complete();

            await sut.HandleAsync(context);

            A.CallTo(() => index.ReserveAppAsync(appId, "my-app"))
            .MustHaveHappened();

            A.CallTo(() => index.AddAppAsync(appId, "my-app"))
            .MustHaveHappened();

            A.CallTo(() => index.RemoveReservationAsync(appId, "my-app"))
            .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);
                }
            }
        }