Example #1
0
        public IActionResult Order()
        {
            List <Order> orders = new List <Order>();

            if (User.IsInRole("admin"))
            {
                orders = _orderRepository.GetAllOrders().ToList();
            }
            else
            {
                orders = _orderRepository.GetOrdersByUserId(_userManager.GetUserId(User)).ToList();
            }
            List <OrderVM> orderVMs = new List <OrderVM>();

            foreach (Order order in orders)
            {
                OrderVM orderVM = new OrderVM();
                orderVM.Name     = _productRepository.GetProductById(order.ProductId).Name;
                orderVM.Price    = order.Price;
                orderVM.Status   = _orderStatusRepository.GetOrderStatusById(order.StatusId);
                orderVM.OrderId  = order.Id;
                orderVM.UserName = _userManager.Users.FirstOrDefault(u => u.Id == order.UserId).UserName;
                orderVMs.Add(orderVM);
            }
            ViewBag.Statuses = _orderStatusRepository.GetAllStatuses();
            ViewBag.Orders   = orderVMs;
            return(View());
        }