public async Task Handle(TransactionCreatedEvent e) { FilterDefinition <CardListQueryModel> filter = Builders <CardListQueryModel> .Filter.Eq("Id", e.Data.CardId); var cardList = await readDbContext.CardListMaterializedView .Find(filter) .FirstOrDefaultAsync(); var transactionList = new TransactionListQueryModel() { Id = e.Data.Id, Amount = e.Data.Charge.Amount, ChargeDate = e.Data.ChargeDate, CardHolder = cardList.CardHolder, CardNumber = cardList.Number, CurrencyCode = e.Data.Charge.CurrencyCode, UniqueId = e.Data.UniqueId }; if (!cardList.HighestTransactionAmount.HasValue || e.Data.Charge.Amount > cardList.HighestTransactionAmount) { cardList.HighestChargeDate = e.Data.ChargeDate; cardList.HighestTransactionId = e.Data.Id; cardList.HighestTransactionAmount = e.Data.Charge.Amount; } cardList.TransactionCount += 1; await cache.Delete(nameof(CardListQueryModel)); await readDbContext.TransactionListMaterializedView.InsertOneAsync(transactionList); await readDbContext.CardListMaterializedView.ReplaceOneAsync(filter, cardList, new UpdateOptions { IsUpsert = true }); }
private void AssertProperties(TransactionListQueryModel queryModel, Transaction transaction, CardListQueryModel cardList) { Assert.Equal(queryModel.Id, transaction.Id); Assert.Equal(queryModel.Amount, transaction.Charge.Amount); Assert.Equal(queryModel.CurrencyCode, transaction.Charge.CurrencyCode); Assert.Equal(queryModel.ChargeDate, transaction.ChargeDate); Assert.Equal(queryModel.UniqueId, transaction.UniqueId); Assert.Equal(queryModel.CardHolder, cardList.CardHolder); Assert.Equal(queryModel.CardNumber, cardList.Number); }