public void AddProduct(Product product)
        {
            using (var context = new AuctionModelContainer())
            {
                context.setLazyFalse();
                if (product.Categories.Count() == 0)
                {
                    throw new ValidationException("The product must have at least one category setted!");
                }

                ICollection<Product> products = this.GetProdctByNameAndDescription(product.Name, product.Description);
                foreach (Category category in product.Categories)
                {
                    foreach (Product p in products)
                    {
                        Product productAux = this.GetProdctById(p.IdProduct);
                        context.Products.Attach(productAux);
                        context.Entry(productAux).Collection(pAux => pAux.Categories).Load();
                        foreach (Category pcateg in productAux.Categories)
                            {
                                if (category.Name.Equals(pcateg.Name))
                                    throw new DuplicateException("The same product already exists - same name, description and category");
                            }
                    }
                }

                foreach (Category categ in product.Categories)
                    context.Categories.Attach(categ);
                context.Products.Add(product);
                context.SaveChanges();

            }
        }
        public void AddCategory(Category category)
        {
            using (var context = new AuctionModelContainer())
            {
                if (category.ParentCategory != null)
                    context.Categories.Attach(category.ParentCategory);
                if (category.ParentCategory != null)
                {
                    Category parent = this.GetCategoryById(category.ParentCategory.IdCategory);
                     if(parent == null)
                    {
                        throw new EntityDoesNotExistException("Parent does not exists!");
                    }
                }
                Category auxCateg = this.GetCategoryByName(category.Name);
                if (category.ParentCategory != null)
                {
                    if (this.exists(category.ParentCategory.IdCategory, category.Name))
                        throw new DuplicateException("You can not add two categories with the same name (" + category.Name + ").");
                }
                else
                {
                    if (this.verifyNameInRoots(category.Name))
                       throw new DuplicateException("You can not add two categories with the same name (" + category.Name + ").");
                }

                 context.Categories.Add(category);
                 context.SaveChanges();

                }
        }
        public void DeleteProduct(int id)
        {
            Product product = this.GetProdctById(id);
            if (product == null)
            {
                throw new EntityDoesNotExistException("Product does not exists!");
            }

            using (var context = new AuctionModelContainer())
            {
                context.Products.Attach(product);
                //if(product.Auction!=null)
                    //context.Entry(product).Collection(prod => prod.Auction.ProductActions).Load();
                if (product.Auction != null)
                {
                   // if (product.Auction.ProductActions.Count() > 0)
                    {
                        throw new DependencyException("The product has auctions. It cannot be deleted!");
                    }
                }
                context.Products.Attach(product);
                context.Products.Remove(product);
                context.SaveChanges();
            }
        }
 public void AddUser(User user)
 {
     using(var context = new AuctionModelContainer())
     {
         context.Users.Add(user);
         context.SaveChanges();
     }
 }
 public void AddRole(Role role)
 {
     using(var context = new AuctionModelContainer())
     {
         context.Roles.Add(role);
         context.SaveChanges();
     }
 }
 public Role GetRoleByName(String name)
 {
     using(var context = new AuctionModelContainer())
     {
         var roleVar = (from role in context.Roles where role.Name.Equals(name)
                           select role).FirstOrDefault();
         return roleVar;
     }
 }
 public void DropRole(Role role)
 {
     using (var context = new AuctionModelContainer())
     {
         context.Roles.Attach(role);
         context.Roles.Remove(role);
         context.SaveChanges();
     }
 }
 public Currency getCurrencyById(int id)
 {
     using (var context = new AuctionModelContainer())
     {
         var currencyVar = (from currency in context.Currencies
                         where currency.IdCurrency == id
                         select currency).FirstOrDefault();
         return currencyVar;
     }
 }
 public Currency GetCurrencyByName(String currencyName)
 {
     using (var context = new AuctionModelContainer())
     {
         var currencyVar = (from currency in context.Currencies
                         where currency.Name == currencyName
                         select currency).FirstOrDefault();
         return currencyVar;
     }
 }
        public ICollection<Category> GetAllCategoryForAProduct(Product product)
        {
            using(var context = new AuctionModelContainer())
            {
                context.Products.Attach(product);
                context.Entry(product).Collection(prod => prod.Categories).Load();

                return product.Categories;
            }
        }
        public ICollection<Product> GetAllProductsOfACategory(Category category)
        {
            using (var context = new AuctionModelContainer())
            {
                context.Categories.Attach(category);
                context.Entry(category).Collection(cat => cat.Products).Load();

                ICollection<Product> products = category.Products;
                return products;
            }
        }
        public void UpdateRole(Role role)
        {
            using (var context = new AuctionModelContainer())
               {
                context.Roles.Attach(role);
                var entry = context.Entry(role);
                entry.Property(r => r.Name).IsModified = true;

                context.SaveChanges();
               }
        }
        public int GetNumberOfActiveAuctionsStartedByUser(User user)
        {
            using(var context = new AuctionModelContainer())
            {
                int nr = (from auction in context.Auctions
                          where auction.User.Email.Equals(user.Email)
                          select auction).Count();

                return nr;
            }
        }
 public bool AddCurrency(String name)
 {
     Currency currency = new Currency();
     currency.Name = name;
     using (var context = new AuctionModelContainer())
     {
         context.Currencies.Add(currency);
         context.SaveChanges();
     }
     return true;
 }
        public void AddNewAuction(Auction auction)
        {
            using(var context=new AuctionModelContainer())
            {
                context.Users.Attach(auction.User);
                context.Currencies.Attach(auction.Currency);
                context.Products.Attach(auction.Product);
                context.Auctions.Add(auction);

                context.SaveChanges();
            }
        }
        public void AddRoleToUser(User user,Role role)
        {
            using(var context = new AuctionModelContainer())
            {
                context.setLazyFalse();

                context.Users.Attach(user);
                context.Roles.Attach(role);
                context.Entry(user).Collection(u => u.Roles).Load();
                user.Roles.Add(role);

                context.SaveChanges();
            }
        }
        private IDictionary<string, int> GetConfigurationFromDatabase()
        {
            IDictionary<string,int> configs = new Dictionary<string,int>();

            using(var context = new AuctionModelContainer())
            {
                var confVars = (from conf in context.Configurations
                               select conf).ToList();

                foreach (Configuration c in confVars)
                    configs.Add(c.Key, c.Value);
            }

            return configs;
        }
        public Auction GetAuctionById(int id)
        {
            using (var context = new AuctionModelContainer())
            {
                context.setLazyFalse();
                var auctionVar = (from auction in context.Auctions
                               where auction.IdAuction.Equals(id)
                               select auction).FirstOrDefault();

                if (auctionVar == null)
                    throw new EntityDoesNotExistException("Auction with id "+id+" does not exist");
                context.Auctions.Attach(auctionVar);
                //context.Entry(auctionVar).Collection(pAux => pAux.Categories).Load();
                context.Entry(auctionVar).Reference(pAux => pAux.Product).Load();
                return auctionVar;
            }
        }
        public void DropUser(String email)
        {
            User user = GetUserByEmail(email);
            if (user == null)
                throw new EntityDoesNotExistException("The user with email " + email + " does not exist.");

            using (var context = new AuctionModelContainer())
            {
                context.setLazyFalse();
                context.Users.Attach(user);
                context.Entry(user).Collection(u => u.Roles).Load();

                if (user.Roles.Count > 0)
                    throw new DependencyException("User with email " + email + " has roles asigned so it can't be droped.");

                context.Users.Remove(user);
                context.SaveChanges();
            }
        }
        public void DeleteCategory(int id)
        {
            Category category = this.GetCategoryById(id);
            if (category == null)
            {
                throw new EntityDoesNotExistException("Category does not exists!");
            }

            using (var context = new AuctionModelContainer())
            {

                context.Categories.Attach(category);
                context.Entry(category).Collection(categ => categ.Products).Load();
                if (category.Products.Count() > 0)
                {
                    throw new DependencyException("The category has products. It cannot be deleted!");
                }
                context.Categories.Attach(category);
                context.Categories.Remove(category);
                context.SaveChanges();
            }
        }
        public void AddRating(Rating rating)
        {
            using(var context = new AuctionModelContainer())
            {
                context.Users.Attach(rating.GivingNoteUser);
                context.Users.Attach(rating.ReceivingNoteUser);

                context.Ratings.Add(rating);
                try
                {
                    context.SaveChanges();
                }
                catch (DbEntityValidationException exc)
                {
                    String message = "";
                    IEnumerable<DbEntityValidationResult> errors = exc.EntityValidationErrors;
                    foreach (DbEntityValidationResult error in errors)
                        foreach (var validationError in error.ValidationErrors)
                            message = message + " " + validationError.PropertyName + ". " + validationError.ErrorMessage;
                    throw new ValidationException(message);
                }
            }
        }
        public ICollection<Role> GetAllRolesOfAnUser(User user)
        {
            using(var context = new AuctionModelContainer())
            {
                var roleVar = (from role in context.Roles
                               select role).ToList();

                for (int i = 0; i < roleVar.Count;i++ )
                {
                    ICollection<User> users = roleVar.ElementAt(i).Users;
                    bool ok = false;
                    foreach (User userFor in users)
                        if (userFor.Email.Equals(user.Email))
                            ok = true;
                    if (!ok)
                    {
                        roleVar.Remove(roleVar.ElementAt(i));
                        i--;
                    }
                }

                return roleVar;
            }
        }
        public Auction GetAuctionOfAProduct(Product product)
        {
            using (var context = new AuctionModelContainer())
            {
                var auctionVar = (from auction in context.Auctions
                                  join productSel in context.Products on auction.Product.IdProduct equals product.IdProduct
                                  where productSel.IdProduct == product.IdProduct
                                  select auction).FirstOrDefault();

                if (auctionVar != null)
                {
                    context.Auctions.Attach(auctionVar);
                    context.Entry(auctionVar).Reference(auc => auc.User).Load();
                }

                return auctionVar;
            }
        }
 public void UpdateCategoryDescription(Category category, String description)
 {
     category.Description = description;
     using (var context = new AuctionModelContainer())
     {
        context.Categories.Attach(category);
        var entry = context.Entry(category);
        entry.Property(r => r.Description).IsModified = true;
        context.SaveChanges();
     }
 }
 public void UpdateCategory(Category category, String newName)
 {
     Category auxCateg = this.GetCategoryByName(newName);
         if (category.IdParentCategory != null)
             category.ParentCategory = this.GetCategoryById((int)category.IdParentCategory);
         if (category.ParentCategory != null)
             if (this.exists(category.ParentCategory.IdCategory, newName))
                 throw new DuplicateException("You can not add two categories with the same name (" + newName + ").");
         {
             if (this.verifyNameInRoots(newName))
                 throw new DuplicateException("You can not add two categories with the same name (" + newName + ").");
         }
         category.Name = newName;
         using (var context = new AuctionModelContainer())
         {
             context.Categories.Attach(category);
             var entry = context.Entry(category);
             entry.Property(r => r.Name).IsModified = true;
             context.SaveChanges();
         }
 }
 private ICollection<Category> getRoots()
 {
     using (var context = new AuctionModelContainer())
     {
         context.setLazyFalse();
         return (from category in context.Categories
                         where category.IdParentCategory == null
                         select category).ToList();
     }
 }
        public void UpdateProductDescription(Product product, String description)
        {
            using (var context = new AuctionModelContainer())
                {
                    ICollection<Product> products = this.GetProdctByNameAndDescription(product.Name, description);
                    foreach (Category category in product.Categories)
                    {
                        foreach (Product p in products)
                        {
                            Product productAux = this.GetProdctById(p.IdProduct);
                            context.Products.Attach(productAux);
                            context.Entry(productAux).Collection(pAux => pAux.Categories).Load();
                            foreach (Category pcateg in productAux.Categories)
                            {
                                if (category.Name.Equals(pcateg.Name))
                                    throw new DuplicateException("The same product already exists - same name, description and category");
                            }
                        }
                    }

                    product.Description = description;
                    context.Products.Attach(product);
                    var entry = context.Entry(product);
                    entry.Property(r => r.Description).IsModified = true;
                    context.SaveChanges();
                }
        }
 public ICollection<Product> GetProdctByNameAndDescription(String name, String description)
 {
     using (var context = new AuctionModelContainer())
     {
         context.setLazyFalse();
         return context.Products.
             Where(product => product.Name.Equals(name) && product.Description.Equals(description)).
             ToList();
     }
 }
 public Product GetProdctById(int id)
 {
     using (var context = new AuctionModelContainer())
     {
         context.setLazyFalse();
         var prodVar = (from product in context.Products
                         where product.IdProduct == id
                         select product).FirstOrDefault();
         if (prodVar != null)
         {
             context.Products.Attach(prodVar);
             context.Entry(prodVar).Collection(pAux => pAux.Categories).Load();
             //context.Entry(auctionVar).Collection(pAux => pAux.Categories).Load();
             context.Entry(prodVar).Reference(pAux => pAux.Auction).Load();
             if (prodVar.Auction != null)
             {
                 context.Entry(prodVar.Auction).Collection(p => p.ProductActions).Load();
                 context.Entry(prodVar.Auction).Reference(c => c.Currency).Load();
                 context.Entry(prodVar.Auction).Reference(c => c.User).Load();
             }
         }
         return prodVar;
     }
 }
 public Category GetCategoryById(int id)
 {
     using(var context = new AuctionModelContainer())
     {
         context.setLazyFalse();
         var categVar = (from category in context.Categories where category.IdCategory == id
                         select category).FirstOrDefault();
         if (categVar != null)
         {
             context.Categories.Attach(categVar);
             context.Entry(categVar).Collection(categ => categ.Products).Load();
         }
         return categVar;
     }
 }