Exemplo n.º 1
0
        public IActionResult SortOptions(int id, DrinkOrderOptionsViewModel options)
        {
            foreach (var sizeModel in options.Sizes)
            {
                var sizeOption = _drinkRepository.GetSizeById(sizeModel.Id);
                _mapper.Map(sizeModel, sizeOption);
                _drinkRepository.UpdateDrinkSize(sizeOption);
            }

            foreach (var iceModel in options.Ices)
            {
                var iceOption = _drinkRepository.GetIceById(iceModel.Id);
                _mapper.Map(iceModel, iceOption);
                _drinkRepository.UpdateIceOption(iceOption);
            }

            foreach (var sugarModel in options.Sugars)
            {
                var sugarOption = _drinkRepository.GetSugarById(sugarModel.Id);
                _mapper.Map(sugarModel, sugarOption);
                _drinkRepository.UpdateSugarOption(sugarOption);
            }
            return(RedirectToAction("SortOptions", new { drinkId = id }));
        }
        public IActionResult EditDetails(EditOrderDetailModel orderDetailModel)
        {
            if (orderDetailModel.OrderDetailToppings != null)
            {
                orderDetailModel.OrderDetailToppings.RemoveAll(t => !t.IsChecked);
            }
            var isNew = orderDetailModel.OrderDetailId == 0;

            var orderDetail = isNew ? new OrderDetail() : _orderDetailRepository.GetById(orderDetailModel.OrderDetailId);

            _mapper.Map(orderDetailModel, orderDetail);

            //set drink name/drink size if new
            if (isNew)
            {
                var drink = _drinkRepository.GetDrinkById(orderDetail.DrinkId);
                orderDetail.DrinkName  = drink.Name;
                orderDetail.IsNewDrink = drink.IsNew;
            }

            var drinkSize = _drinkRepository.GetSizeById(orderDetailModel.DrinkSizeId);

            orderDetail.DrinkSize = drinkSize.Name;
            orderDetail.Price     = drinkSize.Price;

            //update price
            orderDetail.FullPrice = orderDetail.Price + (orderDetail.OrderDetailToppings?.Sum(t => t.Price * t.Quantity) ?? 0);
            orderDetail.Amount    = orderDetail.FullPrice * orderDetail.Quantity;

            var success = isNew ? _orderDetailRepository.Create(orderDetail) : _orderDetailRepository.Edit(orderDetail);

            if (success)
            {
                TempData["Message"] = "Lưu thành công";
            }
            else
            {
                TempData["Message"] = "Lưu thất bại";
            }

            //update order
            var order = _orderRepository.GetById(orderDetail.OrderId);

            order.IsSpecialPromo = _promotionRepository.CheckPromotionBuy1Get1();
            _orderRepository.Edit(order);

            return(new RedirectResult(Url.Action("Edit", new { orderId = orderDetail.OrderId }) + "#editdrinks"));
        }