public IDomainEventCollection Credit(decimal amount, CanCreditAccount canCreditAccount)
        {
            Contract.Requires(canCreditAccount != null);
            Contract.Ensures(Contract.Result <IDomainEventCollection>() != null);

            string declinationReason;

            if (canCreditAccount(this, amount, out declinationReason))
            {
                Balance += amount;

                var creditApplied = new CreditApplied(Identity, amount, Balance);

                if (Overdrawn)
                {
                    return(new DomainEventCollection(this, new Collection <IDomainEvent>
                    {
                        creditApplied,
                        new CreditOverdrawn(Identity, Balance)
                    }));
                }

                return(new DomainEventCollection(this, creditApplied));
            }

            return(new DomainEventCollection(this, new CreditDeclined(Identity, amount, Balance, declinationReason)));
        }
Beispiel #2
0
 public void Handle(CreditApplied message)
 {
     balance += message.Amount;
     Console.WriteLine();
     Console.WriteLine($"CreditApplied: ${message.Amount:0.00}");
     Console.WriteLine($"Balance: ${balance:0.00}");
 }
        private IEnumerable <SqlNonQueryCommand> CreditAppliedHandler(CreditApplied @event)
        {
            var result = new List <SqlNonQueryCommand>();

            var lastAccount       = @event.Account.Split(":").Last();
            var lastAccountPrefix = lastAccount.Split("|").First();
            var lastAccountId     = lastAccount.Split("|").Last();

            if (lastAccountPrefix == "R")
            {
                var penUltimateAccount   = @event.Account.Split(":").Reverse().Skip(1).First();
                var penultimateAccountId = penUltimateAccount.Split("|").Last();

                var command = Sql.NonQueryStatement(
                    @"declare @Amount int = (select top 1 Amount FROM [OnHandInventoryView] where skuId = @SkuId and ReservationId = @ReservationId and location = @Location)
                            UPDATE [OnHandInventoryView] SET [Amount] = @Amount - @ReservedAmount WHERE SkuId = @SkuId and ReservationId = @ReservationId and location = @Location",
                    new
                {
                    SkuId          = Sql.UniqueIdentifier(@event.SkuId),
                    ReservedAmount = Sql.Int(@event.Amount),
                    Location       = Sql.VarChar(penultimateAccountId, 50),
                    ReservationId  = Sql.UniqueIdentifier(Guid.Parse(lastAccountId))
                });

                result.Add(command);
            }

            if (lastAccountPrefix == "WL")
            {
                var command = Sql.NonQueryStatement(
                    @"declare @Amount int = (select top 1 Amount FROM [OnHandInventoryView] where skuId = @SkuId and ReservationId IS NULL and location = @Location)
                            UPDATE [OnHandInventoryView] SET [Amount] = @Amount - @AmountToAppend WHERE SkuId = @SkuId and ReservationId IS NULL and Location = @Location",
                    new
                {
                    SkuId          = Sql.UniqueIdentifier(@event.SkuId),
                    AmountToAppend = Sql.Int(@event.Amount),
                    Location       = Sql.VarChar(lastAccountId, 50)
                });

                result.Add(command);
            }

            return(result);
        }
        private static void CreditApplied(MemoryCache cache, CreditApplied message)
        {
            var account = Account.Parse(message.Account);

            if (!account.ContainsComponent <WarehouseLocationComponent>())
            {
                return;
            }

            var accountId = account.GetId();

            var id = StockLinePartId.NewId(message.SkuId, accountId, message.SkuMetadata);

            if (cache.TryGetValue(id, out StockLine stockLine))
            {
                stockLine.Amount   -= message.Amount;
                stockLine.NetWeight = cache.Get <Sku>(message.SkuId).GetNetWeight() * stockLine.Amount;

                if (stockLine.Amount == 0)
                {
                    cache.Remove(id);
                }
            }
        }
Beispiel #5
0
 private void Apply(CreditApplied @event)
 {
     Balance += @event.Amount;
 }
Beispiel #6
0
 public void Handle(CreditApplied message)
 {
 }
 private void When(CreditApplied domainEvent)
 {
     Balance = domainEvent.NewBalance;
 }