public void Update(ProductImages entity)
 {
     using (_session.BeginTransaction())
     {
         try
         {
             _session.SaveOrUpdate(entity);
             _session.Transaction.Commit();
         }
         catch (Exception)
         {
            _session.Transaction.Rollback();
         }
     }
 }
        public ActionResult ChangeImage(int productId, HttpPostedFileBase photo)
        {
            var product = _productsRepository.Get(productId);

            if (product != null && photo != null)
            {
                var target = new MemoryStream();
                photo.InputStream.CopyTo(target);
                var data = target.ToArray();
                var productImage = new ProductImages(photo.FileName, data, product);
                _productImagesRepository.Save(productImage);
            }

            return RedirectToAction("Details", new {id = productId});
        }
        public void DeleteProductImage()
        {
            var product = productsRepository.GetAll().First();
            var productImages = new ProductImages(filename, bytes, product);

            productImagesRepository.Save(productImages);

            Assert.IsNotNull(product);
            Assert.IsNotNull(productImages);
            Assert.AreNotEqual(0,productImages.ID);

            productImagesRepository.Delete(productImages);

            var fromDb = productImagesRepository.Get(product.ID);

            Assert.IsNull(fromDb);
        }
        public ActionResult UploadImage(HttpPostedFileBase file, int productId)
        {
            if (file.ContentLength > 0)
            {
                if (file.ContentType.Contains("image"))
                {
                    var thePictureAsBytes = new byte[file.ContentLength];

                    using (var theReader = new BinaryReader(file.InputStream))
                    {
                        thePictureAsBytes = theReader.ReadBytes(file.ContentLength);
                    }

                    Products product = _productRepo.Get(productId);
                    if (product == null) throw new Exception("Produkt o podanym id nie istnieje");

                    var productImage = _productImagesRepository.GetImage(productId);
                    var newProductImage = new ProductImages(file.FileName, thePictureAsBytes, product);
                    if (productImage == null)
                    {
                        _productImagesRepository.Save(newProductImage);
                    }
                    else
                    {
                        _productImagesRepository.Delete(productImage);
                        _productImagesRepository.Save(newProductImage);
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Wybrano błędne rozszerzenie pliku. Dopuszczalne pliki graficzne: jpg, jpeg, png");
                    return RedirectToAction("Update", "Products", new {id = productId});
                }
            }
            else
            {
                ViewBag.Message = "Musisz wybrać plik.";
            }
            return RedirectToAction("Update", new {@id = productId});
        }
        public void UpdateProductWithImage()
        {
            string filename = @"C:\Projekty\SklepInternetowy\SklepInternetowy\FrontEnd\img\close.png";
            byte[] newbytes = System.IO.File.ReadAllBytes(filename);

            var productImage = productImagesRepository.GetAllImages().First();
            var product = productImage.Product;
            Assert.IsNotNull(productImage);
            Assert.IsNotNull(product);

            var productOldBytes = productImage.ImageBytes;

            var newProductImage = new ProductImages(filename, newbytes, product);
            productImagesRepository.Update(newProductImage);

            Assert.AreNotEqual(productOldBytes,newbytes);

            var getUpdate = productImagesRepository.Get(newProductImage.ID);
            Assert.IsNotNull(getUpdate);
            Assert.AreEqual(newbytes,getUpdate.ImageBytes);
        }
 public void Save(ProductImages entity)
 {
     _session.Save(entity);
 }
        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 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");
        }