public async Task <IActionResult> ExtractInventoryItem(long id) { var inventoryItem = await _context.InventoryItems.FindAsync(id); var notification = new Notification { Message = $"Item({inventoryItem.Id}) {inventoryItem.Name} extracted", Type = "extracted" }; inventoryItem.Units -= 1; _context.Entry(inventoryItem).State = EntityState.Modified; try { await _context.SaveChangesAsync(); _messageRepository.Broadcast(notification); } catch (DbUpdateConcurrencyException) { if (!InventoryItemExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { using (var scope = _scopeFactory.CreateScope()) { var context = scope.ServiceProvider.GetService <InventoryContext>(); var inventoryItems = from itm in context.InventoryItems where itm.Expired == false && itm.ExpiryDate <= DateTime.Now select itm; foreach (var item in inventoryItems) { var notification = new Notification { Message = $"Item({item.Id}) {item.Name} expired on {item.ExpiryDate.ToShortDateString()}", Type = "expired" }; _messageRepository.Broadcast(notification); item.Expired = true; context.Entry(item).State = EntityState.Modified; } try { await context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } } await Task.Delay(30 * 1000, stoppingToken); } }