private List <SelectListItem> GetAvailableSizes(OrderProductAddress orderProductAddress, int?sizeId = null)
        {
            List <SelectListItem> availableSizes = new List <SelectListItem>();

            if (context.Sizes.Any())
            {
                foreach (var size in context.Sizes.Include(s => s.OrderProductAddressSizes))
                {
                    OrderProductAddressSize orderProductAddressSize = size.OrderProductAddressSizes
                                                                      .FirstOrDefault(opas => opas.OrderId == orderProductAddress.OrderId &&
                                                                                      opas.ProductId == orderProductAddress.ProductId &&
                                                                                      opas.AddressId == orderProductAddress.AddressId &&
                                                                                      opas.SizeId == size.Id);

                    if (orderProductAddressSize == null)
                    {
                        availableSizes.Add(new SelectListItem
                        {
                            Value    = size.Id.ToString(),
                            Text     = size.Name,
                            Selected = size.Id == sizeId
                        });
                    }
                }
            }

            return(availableSizes);
        }
Example #2
0
        public async Task <ActionResult> Edit(EditOrderProductAddressSizeViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            OrderProductAddressSize orderProductAddressSize = await context.OrderProductAddressSizes
                                                              .FindAsync(model.OrderProductAddress.OrderProduct.Order.Id,
                                                                         model.OrderProductAddress.OrderProduct.Product.Id,
                                                                         model.OrderProductAddress.Address.Id,
                                                                         model.Size.Id);

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

            orderProductAddressSize.Quantity = model.Quantity;

            context.Entry(orderProductAddressSize).State = EntityState.Modified;

            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 = orderProductAddressSize.OrderId,
                productId = orderProductAddressSize.ProductId,
                addressId = orderProductAddressSize.AddressId,
                sizeId = orderProductAddressSize.SizeId
            }));
        }
Example #3
0
        public async Task <ActionResult> Edit(int?orderId, int?productId, int?addressId, int?sizeId)
        {
            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 (sizeId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            OrderProductAddressSize orderProductAddressSize = await context.OrderProductAddressSizes
                                                              .Include(opas => opas.OrderProductAddress.OrderProduct.Order)
                                                              .Include(opas => opas.OrderProductAddress.OrderProduct.Product)
                                                              .Include(opas => opas.OrderProductAddress.Address)
                                                              .Include(opas => opas.Size)
                                                              .FirstOrDefaultAsync(opas => opas.OrderId == orderId &&
                                                                                   opas.ProductId == productId &&
                                                                                   opas.AddressId == addressId &&
                                                                                   opas.SizeId == sizeId);

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

            EditOrderProductAddressSizeViewModel model = new EditOrderProductAddressSizeViewModel();

            model.OrderProductAddress = new OrderProductAddressViewModel(orderProductAddressSize.OrderProductAddress);
            model.Size     = new SizeViewModel(orderProductAddressSize.Size);
            model.Quantity = orderProductAddressSize.Quantity;

            return(View(model));
        }
        public async Task <ActionResult> DeleteSizeFromOrderProductAddress(DeleteSizeFromOrderProductAddressViewModel model)
        {
            OrderProductAddressSize orderProductAddressSize = await context.OrderProductAddressSizes
                                                              .FindAsync(model.OrderProductAddress.OrderProduct.Order.Id,
                                                                         model.OrderProductAddress.OrderProduct.Product.Id,
                                                                         model.OrderProductAddress.Address.Id,
                                                                         model.Size.Id);

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

            context.OrderProductAddressSizes.Remove(orderProductAddressSize);

            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 = orderProductAddressSize.OrderId,
                productId = orderProductAddressSize.ProductId,
                addressId = orderProductAddressSize.AddressId
            }));
        }
Example #5
0
 public OrderProductAddressSizeViewModel(OrderProductAddressSize orderProductAddressSize)
 {
     OrderProductAddress = new OrderProductAddressViewModel(orderProductAddressSize.OrderProductAddress);
     Size     = new SizeViewModel(orderProductAddressSize.Size);
     Quantity = orderProductAddressSize.Quantity;
 }