public IActionResult Update(int id, InventoryItemPostRecieve model) { if (model != null) { try { System.Diagnostics.Debug.WriteLine("########## PUT ID Inventory " + id.ToString()); Console.WriteLine("########## PUT ID Inventory " + id.ToString()); InventoryItem newItem = new InventoryItem(id, model.Name, model.Brand, model.Barcode, model.PurchasePrice, model.RetailPrice, model.Quantity); // Check to see if the item exists bool foundItem = false; itemTableLock.WaitOne(); for (int i = 0; i < itemTable.Count; i++) { // If item is found, replace with updated data if (itemTable[i].id == id) { foundItem = true; itemTable[i] = newItem; break; } } // Save File var file = new StreamWriter(inventoryDatabaseFile); foreach (var entry in itemTable) { file.WriteLine(entry); } file.Close(); itemTableLock.ReleaseMutex(); // If the item does not exist return a not found. if (!foundItem) { return(StatusCode(400)); } return(Ok()); } catch (Exception) { // Internal Error return(StatusCode(500)); } } else { // 400 - Failure return(StatusCode(400)); } }
public IActionResult Post(InventoryItemPostRecieve model) { if (model != null) { if (model.Name != null && model.Brand != null && model.Barcode != null && model.RetailPrice != null && model.PurchasePrice != null) { try { itemTableLock.WaitOne(); InventoryItem item = new InventoryItem(nextItemID++, model.Name, model.Brand, model.Barcode, model.PurchasePrice, model.RetailPrice, model.Quantity); itemTable.Add(item); itemTableLock.ReleaseMutex(); System.Diagnostics.Debug.WriteLine("########## INVENTORY POST: " + model); Console.WriteLine("########## INVENTORY POST: " + model); // NOTE(Xavier): It is probably not the best idea to do this // here because it will be called for each request. itemTableLock.WaitOne(); var file = new StreamWriter(inventoryDatabaseFile); foreach (var entry in itemTable) { file.WriteLine(entry); } file.Close(); itemTableLock.ReleaseMutex(); // 200 - Success return(StatusCode(200)); } catch (Exception) { // 403 - Forbidden. The request was legal but the server is refusing to respond to it. // The Model is incomplete. return(StatusCode(403)); } } } // 400 - Failure return(StatusCode(400)); }