// Helper Method or the Sale Post Request: private void ProcessPostGroup(SalePostRecieveGroup group, SalePostRecieve model) { group.sales.Add(model); processingSales.Add(group); if (model.LastInGroup) { group.foundEndOfGroup = true; group.groupLength = model.NumberInGroup + 1; } if (group.foundEndOfGroup) { // Test to see if all of sales group is present, if so add it ro the list. if (group.sales.Count == group.groupLength) { System.Diagnostics.Debug.WriteLine("########## SALES GROUP ADDED: " + group); Console.WriteLine("########## SALES GROUP ADDED: " + group); salesTableLock.WaitOne(); foreach (var item in group.sales) { Sale sale = new Sale(nextSaleID++, item.GroupID, item.ItemID, item.Quantity, item.Date, item.Time); salesTable.Add(sale); if (!InventoryController.itemTableLoadedFromFile) { InventoryController c = new InventoryController(); } InventoryController.itemTableLock.WaitOne(); foreach (var inven in InventoryController.itemTable) { if (inven.id == sale.itemID) { inven.quantity -= sale.quantity; break; } } var file = new StreamWriter(InventoryController.inventoryDatabaseFile); foreach (var entry in InventoryController.itemTable) { file.WriteLine(entry); } file.Close(); InventoryController.itemTableLock.ReleaseMutex(); file = new StreamWriter(salesDatabaseFile); foreach (var entry in salesTable) { file.WriteLine(entry); } file.Close(); } salesTableLock.ReleaseMutex(); processingSales.Remove(group); } } }
public IActionResult Post(SalePostRecieve model) { if (model != null) { if (model.Date != null && model.Time != null) { try { System.Diagnostics.Debug.WriteLine("########## SALES POST: " + model); Console.WriteLine("########## SALES POST: " + model); processingSalesLock.WaitOne(); bool foundGroup = false; foreach (var group in processingSales) { if (group.groupID == model.GroupID) { foundGroup = true; ProcessPostGroup(group, model); break; } } if (!foundGroup) { SalePostRecieveGroup group = new SalePostRecieveGroup(); group.groupID = model.GroupID; ProcessPostGroup(group, model); } processingSalesLock.ReleaseMutex(); // 200 - Success return(Ok()); } catch (Exception) { // 403 - Forbidden. The request was legal but the server is refusing to respond to it. // The Model is incomplete. return(StatusCode(403)); } } } return(StatusCode(400)); }