public async Task Handle(ClienteCrearComando comando, CancellationToken cancellation) { await _context.AddAsync(new Cliente { Nombre = comando.Nombre }); await _context.SaveChangesAsync(); }
public async Task Handle(ProductoCrearComando comando, CancellationToken cancellation) { await _context.AddAsync(new Producto { Nombre = comando.Nombre, Descripcion = comando.Descripcion, Precio = comando.Precio }); await _context.SaveChangesAsync(); }
public async Task Handle(ProductoEnStockActualizarComnando comando, CancellationToken cancellation) { _logger.LogInformation("--- ProductoEnStockActualizarComnando inicia"); var productos = comando.Items.Select(x => x.ProductoId); var stocks = await _context.Stocks.Where(x => productos.Contains(x.ProductoId)).ToListAsync(); _logger.LogInformation("--- Obtuvo los productos de la base de datos"); foreach (var item in comando.Items) { var entry = stocks.SingleOrDefault(x => x.ProductoId == item.ProductoId); if (item.Accion == ProductoEnStockAccion.Substract) { if (entry == null || item.Stock > entry.Stock) { _logger.LogError($"--- Producto {entry.ProductoId} no tiene suficiente stock"); throw new ProductoEnStockActualizarComnandoExcepcion($"Producto {entry.ProductoId} no tiene suficiente stock"); } entry.Stock -= item.Stock; _logger.LogInformation($"--- El producto {entry.ProductoId} fue sustraido. Ahora tiene {entry.Stock}"); } else //Add { if (entry == null) { entry = new ProductoEnStock { ProductoId = item.ProductoId }; await _context.AddAsync(entry); _logger.LogInformation($"--- Nuevo stock fue creado del producto {entry.ProductoId}"); } entry.Stock += item.Stock; _logger.LogInformation($"--- Al producto {entry.ProductoId} se le añadió el stock {entry.Stock}"); } } await _context.SaveChangesAsync(); _logger.LogInformation("--- ProductoEnStockActualizarComnando finaliza"); }