public void UpdateInventory() { foreach (var cmd in context.GetPartCommands()) { var item = context.Inventory.Single(i => i.PartTypeId == cmd.PartTypeId); var oldCount = item.Count; if (cmd.Command == PartCountOperation.Add) { item.Count += cmd.PartCount; } else { item.Count -= cmd.PartCount; } var transaction = context.BeginTransaction(); try { context.UpdateInventoryItem(item.PartTypeId, item.Count, transaction); context.DeletePartCommand(cmd.Id, transaction); transaction.Commit(); } catch { transaction.Rollback(); item.Count = oldCount; throw; } } }
public async Task UpdateInventory() { foreach (var cmd in await _context.GetPartCommands()) { var item = _context.Inventory.Single(i => i.PartTypeId == cmd.PartTypeId); var oldCount = item.Count; if (cmd.Command == PartCountOperation.Add) { item.Count += cmd.PartCount; } else { item.Count -= cmd.PartCount; } using (var transaction = _context.BeginTransaction()) { try { await _context.UpdateInventoryItem(item.PartTypeId, item.Count, transaction); await _context.DeletePartCommand(cmd.Id, transaction); transaction.Commit(); } catch { transaction.Rollback(); item.Count = oldCount; throw; } } } var orders = await _context.GetOrders(); foreach (var item in _context.Inventory) { if (item.Count < item.OrderThreshold && orders.FirstOrDefault(o => o.PartTypeId == item.PartTypeId && !o.FulfilledDate.HasValue) == null) { await OrderPart(item.PartType, item.OrderThreshold); } } }