Beispiel #1
0
        public Task Handle(Mint command, IMessageContext context)
        {
            if (Data.IdempotencyToken == command.IdempotencyToken)
            {
                return(CompletedTask);
            }

            var @event    = new MintRequested(command.AggregateId, command.CatalogId, command.IdempotencyToken);
            var commands  = new List <StartMintJob>();
            var jobId     = 1;
            var start     = 1L;
            var remaining = command.Quantity;

            for ( ; start <= command.Quantity; start += MintBatchSize, remaining -= MintBatchSize, jobId++)
            {
                var count        = Min(remaining, MintBatchSize);
                var mintJob      = new MintJob(jobId, command.CatalogId, start, count);
                var startMintJob = new StartMintJob(@event.AggregateId,
                                                    jobId - 1,
                                                    mintJob.Id,
                                                    mintJob.CatalogId,
                                                    mintJob.StartOfSequence,
                                                    mintJob.Count);

                @event.MintJobs.Add(mintJob);
                commands.Add(startMintJob);
            }

            Record(@event);
            return(context.Send(commands));
        }
Beispiel #2
0
        public async Task Handle(StartMintJob command, IMessageContext context)
        {
            var jobId = command.JobId;

            if (Data.State != Started || !Data.MintJobs.Contains(jobId))
            {
                return;
            }

            var tokenVault    = (ITokenVault)context.GetService(typeof(ITokenVault));
            var tokenSecurity = (ITokenSecurity)context.GetService(typeof(ITokenSecurity));
            var die           = new StandardTokenDie(command.AggregateId, command.StartOfSequence, tokenSecurity);
            var tokens        = RunJob(jobId, die, context.CancellationToken);

            await tokenVault.Deposit(tokens, context.CancellationToken);

            Record(new MintJobFinished(Id, jobId));

            if (Data.MintJobs.Count > 0)
            {
                return;
            }

            if (Data.PartiallyCanceled)
            {
                Record(new MintCanceled(Id));
            }
            else
            {
                await tokenVault.ReleaseToCirculation(Id, command.CorrelationId, context.CancellationToken);

                Record(new Minted(Id));
            }

            MarkAsComplete();
        }