public List <Order> GetOrdersByCustomer(Customer customer) { using var context = new project0Context(_contextOptions); var dbOrders = context.Orders .Where(o => o.CustomerId == customer.CustomerId) .ToList(); var result = new List <Order>(); foreach (var order in dbOrders) { var newLocation = _storeRepo.GetStoreById(order.StoreId); var newCust = _customerRepo.GetCustomerById(order.CustomerId); var newOrder = new Order() { OrderId = order.OrderId, Store = newLocation, Customer = newCust, OrderTotalPrice = order.OrderTotal }; newOrder.OrderId = order.OrderId; newOrder.Date = order.Date; var newOrderItems = GetOrderItemsByOrder(newOrder); foreach (var orderItem in newOrderItems) { newOrder.OrderItems.Add(orderItem); } result.Add(newOrder); } return(result); }
public void DeleteCustomer(Customer customer) { using var context = new project0Context(_context); var dbCustomer = context.Customers .Where(i => i.CustomerId == customer.CustomerId) .FirstOrDefault(); context.Remove(dbCustomer); context.SaveChanges(); }
public void CreateCustomer(Customer customer) { using var context = new project0Context(_context); var newEntry = new Models.Customer() { FirstName = customer.FirstName, LastName = customer.LastName, }; context.Customers.Add(newEntry); context.SaveChanges(); }
public void UpdateCustomer(Customer customer) { using var context = new project0Context(_context); var dbCust = context.Customers .Where(a => a.CustomerId == customer.CustomerId) .FirstOrDefault(); dbCust.FirstName = customer.FirstName; dbCust.LastName = customer.LastName; try { context.SaveChanges(); } catch (Exception) { Console.WriteLine("Error"); } }
public List <Customer> GetAllCustomers() { using var context = new project0Context(_context); var dbCust = context.Customers.Distinct().ToList(); var result = new List <Customer>(); foreach (var customer in dbCust) { var newCustomer = new Customer() { CustomerId = customer.CustomerId, FirstName = customer.FirstName, LastName = customer.LastName }; result.Add(newCustomer); } ; return(result); }
public Customer GetCustomerById(int?id) { using var context = new project0Context(_context); var dbCust = context.Customers .Where(c => c.CustomerId == id) .FirstOrDefault(); if (dbCust == null) { return(null); } var newCust = new Customer() { CustomerId = dbCust.CustomerId, LastName = dbCust.LastName, FirstName = dbCust.FirstName }; return(newCust); }