Exemple #1
0
        public void Handle(ProductPriceChangedEvent domainEvent)
        {
            _logger.LogInformation($"ProductPriceChangeEvent handled.");

            //_context.HistoricalPrices.Add(new HistoricalPrice
            //{
            //    ProductId = domainEvent.ProductId,
            //    Price = domainEvent.NewPrice,
            //    StartDate = domainEvent.DateOccurred
            //});
        }
Exemple #2
0
        public async Task Handle(ProductChangePriceCommand command, CancellationToken cancellationToken)
        {
            var aggregate = await _repository.GetHydratedAggregate <Product>(command.ProductId, cancellationToken);

            var priceChangedEvent = new ProductPriceChangedEvent(command.ProductId, command.Price);

            aggregate.Handle(priceChangedEvent);

            await _repository.SaveAggregateEvent(aggregate, priceChangedEvent, cancellationToken);

            await _mediator.Publish(priceChangedEvent, cancellationToken);
        }
        public void Handle(ProductPriceChangedEvent e)
        {
            Task.Run(async() =>
            {
                var product = await _context.Products.FindAsync(e.ProductId);

                if (product != null)
                {
                    product.Price = e.Price;

                    await _context.SaveChangesAsync();
                }
            });
        }
        public void HandlePriceChangedEventOnAgg()
        {
            var id = Guid.NewGuid();

            var productCreatedEvent = new ProductCreatedEvent(id, "mock name", 500);

            var agg = new Kanayri.Domain.Product.Product();

            agg.Handle(productCreatedEvent);

            const int newPrice          = 600;
            var       priceChangedEvent = new ProductPriceChangedEvent(id, newPrice);

            agg.Handle(priceChangedEvent);

            Assert.Equal(newPrice, agg.Price);
        }
Exemple #5
0
        public async Task <ActionResult> UpdateProductAsync([FromBody] CatalogItem productToUpdate)
        {
            var catalogItem = await _catalogContext.CatalogItems.SingleOrDefaultAsync(i => i.Id == productToUpdate.Id);

            if (catalogItem == null)
            {
                return(NotFound(new { Message = $"Item with id {productToUpdate.Id} not found." }));
            }

            var oldPrice = catalogItem.Price;
            var raiseProductPriceChangedEvent = oldPrice != productToUpdate.Price;

            // Update current product
            catalogItem = productToUpdate;
            _catalogContext.CatalogItems.Update(catalogItem);

            if (raiseProductPriceChangedEvent)             // Save product's data and publish integration event through the Event Bus if price has changed
            {
                //Create Integration Event to be published through the Event Bus
                var priceChangedEvent = new ProductPriceChangedEvent(catalogItem.Id, productToUpdate.Price, oldPrice);

                //// Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction
                //await _catalogEventService.SaveEventAndCatalogContextChangesAsync(priceChangedEvent);

                ////// Publish through the Event Bus and mark the saved event as published
                ////await _catalogEventService.PublishThroughEventBusAsync(priceChangedEvent);

                // Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction
                using (_catalogContext.Database.GetDbConnection().BeginTransaction(_eventBus, autoCommit: true))
                {
                    _eventBus.Publish(nameof(ProductPriceChangedEvent), priceChangedEvent);
                };
            }
            else             // Just save the updated product because the Product's Price hasn't changed.
            {
                await _catalogContext.SaveChangesAsync();
            }

            return(CreatedAtAction(nameof(ItemByIdAsync), new { id = productToUpdate.Id }, null));
        }
Exemple #6
0
        public async Task HandleEvent(ProductPriceChangedEvent @event)
        {
            //Check the last price
            var lastPrice = await _context.PriceErrorAlertLastPrices.FindAsync(@event.AggregateId);

            //Compare
            //We should be checking the currency code and exchanging to a default currency before comparing prices
            if (@event.Price > lastPrice.Price)
            {
                lastPrice.Price            = @event.Price;
                lastPrice.CurrencyCode     = @event.CurrencyCode;
                lastPrice.DateLastModified = @event.EventDate;
                await _context.SaveChangesAsync();

                await _context.DisposeAsync();

                return;
            }

            //Notify
            var discount = 1 - (@event.Price / lastPrice.Price);

            if (discount > 0.4)
            {
                _logger.LogInformation($"[{@event.AggregateId}] Potential price error detected: [{(discount * 100.0).ToString("F2")}%] {lastPrice.Price.ToString("C")} {lastPrice.CurrencyCode} -> {@event.Price.ToString("C")} {@event.CurrencyCode}");
            }


            lastPrice.Price            = @event.Price;
            lastPrice.CurrencyCode     = @event.CurrencyCode;
            lastPrice.DateLastModified = @event.EventDate;
            await _context.SaveChangesAsync();

            await _context.DisposeAsync();


            var projection = await _partitionedProjectionQuery.Execute(@event.AggregateId);

            _logger.LogInformation($"[{@event.AggregateId}] Potential price error detected product data: {JsonConvert.SerializeObject(projection)}");
        }
 public Task <AnotherProjection> ProjectEvent(AnotherProjection prevState, ProductPriceChangedEvent @event)
 {
     prevState.Price        = @event.Price;
     prevState.CurrencyCode = @event.CurrencyCode;
     return(Task.FromResult(prevState));
 }
Exemple #8
0
 private void Apply(ProductPriceChangedEvent @event)
 {
     _price        = @event.Price;
     _currencyCode = @event.CurrencyCode;
 }
Exemple #9
0
        public void Post([FromBody] string value)
        {
            var priceChangedEvent = new ProductPriceChangedEvent("AA-BC", 15, 18);

            _eventBus.Publish(priceChangedEvent);
        }
Exemple #10
0
 public void Handle(ProductPriceChangedEvent e)
 {
     Price = e.Price;
 }