Ejemplo n.º 1
0
        public async Task <ActionResult> AddDeliveryToOrderProduct(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.OrderProductDeliveries)
                                        .FirstOrDefaultAsync(op => op.OrderId == orderId &&
                                                             op.ProductId == productId);

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

            AddDeliveryToOrderProductViewModel model = new AddDeliveryToOrderProductViewModel();

            model.OrderProduct = new OrderProductViewModel(orderProduct);

            return(View(model));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> AddDeliveryToOrderProduct(AddDeliveryToOrderProductViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            OrderProduct orderProduct = await context.OrderProducts
                                        .Include(op => op.OrderProductDeliveries)
                                        .FirstOrDefaultAsync(op => op.OrderId == model.OrderProduct.Order.Id &&
                                                             op.ProductId == model.OrderProduct.Product.Id);

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

            OrderProductDelivery orderProductDelivery = new OrderProductDelivery();

            orderProductDelivery.OrderProduct   = orderProduct;
            orderProductDelivery.DeliveryId     = model.DeliveryId;;
            orderProductDelivery.DeliveryDate   = model.DeliveryDate;
            orderProductDelivery.AcceptanceId   = model.AcceptanceId;
            orderProductDelivery.AcceptanceDate = model.AcceptanceDate;

            context.OrderProductDeliveries.Add(orderProductDelivery);

            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 = orderProduct.OrderId,
                productId = orderProduct.ProductId
            }));
        }