public async Task Handle(OrderUpdateCommand notification, CancellationToken cancellationToken) { _logger.LogInformation("--- Order Status update started"); //var entry = new Domain.Order(); var stocks = _context.Orders.FirstOrDefault(item => item.OrderId == notification.OrderId); if (notification.Status == Common.Enums.OrderStatus.Approved) { if (stocks != null) { // Approved information stocks.Status = Common.Enums.OrderStatus.Approved; stocks.CreatedAt = DateTime.UtcNow; _context.Orders.Update(stocks); await _context.SaveChangesAsync(); SendNotification(stocks, notification); } } else { // Cancel information stocks.Status = Common.Enums.OrderStatus.Canceled; stocks.CreatedAt = DateTime.UtcNow; _context.Orders.Update(stocks); await _context.SaveChangesAsync(); using (var trx = await _context.Database.BeginTransactionAsync()) { try { await _catalogProxy.UpdateStockAsync(new Proxies.Catalog.Commands.ProductInStockUpdateStockCommand { Items = notification.Items.Select(x => new Proxies.Catalog.Commands.ProductInStockUpdateItem { Action = Proxies.Catalog.Commands.ProductInStockAction.Add, ProductId = x.ProductId, Stock = x.Quantity }) }); } catch { throw new Exception(); } // Update Stock command execute await trx.CommitAsync(); SendNotification(stocks, notification); } } }
public async Task Handle(OrderCreateCommand command, CancellationToken cancellationToken) { _logger.LogInformation("--- New order creation started"); var entry = new Domain.Order(); using (var trx = await _context.Database.BeginTransactionAsync()) { //01. prepare detail _logger.LogInformation("--- Preparing detail"); PrepareDetail(entry, command); //02. prepare header _logger.LogInformation("--- Preparing header"); PrepareHeader(entry, command); //03. create order _logger.LogInformation("--- Creating order"); await _context.AddAsync(entry); await _context.SaveChangesAsync(); _logger.LogInformation($"--- Order {entry.OrderId} was created"); //04. Update stocks try { await _catalogProxy.UpdateStockAsync(new ProductInStockUpdateStockCommand { Items = command.Items.Select(x => new ProductInStockUpdateItem { Action = ProductInStockAction.Substract, ProductId = x.ProductId, Stock = x.Quantity }) }); _logger.LogInformation("--- Updating stock"); } catch (Exception ex) { _logger.LogError("--- Ups error when updating stock... Detail: " + ex.Message); throw new Exception(""); } _logger.LogInformation("--- Updating stock"); //Logica para actualizar el stock await trx.CommitAsync(); } }
public async Task Handle(OrderCreateCommand notification, CancellationToken cancellationToken) { _logger.LogInformation("--- New order creation started"); var entry = new Domain.Order(); using (var trx = await _context.Database.BeginTransactionAsync()) { // 01. Prepare detail _logger.LogInformation("--- Preparing detail"); PrepareDetail(entry, notification); // 02. Prepare header _logger.LogInformation("--- Preparing header"); PrepareHeader(entry, notification); // 03. Create order _logger.LogInformation("--- Creating order"); await _context.AddAsync(entry); await _context.SaveChangesAsync(); SendNotification(entry, notification); _logger.LogInformation($"--- Order {entry.OrderId} was created"); // 04. Update Stocks _logger.LogInformation("--- Updating stock"); try { await _catalogProxy.UpdateStockAsync(new Proxies.Catalog.Commands.ProductInStockUpdateStockCommand { Items = notification.Items.Select(x => new Proxies.Catalog.Commands.ProductInStockUpdateItem { Action = Proxies.Catalog.Commands.ProductInStockAction.Substract, ProductId = x.ProductId, Stock = x.Quantity }) }); } catch { _logger.LogError("Order couldn't be created because some of the products don't have enough stock"); throw new Exception(); } // Lógica para actualizar el Stock await trx.CommitAsync(); } _logger.LogInformation("--- New order creation ended"); }
public async Task Handle(OrderCreateCommand notification, CancellationToken cancellationToken) { _logger.LogInformation("--- New order creation started"); var entry = new Domain.Order(); // Si falla, hara un rollback a toda la transaccion y no va a generar la orden // El proceso se conoce como orquestación (Darle toda la responsabilidad a un microservicio) using (var trx = await _context.Database.BeginTransactionAsync()) { // 01. Prepare detail _logger.LogInformation("--- Preparing detail"); PrepareDetail(entry, notification); // 02. Prepare header _logger.LogInformation("--- Preparing header"); PrepareHeader(entry, notification); // 03. Create order _logger.LogInformation("--- Creating order"); await _context.AddAsync(entry); await _context.SaveChangesAsync(); _logger.LogInformation($"--- Order {entry.OrderId} was created"); // 04. Update Stocks try { await _catalogProxy.UpdateStockAsync(new ProductInStockUpdateStockCommand { Items = notification.Items.Select(x => new ProductInStockUpdateItem { Action = ProductInStockAction.Substract, ProductId = x.ProductId, Stock = x.Quantity }) }); _logger.LogInformation("--- Updating stock"); } catch (Exception e) { _logger.LogError("No se puedo crear la orden debido a la falta de stock"); } // Logica para actualizar el stock await trx.CommitAsync(); } }
public async Task Handle(OrderCreateCommand notification, CancellationToken cancellationToken) { _logger.LogInformation("--- New order creation started"); var entry = new Domain.Order(); using (var trx = await _context.Database.BeginTransactionAsync()) { // 02. Prepare header _logger.LogInformation("--- Preparing header"); PrepareHeader(entry, notification); // 03. Create order _logger.LogInformation("--- Creating order"); await _context.AddAsync(entry); await _context.SaveChangesAsync(); // Prepare detail _logger.LogInformation("--- Preparing detail"); PrepareDetail(entry, notification); // Create OrderDetails foreach (var orderDetail in entry.Items) { await _context.AddAsync(orderDetail); } await _context.SaveChangesAsync(); _logger.LogInformation($"--- Order {entry.OrderId} was created"); // 04. Update Stocks _logger.LogInformation("--- Updating stock"); await _catalogProxy.UpdateStockAsync(new ProductInStockUpdateStockCommand { Items = notification.Items.Select(x => new ProductInStockUpdateItem { ProductId = x.ProductId, Stock = x.Quantity, Action = ProductInStockAction.Substract }) }); await trx.CommitAsync(); } _logger.LogInformation("--- New order creation ended"); }