コード例 #1
0
        public void ShoppingCartViewModel_CheckMethods()
        {
            ShoppingCartViewModel scvm = new ShoppingCartViewModel
            {
                Basket = MockData.GetMoqOrderedBooksList().ToList()
            };

            double totalPrice  = scvm.TotalPrice();
            double totalAmount = scvm.TotalAmount();

            Assert.Equal(10, totalAmount);
            Assert.Equal(906.94, totalPrice);
        }
コード例 #2
0
ファイル: HomeController.cs プロジェクト: Aviatore/shop
        public IActionResult CheckOut(ShoppingCartViewModel scvm)
        {
            double totalPrice = scvm.TotalPrice();

            if (!ModelState.IsValid)
            {
                return(View("ShoppingCart", scvm));
            }

            Address billAdd = scvm.Order.BillingAddress;

            scvm.Order.BillingAddressId = Helper.AddAddressToDbOrGetID(_dbContext, billAdd);

            if (scvm.ShippingEqualBilling || scvm.Order.BillingAddress == scvm.Order.ShippingAddress)
            {
                scvm.Order.ShippingAddressId = scvm.Order.BillingAddressId;
            }
            else
            {
                Address shipAdd = scvm.Order.ShippingAddress;
                scvm.Order.ShippingAddressId = Helper.AddAddressToDbOrGetID(_dbContext, shipAdd);
            }

            User user = scvm.Order.User;

            /*int? userId = _dbContext.Users
             *  .Where(u => u.UserName.Equals(user.UserName) && u.Email.Equals(user.Email) && u.Phone.Equals(user.Phone))
             *  .Select(u => (int?)u.UserId)
             *  .FirstOrDefault();*/

            var userAuthId = HttpContext.Session.Get <string>("userId");

            scvm.Order.UserId = Helper.GetUserIdById(_dbContext, userAuthId);

            /*if (userId.HasValue)
             *  //scvm.Order.UserId = userId.Value;
             * {
             *  var userAuthId = HttpContext.Session.Get<string>("userId");
             *  scvm.Order.UserId = Helper.GetUserIdById(_dbContext, userAuthId);
             * }
             * else
             * {
             *  _dbContext.Users.Add(user);
             *  _dbContext.SaveChanges();
             *  _dbContext.Entry(user).GetDatabaseValues();
             *  scvm.Order.UserId = user.UserId;
             * }*/

            (int orderId, bool updated) = Helper.AddOrderToDbOrGetId(_dbContext, scvm.Order);

            _myLogger.Add(orderId, "Order data accepted");

            if (updated)
            {
                Helper.UpdateOrderedBooksInDb(_dbContext, orderId, scvm.Basket);
            }
            else
            {
                Helper.AddOrderedBooksToDb(_dbContext, orderId, scvm.Basket);
            }

            return(RedirectToAction("Payment", new
            {
                orderId = orderId,
                totalPrice = scvm.TotalPrice()
            }));
        }
コード例 #3
0
        public IActionResult CheckOut(ShoppingCartViewModel scvm)
        {
            double totalPrice = scvm.TotalPrice();

            if (!ModelState.IsValid)
            {
                return(View("ShoppingCart", scvm));
            }

            Address billAdd = scvm.Order.BillingAddress;

            scvm.Order.BillingAddressId = AddToAddressDBOrGetID(billAdd);

            if (scvm.ShippingEqualBilling || scvm.Order.BillingAddress == scvm.Order.ShippingAddress)
            {
                scvm.Order.ShippingAddressId = scvm.Order.BillingAddressId;
            }
            else
            {
                Address shipAdd = scvm.Order.ShippingAddress;
                scvm.Order.ShippingAddressId = AddToAddressDBOrGetID(shipAdd);
            }

            User user   = scvm.Order.User;
            int? userId = _dbContext.Users
                          .Where(u => u.UserName.Equals(user.UserName) && u.Email.Equals(user.Email) && u.Phone.Equals(user.Phone))
                          .Select(u => (int?)u.UserId)
                          .FirstOrDefault();

            if (userId.HasValue)
            {
                scvm.Order.UserId = userId.Value;
            }
            else
            {
                _dbContext.Users.Add(user);
                _dbContext.SaveChanges();
                _dbContext.Entry(user).GetDatabaseValues();
                scvm.Order.UserId = user.UserId;
            }

            var order = scvm.Order;

            scvm.Order.User            = null;
            scvm.Order.BillingAddress  = null;
            scvm.Order.ShippingAddress = null;
            _dbContext.Orders.Add(order);
            _dbContext.SaveChanges();
            _dbContext.Entry(order).GetDatabaseValues();
            int orrderId = order.OrderId;

            foreach (var item in scvm.Basket)
            {
                var book = new BooksOrdered();
                book.BookId  = item.BookId;
                book.OrderId = orrderId;
                for (var i = 0; i < item.Quantity; i++)
                {
                    _dbContext.BooksOrdereds.Add(book);
                }
                _dbContext.SaveChanges();
            }


            return(RedirectToAction("Payment", new
            {
                orderId = orrderId,
                totalPrice = scvm.TotalPrice()
            }));
        }