public ActionResult Create(CategoryModel model)
        {
            if (ModelState.IsValid)
            {
                bool isNotExist = _categoryRepository.FindDuplicateByName(model.Name);

                if (isNotExist)
                {
                    var newCategory = new Categories(model.Name, model.Description);

                    try
                    {
                        _categoryRepository.Save(newCategory);
                        TempData["success"] = String.Format("Kategoria {0} została utworzona pomyślnie",
                            newCategory.Name);
                    }
                    catch (Exception)
                    {
                        throw new Exception();
                    }
                    return RedirectToAction("List");
                }
                TempData["error"] = String.Format("Kategoria o nazwie {0} już istnieje", model.Name);
                return RedirectToAction("Create");
            }
            TempData["error"] = "Zostały błednie wprowadzone dane";
            return RedirectToAction("Create");
        }
 public void Update(Categories entity)
 {
     using (_session.BeginTransaction())
     {
         _session.Update(entity);
         _session.Transaction.Commit();
     }
 }
        public void DeleteCategory()
        {
            var category = categoryRepository.GetAll().FirstOrDefault(x => x.Name == "Test");

            if (category == null)
            {
                category = new Categories("Test","");
                categoryRepository.Save(category);
            }

            categoryRepository.Delete(category);

            var category2 = categoryRepository.Get(category.ID);

            Assert.IsNull(category2);
        }
        public ActionResult Index()
        {
            _session.CreateSQLQuery("delete orderdetails").ExecuteUpdate();
            _session.CreateSQLQuery("delete shoppingcarts").ExecuteUpdate();
            _session.CreateSQLQuery("delete productimages").ExecuteUpdate();
            _session.CreateSQLQuery("delete orders").ExecuteUpdate();
            _session.CreateSQLQuery("delete  users").ExecuteUpdate();
            _session.CreateSQLQuery("delete products").ExecuteUpdate();
            _session.CreateSQLQuery("delete orderstate").ExecuteUpdate();
            _session.CreateSQLQuery("delete deliverytypes").ExecuteUpdate();
            _session.CreateSQLQuery("delete categories").ExecuteUpdate();
            _session.CreateSQLQuery("delete  manufacturers").ExecuteUpdate();

            #region OrderStates

            var orderState1 = new OrderState("Nowe zamówienie");
            var orderState2 = new OrderState("W trakcie realizacji");
            var orderState3 = new OrderState("Przekazano do doręczenia");

            #endregion

            #region manufacturers

            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("RockstarGames");

            #endregion

            #region catregories

            var category1 = new Categories("Akcja", "");
            var category2 = new Categories("MMORpg", "Massively multiplayer online role-playing game");
            var category3 = new Categories("Sport", "");
            var category4 = new Categories("Klasyka", "");
            var category5 = new Categories("Pozostałe", "");

            #endregion

            #region products

            var productList = new List<Products>
            {
                new Products("Fifa", "", randomDecimal(5, 150), category3,
                    manufacturer, 15, Common.GetRandomBool(), Common.GetRandomBool(),
                    Common.GetRandomBool(), ""),
                new Products("Need for speed", "", randomDecimal(5, 150), category3,
                    manufacturer, 15, Common.GetRandomBool(), Common.GetRandomBool(),
                    Common.GetRandomBool(), ""),
                new Products("The sims", "", randomDecimal(5, 150), category5,
                    manufacturer, 15, Common.GetRandomBool(), Common.GetRandomBool(),
                    Common.GetRandomBool(), ""),
                new Products("Battlefield", "", randomDecimal(5, 150), category4,
                    manufacturer, 15, Common.GetRandomBool(), Common.GetRandomBool(),
                    Common.GetRandomBool(), ""),
                new Products("Medal of honor", "", randomDecimal(5, 150), category4,
                    manufacturer, 15, Common.GetRandomBool(), Common.GetRandomBool(),
                    Common.GetRandomBool(), ""),
                new Products("Watch dogs", "", randomDecimal(5, 150), category1,
                    manufacturer2, 5, Common.GetRandomBool(), Common.GetRandomBool(),
                    Common.GetRandomBool(), ""),
                new Products("Far cry", "", randomDecimal(5, 150), category1,
                    manufacturer2, 12, Common.GetRandomBool(), Common.GetRandomBool(),
                    Common.GetRandomBool(), ""),
                new Products("Heroes V", "", randomDecimal(5, 150), category4,
                    manufacturer2, 1, Common.GetRandomBool(), Common.GetRandomBool(),
                    Common.GetRandomBool(), ""),
                new Products("Assassins Creed", "", randomDecimal(5, 150), category5,
                    manufacturer2, 4, Common.GetRandomBool(), Common.GetRandomBool(),
                    Common.GetRandomBool(), ""),
                new Products("Wiedźmin", "", randomDecimal(5, 150), category1,
                    manufacturer3, 25, Common.GetRandomBool(), Common.GetRandomBool(),
                    Common.GetRandomBool(), ""),
                new Products("Total war: Rome II", "", randomDecimal(5, 150), category1,
                    manufacturer4, 13, Common.GetRandomBool(), Common.GetRandomBool(),
                    Common.GetRandomBool(), ""),
                new Products("Company of heroes", "", randomDecimal(5, 150), category4,
                    manufacturer4, 9, Common.GetRandomBool(), Common.GetRandomBool(),
                    Common.GetRandomBool(), ""),
                new Products("GTA", "", randomDecimal(5, 150), category5,
                    manufacturer5, 13, Common.GetRandomBool(), Common.GetRandomBool(),
                    Common.GetRandomBool(), ""),
                new Products("Max payne", "", randomDecimal(5, 150), category4,
                    manufacturer5, 9, Common.GetRandomBool(), Common.GetRandomBool(),
                    Common.GetRandomBool(), "")
            };

            #endregion

            #region productImages

            string filename = Server.MapPath(Url.Content("~/FrontEnd/img/empty_gallery.png"));
            byte[] bytes = System.IO.File.ReadAllBytes(filename);
            var products = _productRepo.GetAll();

            #endregion

            #region users

            const string password = "******";
            var crypto = new PBKDF2();
            string enryptPass = crypto.Compute(password);
            string passwordSalt = crypto.Salt;

            const string email = "*****@*****.**";
            const string city = "Warszawa";
            const string address = "Sik 41/12";
            const bool isAdmin = false;
            const string userName = "******";
            const string surname = "Surname";
            const string ipAddress = "102.154.12.12";
            const string zipCode = "12-222";

            var user = new Users(userName, surname, email, enryptPass, city, address, zipCode, isAdmin,
                passwordSalt, ipAddress);

            var user2 = new Users(userName, surname, "*****@*****.**", enryptPass, "Łódź", address, zipCode, true,
                passwordSalt, ipAddress);

            var user3 = new Users(userName, surname, "*****@*****.**", enryptPass, "Katowice", address, zipCode,
                isAdmin,
                passwordSalt, ipAddress);

            #endregion

            #region deliveryTypes

            var deliveryType = new DeliveryTypes("Poczta", Convert.ToDecimal(8.99));
            var deliveryType2 = new DeliveryTypes("Kurier", Convert.ToDecimal(12.00));
            var deliveryType3 = new DeliveryTypes("Obiór osobisty", Convert.ToDecimal(0.00));

            #endregion

            _catRepo.Save(category1);
            _catRepo.Save(category2);
            _catRepo.Save(category3);
            _catRepo.Save(category4);
            _catRepo.Save(category5);

            _orderStateRepository.Save(orderState1);
            _orderStateRepository.Save(orderState2);
            _orderStateRepository.Save(orderState3);

            _manuRepo.Save(manufacturer);
            _manuRepo.Save(manufacturer2);
            _manuRepo.Save(manufacturer3);
            _manuRepo.Save(manufacturer5);
            _manuRepo.Save(manufacturer4);
            _manuRepo.Save(manufacturer6);

            _userRepo.Save(user);
            _userRepo.Save(user2);
            _userRepo.Save(user3);
            foreach (var item in productList)
            {
                item.Description =
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sem mi, efficitur eget nisi vitae, facilisis efficitur massa. Sed rhoncus vestibulum velit, sit amet sodales nisl semper id. Praesent non nisi vitae orci facilisis dapibus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam auctor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sem mi, efficitur eget nisi vitae, facilisis efficitur massa. Sed rhoncus vestibulum velit, sit amet sodales nisl semper id. Praesent non nisi vitae orci facilisis dapibus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam auctor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sem mi, efficitur eget nisi vitae, facilisis efficitur massa. Sed rhoncus vestibulum velit, sit amet sodales nisl semper id. Praesent non nisi vitae orci facilisis dapibus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam auctor.";
                item.ShortDescription =
                    "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse sem mi, efficitur eget nisi vitae, facilisis efficitur massa. Sed rhoncus vestibulum velit, sit amet sodales nisl semper id. Praesent non nisi vitae orci facilisis dapibus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam auctor.";
                _productRepo.Save(item);
            }
            foreach (var productse in products)
                _productImagesRepository.Save(new ProductImages(filename, bytes, productse));

            _deliveryRepo.Save(deliveryType);
            _deliveryRepo.Save(deliveryType2);
            _deliveryRepo.Save(deliveryType3);

            Random rnd = new Random();
            for (int i = 0; i < 150; i++)
            {
                System.Threading.Thread.Sleep(10);
                var order1 = new Orders(user, DateTime.Now.AddDays(-rnd.Next(1, 365)),
                    Convert.ToDecimal(rnd.Next(50, 400)), deliveryType, orderState1);
                var order2 = new Orders(user2, DateTime.Now.AddDays(-rnd.Next(1, 365)),
                    Convert.ToDecimal(rnd.Next(50, 400)), deliveryType, orderState2);
                var order3 = new Orders(user3, DateTime.Now.AddDays(-rnd.Next(1, 365)),
                    Convert.ToDecimal(rnd.Next(50, 400)), deliveryType, orderState2);
                _ordersRepository.Save(order1);
                _ordersRepository.Save(order2);
                _ordersRepository.Save(order3);
                var random = rnd.Next(1, productList.Count);
                var orderDetails = new OrderDetails(order1, productList[random], rnd.Next(1, 5),
                    productList[random].Price);
                var orderDetails2 = new OrderDetails(order2, productList[random], rnd.Next(1, 5),
                    productList[random].Price);
                var orderDetails3 = new OrderDetails(order3, productList[random], rnd.Next(1, 5),
                    productList[random].Price);

                _orderDetailsRepository.Save(orderDetails);
                _orderDetailsRepository.Save(orderDetails2);
                _orderDetailsRepository.Save(orderDetails3);
            }

            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 GetCategory()
 {
     if (category.ID != 0)
     {
         var cat = categoryRepository.Get(category.ID);
         Assert.IsNotNull(cat);
     }
     else if (category2 != null && category2.ID != 0)
     {
         var cat = categoryRepository.Get(category2.ID);
         Assert.IsNotNull(cat);
     }
     else
     {
         var cat2= new Categories("TestCategory", "TestDescription");
         categoryRepository.Save(cat2);
         var cat = categoryRepository.Get(cat2.ID);
         Assert.IsNotNull(cat);
     }
 }
        public void UpdateCategory()
        {
            var category = categoryRepository.GetAll().FirstOrDefault(x => x.Name == "TestCategory");

            if (category != null)
            {
                category.Update("Test", "Description");
                categoryRepository.Update(category);
                Assert.AreEqual("Test", category.Name);
                Assert.AreEqual("Description", category.Description);
            }
            else
            {
                var category2 = new Categories("TestCategory", "");
                categoryRepository.Save(category2);
                category2.Update("Test","Description");
                Assert.AreEqual("Test", category2.Name);
                Assert.AreEqual("Description", category2.Description);
            }
        }
        public void SaveCategory()
        {
            var category = categoryRepository.GetAll().FirstOrDefault(x => x.Name == "TestCategory" || x.Name == "Test");

            if (category == null)
            {
                category2 = new Categories(
                 "TestCategory",
                 "TestDescription");
                Assert.AreEqual(0, category2.ID);

                categoryRepository.Save(category2);
                Assert.IsNotNull(category2);
                Assert.AreNotEqual(0, category2.ID);
            }
            else
            {
                Assert.IsNotNull(category);
                categoryRepository.Save(category);
                Assert.AreNotEqual(0, category.ID);
            }
        }
 public void Save(Categories entity)
 {
     _session.Save(entity);
 }
        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");
        }