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 void DropRole(Role role)
 {
     using (var context = new AuctionModelContainer())
     {
         context.Roles.Attach(role);
         context.Roles.Remove(role);
         context.SaveChanges();
     }
 }
        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 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();
            }
        }
        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 void RemoveRating(Rating rating)
        {
            using(var context = new AuctionModelContainer())
            {
                context.Ratings.Attach(rating);
                context.Ratings.Remove(rating);

                context.SaveChanges();
            }
        }
        public void RemoveRoleFromUser(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();
                context.Entry(role).Collection(r => r.Users).Load();

                if (!user.Roles.Contains(role))
                   throw new EntityDoesNotExistException("User with email "+user.Email+" hasn't the role with name "+role.Name+" so it can't be remove.");

                user.Roles.Remove(role);

                context.Entry(user).State = EntityState.Modified;
                context.SaveChanges();
            }
        }
        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 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();
         }
 }
        public void UpdateEmail(User user, String newEmail)
        {
            if (!user.Email.Equals(newEmail))
            {
                using (var context = new AuctionModelContainer())
                {
                    context.Users.Attach(user);
                    var entry = context.Entry(user);
                    entry.Property(u => u.Email).IsModified = true;

                    context.SaveChanges();
                }
            }
        }
        public void UpdateLastName(User user,String newLastName)
        {
            if (!user.LastName.Equals(newLastName))
            {
                user.LastName = newLastName;

                using (var context = new AuctionModelContainer())
                {
                    context.Users.Attach(user);
                    var entry = context.Entry(user);
                    entry.Property(u => u.LastName).IsModified = true;

                    context.SaveChanges();
                }
            }
        }
        public void UpdateRating(Rating rating)
        {
            using (var context = new AuctionModelContainer())
            {
                context.Ratings.Attach(rating);
                var entry = context.Entry(rating);
                entry.Property(r => r.Grade).IsModified = true;

                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);
                }
            }
        }