public void Create(MainImageViewModel model)
        {
            foreach (var element in GetAllPerProductId(model.ProductId))
            {
                if (model.MainPicture)
                { element.MainPicture = false; }
                else
                { break; }
            }

            Image picture = new Image
            {
            ImageId= model.ImageId,
            ProductId = model.ProductId,
            FileName = model.FileName,
            Picture = new byte [model.Image.ContentLength],
            ImageMineType = model.Image.ContentType,
            MainPicture = model.MainPicture
            };

            using (MemoryStream memStream = new MemoryStream(model.Image.ContentLength))
            {
                // model.Image.InputStream.Read(picture.Picture,0, model.Image.ContentLength);
                model.Image.InputStream.CopyTo(memStream, model.Image.ContentLength);
                picture.Picture = memStream.ToArray();
            }
            using (var context = new WebShopMVCContext())
            {
                 context.Images.Add(picture);
                 context.SaveChanges();
            }
        }
        public ProductViewModel GetModelById(int id)
        {
            ProductViewModel model;

            using (var context = new WebShopMVCContext())
            {
                var product = context.Products.Find(id);
                model = new ProductViewModel
                {
                    ProductId = product.ProductId,
                    ProductName = product.ProductName,
                    SubcategoryId = product.SubcategoryId,
                    Price = product.Price,
                    Discount = product.Discount,
                    Description = product.Description,
                    Subcategories = context.Subcategories.Select(c => new PartViewSubcategoriesForProduct
                    {
                        SubcategoryName = c.SubcategoryName,
                        SubcategoryId = c.SubcategoryId
                    }).ToList(),
                    Images = context.Images.Select(f => new PartImageViewModel
                    {
                        MainPicture = f.MainPicture,
                        FileName = f.FileName,
                        ImageId = f.ImageId,
                        ProductId = f.ProductId
                    }).Where(p=>p.ProductId==id).ToList()

                };
            }
            return model;
        }
 public void Delete(int id)
 {
     using (var context = new WebShopMVCContext())
     {
         context.Clients.Remove(
             context.Clients.Where(m => m.UserId == id).FirstOrDefault());
         context.SaveChanges();
     }
 }
 public void RemoveUnit(int productId)
 {
     using (var context = new WebShopMVCContext())
     {
         var product = context.CartItems.Find(productId);
         context.CartItems.Remove(product);
         context.SaveChanges();
     }
 }
 public void Delete(int id)
 {
     using (var context = new WebShopMVCContext())
     {
         var product = context.Products.Find(id);
         context.Products.Remove(product);
         context.SaveChanges();
     }
 }
 public void Delete(int id)
 {
     using (var context = new WebShopMVCContext())
     {
         var picture = context.Images.Find(id);
         context.Images.Remove(picture);
         context.SaveChanges();
     }
 }
        public void Delete(int id)
        {
            using (var context = new WebShopMVCContext())
            {

                var category = context.Categories.Find(id);
                context.Categories.Remove(category);
                context.SaveChanges();
            }
        }
        public void Create(CategoryViewModel model)
        {
            Category category = new Category
            {
                CategoryName = model.CategoryName
            };

            using (var context = new WebShopMVCContext())
            {
                context.Categories.Add(category);
                context.SaveChanges();
            }
        }
 public IEnumerable<CategoryViewModel> GetAll()
 {
     var list = new List<CategoryViewModel>();
     using (var context = new WebShopMVCContext())
     {
         list = context.Categories.Select(m => new CategoryViewModel
         {
             CategoryId = m.CategoryId,
             CategoryName = m.CategoryName
         }).ToList();
     }
     return list;
 }
 public void Create(SubcategoryViewModel model)
 {
     Subcategory subcategory = new Subcategory
     {
         CategoryId = model.CategoryId,
         SubcategoryName = model.SubcategoryName
     };
     using (var context = new WebShopMVCContext())
     {
         context.Subcategories.Add(subcategory);
         context.SaveChanges();
     }
 }
 public void AddItem(int clientId, int productId, int quantity)
 {
     CartItem item = new CartItem
     {
         ProductId = productId,
         ClientId = clientId,
         Quantity = quantity
     };
     using (var context = new WebShopMVCContext())
     {
         context.CartItems.Add(item);
         context.SaveChanges();
     }
 }
        public CategoryViewModel GetModelById(int id)
        {
            CategoryViewModel model;

            using (var context = new WebShopMVCContext())
            {
                var category = context.Categories.Find(id);
                model = new CategoryViewModel
                {
                    CategoryId = category.CategoryId,
                    CategoryName = category.CategoryName
                };
            }
            return model;
        }
 public IEnumerable<SubcategoryViewModel> GetAllById(int categoryId)
 {
     var list = new List<SubcategoryViewModel>();
     using (var context = new WebShopMVCContext())
     {
         list = context.Subcategories.Include("Categories").Where(c => c.CategoryId == categoryId).Select(m => new SubcategoryViewModel
         {
             CategoryId = m.CategoryId,
             SubcategoryId = m.SubcategoryId,
             SubcategoryName = m.SubcategoryName,
             CategoryName = m.Category.CategoryName
         }).ToList();
     }
     return list;
 }
 public IEnumerable<PartImageViewModel> GetAllPerProductId(int id)
 {
     var list = new List<PartImageViewModel>();
     using (var context = new WebShopMVCContext())
     {
         list = context.Images.Select(m => new PartImageViewModel
         {
             ImageId = m.ImageId,
             ProductId = m.ProductId,
             MainPicture = m.MainPicture,
             FileName = m.FileName
         }).Where(m => m.ProductId==id).ToList();
     }
     return list;
 }
        public FileContentResult GetImage(int imageId)
        {
            using (var context = new WebShopMVCContext())
            {
                var image = context.Images.FirstOrDefault(i => i.ImageId == imageId);

                if (image != null)
                {
                    return File(image.Picture, image.ImageMineType);
                }
                else
                {
                    return null;
                }
            }
        }
        public IEnumerable<CartItemViewModel> GetCartFromSession()
        {
            HttpContext context = HttpContext.Current;
            if (context.Session["Cart"] == null)
            {
                return null;
            }
            else
            {
                var cart = (List<PartCartItemViewModel>)(context.Session["Cart"]);
                List<CartItemViewModel> list = new List<CartItemViewModel>();
                List<ProductViewModel> products = new List<ProductViewModel>();
                foreach (var item in cart)
                {
                    using (var contextBd = new WebShopMVCContext())
                    {
                        ProductViewModel product = contextBd.Products.Select(p => new ProductViewModel
                        {
                            ProductId = p.ProductId,
                            ProductName = p.ProductName,
                            SubcategoryId = p.SubcategoryId,
                            Price = p.Price,
                            Discount = p.Discount,
                            Description = p.Description,
                        }).Where(p => p.ProductId == item.ProductId).SingleOrDefault();

                        products.Add(product);
                    }

                    foreach (var product in products)
                    {
                        if (product.ProductId == item.ProductId)
                        {
                            CartItemViewModel cartitem = new CartItemViewModel
                            {
                                ProductId = product.ProductId,
                                Product = product,
                                Quantity = item.Quantity
                            };
                            list.Add(cartitem);
                        }
                    }
                }

                return list;
            }
        }
 public IEnumerable<ClientViewModel> GetAll()
 {
     var model = new List<ClientViewModel>();
     using (var context = new WebShopMVCContext())
     {
         model = context.Clients.Select(m => new ClientViewModel
         {
             FirstName = m.FirstName,
             LastName = m.LastName,
             Login = m.Login,
             Password = m.Password,
             Email = m.Email,
             Phone = m.Phone
         }).ToList();
     }
     return model;
 }
 public IEnumerable<ProductViewModel> GetAll()
 {
     var list = new List<ProductViewModel>();
     using (var context = new WebShopMVCContext())
     {
         list = context.Products.Select(m => new ProductViewModel
         {
             ProductId = m.ProductId,
             ProductName = m.ProductName,
             SubcategoryId = m.SubcategoryId,
             Price = m.Price,
             Discount = m.Discount,
             Description = m.Description
         }).ToList();
     }
     return list;
 }
 public void Create(ProductViewModel model)
 {
     Product product = new Product
     {
         ProductId = model.ProductId,
         ProductName = model.ProductName,
         SubcategoryId = model.SubcategoryId,
         Price = model.Price,
         Discount = model.Discount,
         Description = model.Description
     };
     using (var context = new WebShopMVCContext())
     {
         context.Products.Add(product);
         context.SaveChanges();
     }
 }
 public void ClearCart()
 {
     var list = new List<CartItem>();
     using (var context = new WebShopMVCContext())
     {
         list = context.CartItems.Select(m => new CartItem
         {
             CartItemId = m.CartItemId,
             ClientId = m.ClientId,
             Client = m.Client,
             ProductId = m.ProductId,
             Product = m.Product,
             Quantity = m.Quantity
         }).ToList();
         context.CartItems.RemoveRange(list);
         context.SaveChanges();
     }
 }
        public IEnumerable<CompositeCategoryViewModel> GetAllWithSubcategory()
        {
            using (var context = new WebShopMVCContext())
             {
                 //context.Database.Log = message => Trace.Write(message);

                 return context.Categories.Select(c => new CompositeCategoryViewModel
                 {
                     CategoryId = c.CategoryId,
                     CategoryName = c.CategoryName,
                     Subcategories = context.Subcategories.Where(s => s.CategoryId == c.CategoryId).Select(s => new SubcategoryViewModel
                     {
                         SubcategoryId = s.SubcategoryId,
                         SubcategoryName = s.SubcategoryName
                     })
                 }).ToList();
             }
        }
        public void Create(ClientViewModel model)
        {
            Client client = new Client
            {
                FirstName = model.FirstName,
                LastName = model.LastName,
                Login = model.Login,
                Password = model.Password,   //.GetHashCode().ToString(),
                Email = model.Email,
                Phone = model.Phone
            };

            using (var context = new WebShopMVCContext())
            {
                context.Clients.Add(client);
                context.SaveChanges();
            }
        }
 public ClientViewModel GetModelById(int id)
 {
     ClientViewModel model;
     using (var context = new WebShopMVCContext())
     {
         model = context.Clients.Where(m => m.UserId == id)
             .Select(m => new ClientViewModel
             {
                 UserId = m.UserId,
                 Login = m.Login,
                 Email = m.Email,
                 FirstName = m.FirstName,
                 LastName = m.LastName,
                 Phone = m.Phone,
                 RowVersion = m.RowVersion
             }).SingleOrDefault();
     }
     return model;
 }
        public CompositeSubcategoryViewModel GetModelById(int id)
        {
            CompositeSubcategoryViewModel model;

            using (var context = new WebShopMVCContext())
            {
                var subcategory = context.Subcategories.Find(id);
                model = new CompositeSubcategoryViewModel
                {
                    CategoryId = subcategory.CategoryId,
                    SubcategoryId = subcategory.SubcategoryId,
                    SubcategoryName = subcategory.SubcategoryName,
                    Categories = context.Categories.Select(c => new CategoryViewModel
                        { CategoryName = c.CategoryName, CategoryId = c.CategoryId }).ToList()
                };

            }
            return model;
        }
 public ClientViewModel GetClientByLoginAndPassword(string login, string password)
 {
     ClientViewModel model;
     using (var context = new WebShopMVCContext())
     {
         model = context.Clients.Where(m => m.Login == login && m.Password == password)
             .Select(m => new ClientViewModel
             {
                 UserId = m.UserId,
                 Login = m.Login,
                 Email = m.Email,
                 FirstName = m.FirstName,
                 LastName = m.LastName,
                 Phone = m.Phone,
                 RowVersion = m.RowVersion
             }).SingleOrDefault();
     }
     return model;
 }
        public EmployeeViewModel GetEmployeeById(int id)
        {
            EmployeeViewModel model = new EmployeeViewModel();
            using (var context = new WebShopMVCContext())
            {
                var employee = context.Employees.Find(id);

                model.UserId = id;
                employee.FirstName = model.FirstName;
                model.LastName = employee.LastName;
                model.Address = employee.Address;
                model.Email = employee.Email;
                model.Phone = employee.Phone;
                model.Login = employee.Login;
                model.Password = employee.Password;
                model.Role = employee.Role;
                model.IsBlocked = employee.IsBlocked;
                model.IsDelete = employee.IsDelete;
                model.RowVersion = employee.RowVersion;
            }
            return model;
        }
        public void Create(EmployeeViewModel model)
        {
            Employee employee = new Employee
            {
                FirstName = model.FirstName,
                LastName = model.LastName,
                Login = model.Login,
                Password = model.Password,
                Address = model.Address,
                Email = model.Email,
                Phone = model.Phone,
                Role = model.Role,
                IsBlocked = model.IsBlocked,
                IsDelete = model.IsDelete,
                RowVersion = model.RowVersion
            };

            using (var context = new WebShopMVCContext())
            {
                context.Employees.Add(employee);
                context.SaveChanges();
            }
        }
        public IEnumerable<EmployeeViewModel> GetAllEmployee()
        {
            var model = new List<EmployeeViewModel>();

            using (var context = new WebShopMVCContext())
            {
                model = context.Employees.Select(m => new EmployeeViewModel
                {
                    UserId = m.UserId,
                    FirstName = m.FirstName,
                    LastName = m.LastName,
                    Address = m.Address,
                    Email = m.Email,
                    Phone = m.Phone,
                    Login = m.Login,
                    Password = m.Password,
                    Role = m.Role,
                    IsBlocked = m.IsBlocked,
                    IsDelete = m.IsDelete,
                    RowVersion = m.RowVersion
                }).ToList();
            }
            return model;
        }
 public IEnumerable<CartItemViewModel> GetAllCartItem()
 {
     var list = new List<CartItemViewModel>();
     using (var context = new WebShopMVCContext())
     {
         list = context.CartItems.Select(m => new CartItemViewModel
         {
             CartItemId = m.CartItemId,
             ClientId = m.ClientId,
             ProductId = m.ProductId,
             Product = context.Products.Select(p => new ProductViewModel//??
             {
                 ProductId = p.ProductId,
                 ProductName = p.ProductName,
                 SubcategoryId = p.SubcategoryId,
                 Price = p.Price,
                 Discount = p.Discount,
                 Description = p.Description,
             }).Where(p => p.ProductId == m.ProductId).SingleOrDefault(),
             Quantity = m.Quantity
         }).ToList();
     }
     return list;
 }
        public ProductViewModel GetNewProductViewModelWithSubcategory()
        {
            ProductViewModel model = new ProductViewModel();

            using (var context = new WebShopMVCContext())
            {
                model.Subcategories = context.Subcategories.Select(c => new PartViewSubcategoriesForProduct
                {
                    SubcategoryName = c.SubcategoryName,
                    SubcategoryId = c.SubcategoryId
                }).ToList();

            }
            return model;
        }