public async Task <ActionResult> AddSizeToOrderProductAddressProductionDate(int?orderId, int?productId, int?addressId, DateTime?productionDate)
        {
            if (orderId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

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

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

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

            OrderProductAddressProductionDate orderProductAddressProductionDate = await context.OrderProductAddressProductionDates
                                                                                  .Include(opapd => opapd.OrderProductAddress.OrderProduct.Order)
                                                                                  .Include(opapd => opapd.OrderProductAddress.OrderProduct.Product)
                                                                                  .Include(opapd => opapd.OrderProductAddress.Address)
                                                                                  .FirstOrDefaultAsync(opapd => opapd.OrderId == orderId &&
                                                                                                       opapd.ProductId == productId &&
                                                                                                       opapd.AddressId == addressId &&
                                                                                                       opapd.ProductionDate == productionDate);

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

            AddSizeToOrderProductAddressProductionDateViewModel model = new AddSizeToOrderProductAddressProductionDateViewModel();

            model.OrderProductAddressProductionDate = new OrderProductAddressProductionDateViewModel(orderProductAddressProductionDate);
            model.AvailableSizes = GetAvailableSizes(orderProductAddressProductionDate);

            return(View(model));
        }
        public async Task <ActionResult> AddSizeToOrderProductAddressProductionDate(AddSizeToOrderProductAddressProductionDateViewModel model)
        {
            OrderProductAddressProductionDate orderProductAddressProductionDate = await context.OrderProductAddressProductionDates
                                                                                  .FindAsync(model.OrderProductAddressProductionDate.OrderProductAddress.OrderProduct.Order.Id,
                                                                                             model.OrderProductAddressProductionDate.OrderProductAddress.OrderProduct.Product.Id,
                                                                                             model.OrderProductAddressProductionDate.OrderProductAddress.Address.Id,
                                                                                             model.OrderProductAddressProductionDate.ProductionDate);

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

            if (!ModelState.IsValid)
            {
                model.AvailableSizes = GetAvailableSizes(orderProductAddressProductionDate, model.SizeId);

                return(View(model));
            }

            Size size = await context.Sizes.FindAsync(model.SizeId);

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

            context.OrderProductAddressProductionDateSizes.Add(new OrderProductAddressProductionDateSize
            {
                OrderProductAddressProductionDate = orderProductAddressProductionDate,
                Size     = size,
                Quantity = model.Quantity
            });

            try
            {
                await context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                string errorMessage = ex.Message;

                while (ex != null)
                {
                    errorMessage = ex.Message;
                    ex           = ex.InnerException;
                }

                ModelState.AddModelError("", errorMessage);

                return(View(model));
            }

            return(RedirectToAction(nameof(Details),
                                    new
            {
                orderId = orderProductAddressProductionDate.OrderId,
                productId = orderProductAddressProductionDate.ProductId,
                addressId = orderProductAddressProductionDate.AddressId,
                productionDate = orderProductAddressProductionDate.ProductionDate
            }));
        }