示例#1
0
        public IActionResult Index()
        {
            var listId   = HttpContext.Session.Get <List <int> >("CartId");
            var gameList = new List <Game>();

            if (listId != null)
            {
                foreach (var id in listId)
                {
                    var game = _db.Games.Include(price => price.GamePrice)
                               .Include(image => image.GameImage)
                               .FirstOrDefault(item => item.Id == id);

                    if (game != null)
                    {
                        gameList.Add(game);
                    }
                }
            }

            var cartOrder = new CartOrderViewModel
            {
                GameInCart = gameList,
            };

            return(View(cartOrder));
        }
        public ActionResult OrderCartPartial()
        {
            CartOrderViewModel model = new CartOrderViewModel();
            int quantity             = 0;

            decimal price = 0m;

            if (Session["myCart"] != null)
            {
                var listOrder = (List <CartOrderViewModel>)Session["myCart"];
                foreach (var item in listOrder)
                {
                    quantity += item.Quantity;
                    price    += item.Quantity * item.Price;
                }
                model.Quantity = quantity;
                model.Price    = price;
            }
            else
            {
                model.Quantity = 0;
                model.Price    = 0;
            }

            return(PartialView(model));
        }
        public JsonResult DecreaseProductCart(int productId)
        {
            List <CartOrderViewModel> cartorderVM = Session["myCart"] as List <CartOrderViewModel>;

            using (Db db = new Db())
            {
                CartOrderViewModel model = cartorderVM.FirstOrDefault(x => x.ProductId == productId);
                if (model.Quantity > 1)
                {
                    model.Quantity--;
                }
                else
                {
                    model.Quantity = 0;
                    cartorderVM.Remove(model);
                }

                var result = new
                {
                    quantity = model.Quantity,
                    price    = model.Price
                };
                return(Json(result, JsonRequestBehavior.AllowGet));
            }
        }
示例#4
0
        public async Task Checkout_ModelState_Invalid_Returns_View_with_Model()
        {
            //Arrange
            Mock <ICartService> mockCartService = new Mock <ICartService>();
            UserManager <User>  mockUserManager = TestUserManager <User>();

            CartController controller = new CartController(mockCartService.Object, mockUserManager);

            controller.ModelState.AddModelError("testError", $"Ошибка сгенерированная в тестовом методе " +
                                                $"{nameof(Checkout_ModelState_Invalid_Returns_View_with_Model)} тестового класса {nameof(CartControllerTests)} " +
                                                $"для тестирования первого условного оператора метода {nameof(controller.Checkout)} контроллера {nameof(CartController)}");

            int            expectedOrderId = 11223344;
            OrderViewModel orderViewModel  = new OrderViewModel()
            {
                Id = expectedOrderId
            };

            Mock <IOrderService> mockOrderService = new Mock <IOrderService>();

            //Act
            var ret_IActionResult = await controller.Checkout(orderViewModel, mockOrderService.Object);

            //Assert
            ViewResult         ret_ViewResult = Assert.IsType <ViewResult>(ret_IActionResult);
            CartOrderViewModel model          = Assert.IsAssignableFrom <CartOrderViewModel>(ret_ViewResult.Model);

            Assert.Equal(expectedOrderId, model.Order.Id);
        }
        public async Task <Order> CreateOrder(CartOrderViewModel cartOrderViewModel)
        {
            var response = await PostAsync(ServiceAddress, cartOrderViewModel);

            var result = response.Content.ReadAsAsync <Order>().Result;

            return(result);
        }
示例#6
0
        public IActionResult Index()
        {
            var model = new CartOrderViewModel
            {
                Cart = GetCartViewModel()
            };

            return(View(model));
        }
        public void DeletePosition(int productId)
        {
            List <CartOrderViewModel> cartorderVM = Session["myCart"] as List <CartOrderViewModel>;

            using (Db db = new Db())
            {
                CartOrderViewModel model = cartorderVM.FirstOrDefault(x => x.ProductId == productId);
                cartorderVM.Remove(model);
            }
        }
示例#8
0
        public OrderNumber SetOrderNumber(CartOrderViewModel model)
        {
            var orderNumber = new OrderNumber
            {
                Email         = model.Email,
                UserAgreement = model.UserAgreement,
                PaymentMethod = model.PaymentMethod,
                Promocode     = model.Promocode,
                OrderTime     = DateTime.Now,
            };

            return(orderNumber);
        }
示例#9
0
        public IActionResult Order(CartOrderViewModel model)
        {
            if (!OrderValidate(model))
            {
                return(RedirectToAction("Index", "Cart", model));
            }

            int totalPrice = 0;

            var orderNumber = SetOrderNumber(model);

            foreach (var item in model.Items)
            {
                var game = _db.Games
                           .Include(keys => keys.GameKeys)
                           .Include(price => price.GamePrice)
                           .FirstOrDefault(g => g.Id == item.Key);

                if (game == null)
                {
                    continue;
                }

                if (!ValidateAmountProduct(item.Value, game))
                {
                    return(RedirectToAction("Index", "Cart"));
                }

                SetPrice(game, item.Value, totalPrice, out var gamePrice, out totalPrice);

                var order = new Order
                {
                    ProductId     = game.Id,
                    AmountProduct = item.Value,
                    ProductName   = game.Name,
                    ProductPrice  = gamePrice,
                    OrderNumber   = orderNumber,
                };
                _db.Orders.Add(order);
                SetOrderKeysGame(order, game, item.Value);
            }

            orderNumber.TotalPrice = totalPrice;

            _db.OrderNumbers.Add(orderNumber);
            HttpContext.Session.Remove("CartId");
            HttpContext.Session.Remove("CountOfGoods");
            _db.SaveChanges();
            return(RedirectToAction("Index", "Home"));
        }
示例#10
0
        public bool OrderValidate(CartOrderViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(false);
            }

            if (model.UserAgreement == false)
            {
                return(false);
            }

            return(true);
        }
示例#11
0
        public async Task <Order> CreateOrder(CartOrderViewModel cartOrderViewModel)
        {
            var user = await _userManager.FindByNameAsync(cartOrderViewModel.UserName);

            if (user is null)
            {
                throw new InvalidOperationException($"Пользователь {cartOrderViewModel.UserName} не найден");
            }

            await using var transaction = await _storeContext.Database.BeginTransactionAsync();

            var order = new Order
            {
                Address = cartOrderViewModel.Order.Address,
                Phone   = cartOrderViewModel.Order.Phone,
                User    = user,
                Date    = DateTime.Now
            };

            foreach (var(product, quantity) in cartOrderViewModel.Cart.Items)
            {
                var prod = await _productService.GetProductByIdAsync(product.Id);

                if (prod is null)
                {
                    continue;
                }

                var order_item = new OrderItem()
                {
                    Order    = order,
                    Price    = prod.Price,
                    Product  = prod,
                    Quantity = quantity,
                };
                order.Items.Add(order_item);
            }

            await _unitOfWork.OrderRepository.Add(_mapper.Map <OrderEntity>(order));

            await transaction.CommitAsync();

            return(order);
        }
        public ActionResult AddtomycartPartial(int id)
        {
            List <CartOrderViewModel> cartorderVM = Session["myCart"] as List <CartOrderViewModel> ?? new List <CartOrderViewModel>();
            CartOrderViewModel        model       = new CartOrderViewModel();

            using (Db db = new Db())
            {
                Products product         = db.product.Find(id);
                var      productIsExists = cartorderVM.FirstOrDefault(x => x.ProductId == id);
                //if product isn't exists in the cart
                if (productIsExists == null)
                {
                    cartorderVM.Add(new CartOrderViewModel()
                    {
                        ProductId    = product.Id,
                        ProductName  = product.Name,
                        Price        = product.Price,
                        Quantity     = 1,
                        ImageProduct = product.ImageString
                    });
                }
                //if  product is already in cart, then we will increment quantity
                else
                {
                    productIsExists.Quantity++;
                }
                int     quantity = 0;
                decimal price    = 0m;
                foreach (var item in cartorderVM)
                {
                    quantity += item.Quantity;
                    price    += item.Price * item.Quantity;
                }
                model.Quantity    = quantity;
                model.Price       = price;
                Session["myCart"] = cartorderVM;
            }
            return(PartialView(model));
        }
示例#13
0
 public Task <Order> CreateOrder([FromBody] CartOrderViewModel cartOrderViewModel)
 {
     return(_orderService.CreateOrder(cartOrderViewModel));
 }