Exemplo n.º 1
0
 public ShoppingCarts(Products product, int quantity, string cartId)
 {
     Product = product;
     Quantity = quantity;
     DateCreated = DateTime.Now;
     CartId = cartId;
 }
        public void AddToCart(Products product)
        {
            var cartItems = _cartRepo.GetAll();

            if (cartItems.Count == 0)
            {
                var cartItem = new ShoppingCarts(product, 1, ShoppingCartId);
                _cartRepo.Save(cartItem);
            }
            else if (cartItems.Count > 0)
            {
                var cartItem =
                    cartItems.FirstOrDefault(x => x.CartId == ShoppingCartId && x.Product.Name == product.Name);
                if (cartItem == null)
                {
                    cartItem = new ShoppingCarts(product, 1, ShoppingCartId);
                    _cartRepo.Save(cartItem);
                }
                else
                {
                    cartItem.Quantity++;
                }
                _cartRepo.Update(cartItem);
            }
        }
 public void Delete(Products entity)
 {
     using (_session.BeginTransaction())
     {
         _session.Delete(entity);
         _session.Transaction.Commit();
     }
 }
        public ActionResult Create(ProductModel model)
        {
            bool isNotExist = _productRepo.FindDuplicateByName(model.Name);

            if (isNotExist)
            {
                Categories category = _categoryRepo.Get(Int32.Parse(model.Category));
                Manufacturers manufacturer = _manufacturersRepo.Get(Int32.Parse(model.Manufacturer));
                var product = new Products(model.Name, model.Description,
                    Convert.ToDecimal(model.Price, CultureInfo.InvariantCulture), category, manufacturer,
                    Int32.Parse(model.Quantity), model.IsFeatured, model.IsRecent, model.IsBestSeller, model.ShortDescription);

                try
                {
                    _productRepo.Save(product);
                    TempData["success"] = "Pomyślnie dodano nowy towar.";
                    return RedirectToAction("index");
                }
                catch (Exception)
                {
                    TempData["error"] = "Wystąpił problem z połączeniem do bazy danych.";
                    return RedirectToAction("Create");
                }
            }
            TempData["error"] = "Towar o podanej nazwie już istnieje";
            return RedirectToAction("Create");
        }
 public void Save(Products entity)
 {
     _session.Save(entity);
 }
Exemplo n.º 6
0
        public void Products()
        {
            var conn = new NHibernateConnection();
            var productRepo = new ProductsRepository(conn);
            var categoryRepo = new CategoryRepository(conn);
            var manufacturersRepository = new ManufacturersRepository(conn);
            Categories category = categoryRepo.Get(4);

            Manufacturers manufacturer = manufacturersRepository.Get(1);

            var product = new Products("Towar", "Jego opis", Convert.ToDecimal(125.99), category, manufacturer, 100,
                false, true, false, "Krótki opis");

            productRepo.Save(product);

            Products fromDB = productRepo.Get(product.ID);

            Assert.AreEqual(product, fromDB);
        }
        public ActionResult Create(CreateProductModel model)
        {
            bool isNotExist = _productsRepository.FindDuplicateByName(model.Name);

            if (isNotExist)
            {
                var categoryId = Convert.ToInt32(model.Category);
                var manufacturerId = Convert.ToInt32(model.Manufacturer);
                var price = model.Price.Contains(".")
                    ? Convert.ToDecimal(model.Price.Replace('.', ','))
                    : Convert.ToDecimal(model.Price);

                var newProduct = new Products(model.Name, model.Description ?? String.Empty, price,
                    _categoryRepository.Get(categoryId), _manufacturerRepository.Get(manufacturerId), model.Quantity,
                    model.IsFeatured, model.IsRecent, model.IsBestseller, model.ShortDescription);

                try
                {
                    _productsRepository.Save(newProduct);
                    if (model.Photo != null)
                    {
                        var target = new MemoryStream();
                        model.Photo.InputStream.CopyTo(target);
                        var data = target.ToArray();
                        var productImage = new ProductImages(model.Photo.FileName, data, newProduct);
                        _productImagesRepository.Save(productImage);
                    }

                    TempData["success"] = String.Format("Produkt {0} została utworzona pomyślnie",
                        newProduct.Name);
                }
                catch (Exception)
                {
                    throw new Exception();
                }

                return RedirectToAction("List");
            }
            TempData["error"] = String.Format("Produkt o nazwie {0} już istnieje", model.Name);
            return RedirectToAction("List");
        }
        public ActionResult Update(CreateProductModel model, int id)
        {
            bool isNotExist = _productsRepository.FindDuplicateByNameAndID(model.Name, id);

            if (isNotExist)
            {
                var categoryId = Convert.ToInt32(model.Category);
                var manufacturerId = Convert.ToInt32(model.Manufacturer);
                var price = model.Price.Contains(".")
                    ? Convert.ToDecimal(model.Price.Replace('.', ','))
                    : Convert.ToDecimal(model.Price);

                var newProduct = new Products(model.Name, model.Description, price, _categoryRepository.Get(categoryId),
                    _manufacturerRepository.Get(manufacturerId), model.Quantity, model.IsFeatured, model.IsRecent,
                    model.IsBestseller, model.ShortDescription) {ID = id};
                try
                {
                    _productsRepository.Update(newProduct);
                    TempData["success"] = String.Format("Produkt {0} został edytowany pomyślnie", newProduct.Name);
                }
                catch (Exception)
                {
                    throw new Exception();
                }
            }
            else
            {
                TempData["error"] = String.Format("Produkt o nazwie {0} już istnieje", model.Name);
            }

            return RedirectToAction("Update", new {id = id});
        }
Exemplo n.º 9
0
        public ActionResult InstallDB()
        {
            _session.CreateSQLQuery("delete from orderdetails").ExecuteUpdate();
            _session.CreateSQLQuery("delete from orders").ExecuteUpdate();
            _session.CreateSQLQuery("delete from productimages").ExecuteUpdate();
            _session.CreateSQLQuery("delete from products").ExecuteUpdate();
            _session.CreateSQLQuery("delete from categories").ExecuteUpdate();
            _session.CreateSQLQuery("delete from  manufacturers").ExecuteUpdate();
            _session.CreateSQLQuery("delete from  users").ExecuteUpdate();
            _session.CreateSQLQuery("delete from deliverytypes").ExecuteUpdate();

            #region catregorie

            var category1 = new Categories("Akcja","");
            var category2 = new Categories("MMO", "");
            var category3 = new Categories("Zręcznościowe", "");
            var category4 = new Categories("Klasyka", "");
            var category5 = new Categories("Strategia", "");
            var category6 = new Categories("Logiczne", "");
            var category7 = new Categories("Bijatyki", "");
            var category8 = new Categories("Sportowe", "");
            var category9 = new Categories("Wyścigi i rajdy", "");
            var category10 = new Categories("Symulatory", "");

            _catRepo.Save(category1);
            _catRepo.Save(category2);
            _catRepo.Save(category3);
            _catRepo.Save(category4);
            _catRepo.Save(category5);
            _catRepo.Save(category6);
            _catRepo.Save(category7);
            _catRepo.Save(category8);
            _catRepo.Save(category9);
            _catRepo.Save(category10);

            #endregion

            #region producenci

            var manufacturer = new Manufacturers("Electronic Arts");
            var manufacturer2 = new Manufacturers("Ubisoft");
            var manufacturer3 = new Manufacturers("CD Projekt RED");
            var manufacturer4 = new Manufacturers("Blizzard");
            var manufacturer5 = new Manufacturers("SEGA");
            var manufacturer6 = new Manufacturers("Valve");
            var manufacturer7 = new Manufacturers("BioVare");
            var manufacturer8 = new Manufacturers("Activision");
            var manufacturer9 = new Manufacturers("RockstarGames");
            var manufacturer10 = new Manufacturers("THQ");

            _manuRepo.Save(manufacturer);
            _manuRepo.Save(manufacturer2);
            _manuRepo.Save(manufacturer3);
            _manuRepo.Save(manufacturer5);
            _manuRepo.Save(manufacturer4);
            _manuRepo.Save(manufacturer6);
            _manuRepo.Save(manufacturer7);
            _manuRepo.Save(manufacturer8);
            _manuRepo.Save(manufacturer9);
            _manuRepo.Save(manufacturer10);

            #endregion

            #region products

            Random rand = new Random();
            for (int i = 0; i < 30; i++)
            {
                string name = "Test " + i;
                string description = "Description " + i;
                string shortDescription = "Short desc " + i;
                var randomPrice = rand.Next(20, 200);
                var category = rand.Next(category1.ID, category6.ID+1);

                var producXt = new Products(name, description, Convert.ToDecimal(randomPrice), _catRepo.Get(category),
                    _manuRepo.Get(category), category, Common.GetRandomBool(), Common.GetRandomBool(),
                    Common.GetRandomBool(), shortDescription);

                _productRepo.Save(producXt);
            }

            #endregion

            #region productImages

            string filename = Server.MapPath(Url.Content("~/FrontEnd/img/empty_gallery.png"));
            //@"C:\Projekty\SklepInternetowy\SklepInternetowy\FrontEnd\img\empty_gallery.png";
            //string filename = @"E:\Projekty\SklepInternetowy\SklepInternetowy\FrontEnd\img\empty_gallery.png";
            byte[] bytes = System.IO.File.ReadAllBytes(filename);

            IList<Products> products = _productRepo.GetAll();

            foreach (Products productse in products)
            {
                var productImages = new ProductImages(filename, bytes, productse);
                _productImagesRepository.Save(productImages);
            }

            #endregion

            #region users

            string password = "******";

            var crypto = new PBKDF2();
            string enryptPass = crypto.Compute(password);

            string Email = "*****@*****.**";
            string City = "WWA";
            string Address = "Sik 41/12";
            bool IsAdmin = false;
            string Name = "Name";
            string Surname = "Surname";
            string ipAddress = "102.154.12.12";
            string ZipCode = "12-222";
            string Password = enryptPass;
            string PasswordSalt = crypto.Salt;

            var user = new Users(Name, Surname, Email, Password, City, Address, ZipCode, IsAdmin,
                PasswordSalt, ipAddress);

            string pass = "******";
            string email2 = "*****@*****.**";
            string enryptPass2 = crypto.Compute(pass);
            string password2 = enryptPass2;
            string passwordSalt = crypto.Salt;

            var user2 = new Users(Name, Surname, email2, password2, City, Address, ZipCode, true,
                passwordSalt, ipAddress);

            _userRepo.Save(user);
            _userRepo.Save(user2);

            #endregion

            #region deliveryTypes

            var deliveryType = new DeliveryTypes("Poczta Polska", Convert.ToDecimal(8.99));
            _deliveryRepo.Save(deliveryType);

            #endregion

            #region orders
            Random rnd = new Random();
            for (int i = 0; i < 15; i++)
            {
                System.Threading.Thread.Sleep(50);
                var order1 = new Orders(user, DateTime.Now.AddDays(-2.0), Convert.ToDecimal(rnd.Next(50, 400)), deliveryType);
                var order2 = new Orders(user2, DateTime.Now.AddDays(-rnd.Next(1, 50)), Convert.ToDecimal(rnd.Next(50, 400)), deliveryType);
                _ordersRepository.Save(order1);
                _ordersRepository.Save(order2);
            }

            var order = new Orders(user, DateTime.Now, Convert.ToDecimal(29.99), deliveryType);
            _ordersRepository.Save(order);

            #endregion

            string[] myCookies = Request.Cookies.AllKeys;
            foreach (string cookie in myCookies)
            {
                Response.Cookies[cookie].Expires = DateTime.Now.AddDays(-1);
            }

            FormsAuthentication.SignOut();

            return RedirectToAction("index", "Home");
        }
        public void SaveProduct()
        {
            var category = _categoryRepository.GetAll().First();
            var manufacturer = _manufacturersRepository.GetAll().First();

            Assert.IsNotNull(category);
            Assert.IsNotNull(manufacturer);

            var product =
                new Products("TestName", "", Convert.ToDecimal(29.99), category, manufacturer, 1, true, true, true, "");

            Assert.AreEqual(0,product.ID);

            _productsRepository.Save(product);

            Assert.AreNotEqual(0,product.ID);
        }