Beispiel #1
0
        public ActionResult AddToCart(FullBookViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var bookModel = this.Data.Books.GetById(viewModel.Id);

                if (bookModel != null)
                {
                    var bookHasEnoughQuantityInStorage = viewModel.QuantityToAddToCart <= bookModel.Quantity;

                    if (!bookHasEnoughQuantityInStorage)
                    {
                        this.TempData[GlobalConstants.WarningMessage] = "Няма налично количество";
                        return this.RedirectToAction("Details", "Books", new { id = viewModel.Id });
                    }

                    bookModel.Quantity -= viewModel.QuantityToAddToCart;
                    var cartModel = new ShopingCartItem
                    {
                        BookId = viewModel.Id,
                        UserId = this.UserProfile.Id,
                        OrderId = "test test"
                    };
                    this.Data.ShopingCart.Add(cartModel);
                    this.Data.SaveChanges();

                    var successMessage = string.Format("Успешно добавихте {0} артикула в количката си.", viewModel.QuantityToAddToCart);
                    this.TempData[GlobalConstants.SuccessMessage] = successMessage;
                    return this.RedirectToAction("Index", "Books");
                }

                this.TempData[GlobalConstants.DangerMessage] = "Няма такава книга";
                return this.RedirectToAction("Index", "Books");
            }

            this.TempData[GlobalConstants.DangerMessage] = "Невалидно количество";
            return this.RedirectToAction("Details", "Books", new { id = viewModel.Id });
        }
Beispiel #2
0
        private static void MigrateShopingCart()
        {
            var uow = new SbsData();

            const string ConnectionString = "Data Source=.;Initial Catalog=BooksDBSQL;Integrated Security=True";
            const string queryString = "SELECT * FROM dbo.ShopCart";

            using (SqlConnection connection =
                new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand(queryString, connection);

                try
                {
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        var bookId = reader.GetString(1);
                        var book = uow.Books.All().FirstOrDefault(x => x.BookId == bookId);

                        if (book != null)
                        {
                            var user = uow.Users.All().FirstOrDefault(x => x.Email == "*****@*****.**") ?? uow.Users.All().FirstOrDefault();

                            var cartItem = new ShopingCartItem()
                            {
                                OrderId = reader.GetString(0),
                                BookId = book.Id,
                                UserId = user.Id
                            };

                            uow.ShopingCart.Add(cartItem);
                            uow.SaveChanges();
                        }
                    }

                    Console.WriteLine("Done");

                    reader.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }