Exemple #1
0
        public Order CreateOrder(Order order)
        {
            _IOrderRepository.Add(order);

            var orderLines = order.OrderLines;

            if (orderLines != null && orderLines.Any())
            {
                foreach (var item in orderLines)
                {
                    _IOrderLineRepository.Add(item);
                }
            }

            _IUnitOfWork.Commit();

            return(order);
        }
        public ActionResult Order([Bind(Include = "FirstName,LastName,Phone,Email,Street,HouseNumber,ApartmentNumber,NamePaymentType,OrderDeliveryTime,Comment")] UserDelivery userAddress)
        {
            List <PaymentType> payments = paymentTypeRepository.PaymentTypes.ToList();

            ViewBag.PaymentTypes = new SelectList(payments, "Id", "NamePaymentType", payments.First(x => x.Id == int.Parse(userAddress.NamePaymentType)));

            if (!(Session["order"] is Order order) || order.OrderLines.Count == 0)
            {
                ModelState.AddModelError("", "Корзина заказов пуста");

                return(View(userAddress));
            }

            userAddress.OrderDeliveryTime ??= DateTime.Now.AddHours(1);

            if (DateTime.Now.AddMinutes(59).CompareTo(userAddress.OrderDeliveryTime) > 0)
            {
                ModelState.AddModelError("OrderDeliveryTime", "Время для доставки мин. 1 час");

                return(View(userAddress));
            }

            if (!ModelState.IsValid)
            {
                return(View(userAddress));
            }

            //-----------------------------------

            Order newOrder = null;
            User  user     = userRepository.Users.FirstOrDefault(u => u.Phone == userAddress.Phone);

            Address address = addressRepository.Addresses.FirstOrDefault(a => a.Street == userAddress.Street &&
                                                                         a.HouseNumber == userAddress.HouseNumber &&
                                                                         a.ApartmentNumber == userAddress.ApartmentNumber);

            Address newAddress = null;

            if (address == null)
            {
                newAddress = new Address
                {
                    Street          = userAddress.Street,
                    HouseNumber     = userAddress.HouseNumber,
                    ApartmentNumber = userAddress.ApartmentNumber
                };
            }

            if (user == null)
            {
                user = new User
                {
                    FirstName = userAddress.FirstName,
                    LastName  = userAddress.LastName,
                    Phone     = userAddress.Phone,
                    Email     = userAddress.Email
                };

                if (address == null)
                {
                    user.Addresses.Add(newAddress);

                    newOrder = new Order
                    {
                        User    = user,
                        Address = newAddress
                    };
                }
                else
                {
                    address.Users.Add(user);
                    addressRepository.Update(address);

                    newOrder = new Order
                    {
                        UserId    = user.Id,
                        AddressId = address.Id
                    };
                }
            }
            else
            {
                if (address == null)
                {
                    user.Addresses.Add(newAddress);
                    userRepository.Update(user);

                    newOrder = new Order
                    {
                        UserId    = user.Id,
                        AddressId = newAddress.Id
                    };
                }
                else
                {
                    bool isAddressUser = false;

                    foreach (var item in user.Addresses)
                    {
                        if (item.Street == userAddress.Street && item.HouseNumber == userAddress.HouseNumber &&
                            item.ApartmentNumber == userAddress.ApartmentNumber)
                        {
                            isAddressUser = true;
                            break;
                        }
                    }

                    if (!isAddressUser)
                    {
                        userRepository.Add(user, address);
                    }

                    newOrder = new Order
                    {
                        UserId    = user.Id,
                        AddressId = address.Id
                    };
                }
            }

            newOrder.Comment           = userAddress.Comment;
            newOrder.OrderDate         = DateTime.Now;
            newOrder.OrderDeliveryTime = (DateTime)userAddress.OrderDeliveryTime;
            newOrder.PaymentTypeId     = int.Parse(userAddress.NamePaymentType);

            orderRepository.Add(newOrder);

            foreach (var item in order.OrderLines)
            {
                OrderLine orderLine = new OrderLine
                {
                    DishId   = item.Dish.Id,
                    Quantity = item.Quantity,
                    OrderId  = newOrder.Id
                };

                orderLineRepository.Add(orderLine);
            }
            Session["order"] = null;
            order            = orderRepository.Orders.First(o => o.Id == newOrder.Id);

            return(View("OrderAccepted", order));
        }