public void Post(OrderDetail entity) { Stock checkStock = db.Stocks.Where(s => s.StockID == entity.StockID).FirstOrDefault(); float stockToSell = checkStock.AvailableQuantity; try { if (stockToSell >= entity.Quantity) { Random random = new Random(); int unique1 = random.Next(); int uniqueID = unique1; CustomShippingAddress customShipping = new CustomShippingAddress(); customShipping.UniqueOrderID = uniqueID; customShipping.CellPhone = "Not Specified"; customShipping.ShippingAddress = "Not Specified"; db.CustomShippingAddresses.Add(customShipping); db.SaveChanges(); Stock stock = db.Stocks.Where(s => s.StockID == entity.StockID).FirstOrDefault(); entity.PricePerUnit = stock.SellingPricePertUnit; entity.TotalBill = entity.PricePerUnit * (decimal)entity.Quantity; entity.UniqueID = uniqueID; entity.Status = "Pending"; entity.CustomerID = "Not Specified"; db.OrderDetails.Add(entity); db.SaveChanges(); // We should send uniqueID to the customer table where customer id = current login ID; Stock mainStock = db.Stocks.Where(s => s.StockID == entity.StockID).FirstOrDefault(); float availableStock = mainStock.AvailableQuantity; if (availableStock >= entity.Quantity) { mainStock.AvailableQuantity = availableStock - entity.Quantity; db.SaveChanges(); } else { } } } catch { } }
public void Put(int id, Customer entity) { string action = "Login"; // here action will be the Logged ID; if (action == "Login") { Customer customer = db.Customers.Where(s => s.UniqueOrderID == id).FirstOrDefault(); customer.CustomerCellPhone = entity.CustomerCellPhone; customer.CustomerAddress = entity.CustomerAddress; db.SaveChanges(); OrderDetail order = new OrderDetail(); order.CustomerID = action; db.SaveChanges(); } else if (action == "Custom") { CustomShippingAddress custom = db.CustomShippingAddresses.Where(s => s.UniqueOrderID == id).FirstOrDefault(); custom.CellPhone = entity.CustomerCellPhone; custom.ShippingAddress = entity.CustomerAddress; db.SaveChanges(); OrderDetail order = db.OrderDetails.Where(s => s.UniqueID == id).FirstOrDefault(); order.CustomerID = "Custom Address Used"; db.SaveChanges(); } //var customer = db.Customers.Find(id); //if (customer != null) //{ // //customer.CustomerAddress = entity.CustomerAddress; // //customer.CustomerName = entity.CustomerName; // //customer.CustomerCellPhone = entity.CustomerCellPhone; // //customer.CustomerEmail = entity.CustomerEmail; // //customer.CustomerAddress = entity.CustomerAddress; // //customer.DateOfRegistration = entity.DateOfRegistration; // db.SaveChanges(); //} }