public CustomerModel AddCustomer(CustomerModel appCustomer) { IQueryable <Customer> dbCustomer = _context.Customers .OrderBy(x => x.CustomerId); DateTime dateTime = DateTime.Now; var newCustomer = new Customer { FirstName = appCustomer.FirstName, LastName = appCustomer.LastName, StreetAddress1 = appCustomer.StreetAddress1, StreetAddress2 = appCustomer.StreetAddress2, City = appCustomer.City, State = appCustomer.State, Zipcode = appCustomer.Zipcode, CustomerCreated = dateTime, DefaultStoreId = appCustomer.DefaultStore }; _context.Add(newCustomer); _context.SaveChanges(); appCustomer.CustomerID = newCustomer.CustomerId; return(appCustomer); }
public void AddOrder(OrderModel appOrder) { Order dbOrder = _context.Orders.OrderBy(x => x.OrderId).Last(); DateTime dateTime = DateTime.Now; Order newOrder = new Order { CustomerId = appOrder.CustomerNumber, StoreId = appOrder.StoreID, NumberOfProducts = appOrder.NumberOfProducts, OrderTotal = appOrder.Total, DatePlaced = dateTime }; _context.Add(newOrder); _context.SaveChanges(); Order dbOrderV2 = _context.Orders.OrderBy(x => x.OrderId).Last(); OrderDetail dbOrderDetails = _context.OrderDetails.OrderBy(x => x.Id).Last(); List <OrderDetail> orderDetailList = new List <OrderDetail>(); foreach (var product in appOrder.Product) { OrderDetail newDetail = new OrderDetail { OrderId = dbOrderV2.OrderId, ProductId = product.ProductId, Quantity = product.Quantity, TotalCost = product.CostPerItem * product.Quantity }; orderDetailList.Add(newDetail); } foreach (var lineItem in orderDetailList) { _context.Add(lineItem); _context.SaveChanges(); } for (int prod = 0; prod < appOrder.Product.Count; prod++) { Inventory dbInventory = _context.Inventories .Include(x => x.Product) .Where(x => appOrder.StoreID == x.StoreId) .Where(x => x.Product.ProductId == appOrder.Product[prod].ProductId) .First(); dbInventory.ProductId = appOrder.Product[prod].ProductId; dbInventory.Quantity -= appOrder.Product[prod].Quantity; _context.Update(dbInventory); _context.SaveChanges(); } }