Ejemplo n.º 1
0
        public async Task <ActionResult> Details(int?orderId, int?productId)
        {
            if (orderId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            if (productId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            OrderProduct orderProduct = await context.OrderProducts
                                        .Include(op => op.Order)
                                        .Include(op => op.Product)
                                        .Include(op => op.OrderProductAddresses)
                                        .Include(op => op.OrderProductMaterials)
                                        .Include(op => op.OrderProductDeliveries)
                                        .Include(op => op.OrderProductAddresses.Select(opa => opa.Address))
                                        .Include(op => op.OrderProductMaterials.Select(opm => opm.Material))
                                        .Include(op => op.OrderProductMaterials.Select(opm => opm.Supplier))
                                        .FirstOrDefaultAsync(op => op.OrderId == orderId &&
                                                             op.ProductId == productId);

            if (orderProduct == null)
            {
                return(HttpNotFound());
            }

            OrderProductDetailsViewModel model = new OrderProductDetailsViewModel(orderProduct);

            if (orderProduct.OrderProductAddresses.Any())
            {
                foreach (var orderAddressProduct in orderProduct.OrderProductAddresses)
                {
                    model.OrderProductAddresses.Add(new OrderProductAddressViewModel(orderAddressProduct));
                }
            }

            if (orderProduct.OrderProductMaterials.Any())
            {
                foreach (var orderAddressMaterial in orderProduct.OrderProductMaterials)
                {
                    model.OrderProductMaterials.Add(new OrderProductMaterialViewModel(orderAddressMaterial));
                }
            }

            if (orderProduct.OrderProductDeliveries.Any())
            {
                foreach (var orderProductDelivery in orderProduct.OrderProductDeliveries)
                {
                    model.OrderProductDeliveries.Add(new OrderProductDeliveryViewModel(orderProductDelivery));
                }
            }

            return(View(model));
        }
        public async Task <IActionResult> Details(string orderId)
        {
            var viewModel = new OrderDetailsViewModel();

            var order = this.db.Orders.FirstOrDefault(x => x.Id.ToString() == orderId);

            var user = await this.userManager.FindByIdAsync(order.UserId);

            viewModel.UserName  = user.UserName;
            viewModel.FullPrice = order.TotalPrice;

            List <OrderItem> orderItems = this.db.OrderItem.Where(x => x.OrderId.ToString() == orderId).ToList();

            if (orderItems == null)
            {
                return(View(viewModel));
            }

            List <OrderProductDetailsViewModel> orderProductDetails = new List <OrderProductDetailsViewModel>();

            foreach (var product in orderItems)
            {
                var bag      = this.db.Bags.FirstOrDefault(x => x.Id == product.BagId);
                var bagOrder = this.db.OrderItem.FirstOrDefault(x => x.BagId == product.BagId);

                if (bag != null)
                {
                    var productViewModel = new OrderProductDetailsViewModel
                    {
                        ProductName  = bag.Title,
                        ProductPrice = bagOrder.BagPrice,

                        Quantity = product.Quantity
                    };
                    orderProductDetails.Add(productViewModel);
                }
            }
            viewModel.Products = orderProductDetails;

            viewModel.OrderId = orderId.ToString();

            return(View(viewModel));
        }