public Entities.Order Map(Domain.Models.Order model, Entities.PizzaBoxDbContext context, bool update = false) { Entities.Order order = context.Orders.Include(o => o.Customer).Include(o => o.Store).Include(o => o.Pizzas) .ThenInclude(p => p.PizzaToppings).ThenInclude(pt => pt.Topping).Include(o => o.Pizzas).ThenInclude(p => p.Size) .Include(o => o.Pizzas).ThenInclude(p => p.Crust).FirstOrDefault(o => o.ID == model.ID) ?? new Entities.Order(); //Entities.Order order = context.Orders.FirstOrDefault(o => o.ID == model.ID) ?? new Entities.Order(); if (order.ID != 0 && !update) { return(order); } order.Customer = customerMapper.Map(model.Customer, context, update); order.Pizzas.Clear(); foreach (Domain.Abstracts.APizza pizza in model.Pizzas) { var mappedPizza = pizzaMapper.Map(pizza, context, update); mappedPizza.Order = order; order.Pizzas.Add(mappedPizza); } //model.Pizzas.ForEach(p => pizzas.Add(pizzaMapper.Map(p, context))); order.Store = storeMapper.Map(model.Store, context, update); order.TotalPrice = model.Price; order.TimePlaced = DateTime.Now; if (order.ID == 0) { context.Orders.Add(order); } return(order); }
public Pizza Map(APizza model, Entities.PizzaBoxDbContext context, bool update = false) { Pizza pizza = context.Pizzas.Include(p => p.Order).Include(p => p.Crust).Include(p => p.Size).Include(p => p.PizzaToppings) .ThenInclude(pt => pt.Topping).Include(p => p.PizzaToppings).ThenInclude(pt => pt.Pizza).FirstOrDefault(p => p.ID == model.ID) ?? new Pizza(); //Pizza pizza = context.Pizzas.FirstOrDefault(p => p.ID == model.ID) ?? new Pizza(); if (pizza.ID != 0 && !update) { return(pizza); } pizza.PizzaType = model.PizzaType; pizza.Crust = crustMapper.Map(model.Crust, context, update); pizza.Size = sizeMapper.Map(model.Size, context, update); pizza.Name = model.Name; // need to group the PizzaToppings by their topping's toppingtype // need to make AMOUNT = Count of the grouping List <Entities.Topping> mappedToppings = new List <Entities.Topping>(); model.Toppings.ForEach(t => mappedToppings.Add(toppingMapper.Map(t, context, update))); pizza.PizzaToppings.Clear(); foreach (var group in mappedToppings.GroupBy(t => t.ToppingType)) { var firstTopping = group.Last(); if (firstTopping is null) { // Should never happen throw new ArgumentException("Everything is on fire. Just rewrite everything please"); } PizzaTopping pt = context.PizzaToppings.Include(pt => pt.Pizza).Include(pt => pt.Topping) .FirstOrDefault(pt => pt.Pizza.ID == pizza.ID && pt.Topping.ID == firstTopping.ID) ?? new PizzaTopping(); if (pt.ID != 0 && !update) { // TODO: check if this is needed //pizza.PizzaToppings.Add(pt); continue; } //it either isn't already in the DB or we need to update it if it does exist pt.Pizza = pizza; pt.Topping = firstTopping; pt.Amount = group.Count(); //firstTopping.PizzaToppings.Add(pt); pizza.PizzaToppings.Add(pt); } pizza.Price = model.Price; if (pizza.ID == 0) { context.Pizzas.Add(pizza); } return(pizza); }
public Entities.Customer Map(Domain.Models.Customer model, Entities.PizzaBoxDbContext context, bool update = false) { Entities.Customer customer = context.Customers.FirstOrDefault(cust => cust.ID == model.ID) ?? new Entities.Customer(); if (customer.ID != 0 && !update) { return(customer); } customer.Name = model.Name; if (customer.ID == 0) { context.Customers.Add(customer); } return(customer); }
public Entities.Topping Map(Domain.Abstracts.ATopping model, Entities.PizzaBoxDbContext context, bool update = false) { Entities.Topping topping = context.Toppings.FirstOrDefault(t => t.ID == model.ID) ?? new Entities.Topping(); if (topping.ID != 0 && !update) { return(topping); } topping.ToppingType = model.ToppingType; topping.Name = model.Name; topping.Price = model.Price; if (topping.ID == 0) { context.Toppings.Add(topping); } return(topping); }
public Entities.Size Map(Domain.Abstracts.ASize model, Entities.PizzaBoxDbContext context, bool update = false) { Entities.Size size = context.Sizes.FirstOrDefault(s => s.ID == model.ID) ?? new Entities.Size(); if (size.ID != 0 && !update) { return(size); } size.SizeType = model.SizeType; size.Name = model.Name; size.Price = model.Price; if (size.ID == 0) { context.Sizes.Add(size); } return(size); }
public Store Map(AStore model, Entities.PizzaBoxDbContext context, bool update = false) { Store store = context.Stores.FirstOrDefault(s => s.ID == model.ID) ?? new Store(); if (store.ID != 0 && !update) { return(store); } store.Name = model.Name; store.StoreType = model.StoreType; if (store.ID == 0) { context.Stores.Add(store); } return(store); }
public Entities.Crust Map(Domain.Abstracts.ACrust model, Entities.PizzaBoxDbContext context, bool update = false) { Entities.Crust crust = context.Crusts.FirstOrDefault(crust => crust.ID == model.ID) ?? new Entities.Crust(); if (crust.ID != 0 && !update) { return(crust); } // either we are updating and we want to cascade all changes // or it didn't exist and we need to instantiate it crust.CrustType = model.CrustType; crust.Price = model.Price; crust.Name = model.Name; // if it is new, insert it into context so a future map can find it if (crust.ID == 0) { context.Crusts.Add(crust); } return(crust); }
public OrderRepository(Entities.PizzaBoxDbContext context) { this.context = context; }