public IActionResult Index() { List <Order> orders = _pizzaOrderService.GetAllOrders(); //This is the right way of fetching data. Instead of using DOMAIN model in the controller //we need to use DTO model. //List<OrderDTO> orders = _pizzaOrderService.GetAllOrders(); //MAPPING SECTION List <OrderItemViewModel> viewOrders = new List <OrderItemViewModel>(); foreach (var order in orders) { List <PizzaViewModel> pizzasView = new List <PizzaViewModel>(); foreach (var pizzaOrder in order.PizzaOrders) { pizzasView.Add(new PizzaViewModel() { Id = pizzaOrder.Pizza.Id, Image = pizzaOrder.Pizza.Image, Name = pizzaOrder.Pizza.Name, Price = pizzaOrder.Pizza.Price, Size = pizzaOrder.Pizza.PizzaSize }); } viewOrders.Add(new OrderItemViewModel() { Id = order.Id, FirstName = order.User.FirstName, LastName = order.User.LastName, Price = order.Price, Pizzas = pizzasView }); } OrdersViewModel model = new OrdersViewModel() { LastPizza = _pizzaOrderService.GetLastOrder().PizzaOrders.FirstOrDefault().Pizza.Name, MostPopularPizza = _pizzaOrderService.GetMostPopularPizza(), NameOfFirstCustomer = _userService.GetLastUserName(), OrderCount = _pizzaOrderService.GetOrderCount(), Orders = viewOrders }; return(View(model)); }
public IActionResult Index() { // Domain models List <Order> orders = _pizzaOrderService.GetAllOrders(); // View Models // MAPPINT SECTION List <OrderItemViewModel> viewOrders = new List <OrderItemViewModel>(); foreach (Order order in orders) { List <PizzaViewModel> pizzasView = new List <PizzaViewModel>(); foreach (var pizzaOrder in order.PizzaOrders) { pizzasView.Add(new PizzaViewModel() { Id = pizzaOrder.Pizza.Id, Image = pizzaOrder.Pizza.Image, Name = pizzaOrder.Pizza.Name, Price = pizzaOrder.Pizza.Price, Size = pizzaOrder.Pizza.Size }); } ; viewOrders.Add(new OrderItemViewModel() { FirstName = order.User.FirstName, LastName = order.User.LastName, Id = order.Id, Price = order.Price, Pizzas = pizzasView }); } OrdersViewModel model = new OrdersViewModel() { LastPizza = _pizzaOrderService.GetLastOrder().PizzaOrders[0].Pizza.Name, MostPopularPizza = _pizzaOrderService.GetMostPopularPizza(), NameOfFirstCustomer = _userService.GetLastUserName(), OrderCount = _pizzaOrderService.GetOrderCount(), Orders = viewOrders }; // We send mapped view model to the view return(View(model)); }
public IActionResult Index() { List <Order> orders = _pizzaOrderService.GetAllOrders(); //MAPPING SECTION List <OrderItemViewModel> viewOrders = new List <OrderItemViewModel>(); foreach (var order in orders) { List <PizzaViewModel> pizzasView = new List <PizzaViewModel>(); foreach (var pizzaOrder in order.PizzaOrders) { pizzasView.Add(new PizzaViewModel() { Id = pizzaOrder.Pizza.Id, Image = pizzaOrder.Pizza.Image, Name = pizzaOrder.Pizza.Name, Price = pizzaOrder.Pizza.Price, Size = pizzaOrder.Pizza.PizzaSize }); } viewOrders.Add(new OrderItemViewModel() { Id = order.Id, Firstname = order.User.FirstName, Lastname = order.User.LastName, Price = order.Price, Pizzas = pizzasView }); } OrdersViewModel model = new OrdersViewModel() { LastPizza = _pizzaOrderService.GetLastOrder().PizzaOrders.FirstOrDefault().Pizza.Name, MostPopularPizza = _pizzaOrderService.GetMostPopularPizza(), NameOfFirstCustomer = _userService.GetLastUserName(), OrderCount = _pizzaOrderService.GetOrderCount(), Orders = viewOrders }; return(View(model)); }