예제 #1
0
        public async Task <ActionResult> AddSizeToOrderProductAddressDelivery(int?orderId, int?productId, int?addressId, int?deliveryId)
        {
            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 (deliveryId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            OrderProductAddressDelivery orderProductAddressDelivery = await context.OrderProductAddressDeliveries
                                                                      .Include(opad => opad.OrderProductAddress.OrderProduct.Order)
                                                                      .Include(opad => opad.OrderProductAddress.OrderProduct.Product)
                                                                      .Include(opad => opad.OrderProductAddress.Address)
                                                                      .FirstOrDefaultAsync(opad => opad.OrderId == orderId &&
                                                                                           opad.ProductId == productId &&
                                                                                           opad.AddressId == addressId &&
                                                                                           opad.DeliveryId == deliveryId);

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

            AddSizeToOrderProductAddressDeliveryViewModel model = new AddSizeToOrderProductAddressDeliveryViewModel();

            model.OrderProductAddressDelivery = new OrderProductAddressDeliveryViewModel(orderProductAddressDelivery);
            model.AvailableSizes = GetAvailableSizes(orderProductAddressDelivery);

            return(View(model));
        }
예제 #2
0
        public async Task <ActionResult> AddSizeToOrderProductAddressDelivery(AddSizeToOrderProductAddressDeliveryViewModel model)
        {
            OrderProductAddressDelivery orderProductAddressDelivery = await context.OrderProductAddressDeliveries
                                                                      .FindAsync(model.OrderProductAddressDelivery.OrderProductAddress.OrderProduct.Order.Id,
                                                                                 model.OrderProductAddressDelivery.OrderProductAddress.OrderProduct.Product.Id,
                                                                                 model.OrderProductAddressDelivery.OrderProductAddress.Address.Id,
                                                                                 model.OrderProductAddressDelivery.DeliveryId);

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

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

                return(View(model));
            }

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

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

            context.OrderProductAddressDeliverySizes.Add(new OrderProductAddressDeliverySize
            {
                OrderProductAddressDelivery = orderProductAddressDelivery,
                Size               = size,
                DeliveryQuantity   = model.DeliveryQuantity,
                AcceptanceQuantity = model.AcceptanceQuantity
            });

            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 = orderProductAddressDelivery.OrderId,
                productId = orderProductAddressDelivery.ProductId,
                addressId = orderProductAddressDelivery.AddressId,
                deliveryId = orderProductAddressDelivery.DeliveryId
            }));
        }