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

                }
        }
 internal static void Validate(Category category, ValidationResults results)
 {
     if ( category.Name.Length < 3 || category.Name.Length > 30 || category.Description.Length > 200)//some business-logic derived condition
     {
         results.AddResult
             (
                 new ValidationResult("Invalid category's name/description.", category, "ValidateMethod", "error", null)
             );
     }
 }
        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 bool AddCategory(Category category)
 {
     logger.logInfo("Try to add a new category in db.");
     if (category == null)
     {
         ValidationException e = new ValidationException("Category is null");
         logger.logError(e);
         throw e;
     }
     var validationResults = Validation.Validate<Category>(category);
     if (category.Name != null)
     {
         if (!validationResults.IsValid)
         {
             ValidationException e = new ValidationException("Invalid category's name/description.");
             logger.logError(e);
            foreach (ValidationResult vr in validationResults)
                 Console.WriteLine(vr.Message);
             throw e;
         }
         else
         {
             try
             {
                 DataMapperFactoryMethod.GetCurrentFactory().CategoryFactory.AddCategory(category);
             }
              catch (DuplicateException duplicateException)
              {
                  logger.logError(duplicateException);
                  throw duplicateException;
              }
              catch (EntityDoesNotExistException entityDoesNotExist)
              {
                  logger.logError(entityDoesNotExist);
                  throw entityDoesNotExist;
              }
              logger.logInfo("Category added successfully!");
         }
     }
     else
     {
         ValidationException e = new ValidationException("Invalid category's name - null.");
         logger.logError(e);
         throw e;
     }
     return true;
 }
        public void TestAddLicitation()
        {
            User user1 = new User();
            user1.FirstName = "AAA";
            user1.LastName = "AAA";
            user1.Email = "[email protected]";

            User user2 = new User();
            user2.FirstName = "BBB";
            user2.LastName = "BBB";
            user2.Email = "[email protected]";

            Role role1 = new Role();
            role1.Name = Constants.OWNER;
            Role role2 = new Role();
            role2.Name = Constants.ACTIONEER;

            Category category = new Category();
            category.Name = "category";
            category.Description = "AAAAA";
            categoryService.AddCategory(category);

            Product product = new Product();
            product.Name = "AAA";
            product.Description = "AAAAAAAA";
            product.Categories.Add(category);

            userService.AddUser(user1);
            userService.AddUser(user2);

            productService.AddProduct(product);
            currencyService.AddCurrency("RON");
            Currency currency = currencyService.GetCurrencyByName("RON");
            roleService.AddRole(role1);
            roleService.AddRole(role2);
            userService.AddRoleToUser("[email protected]", role1);
            userService.AddRoleToUser("[email protected]", role2);

            auctionService.AddNewAuction(user1, product, currency, 100, DateTime.Now, DateTime.Now);
            productAuctionService.AddProductAuction(user2, product, 200, currency);
            userService.AddNoteToUser(user2, user1, 7);
        }
 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 ICollection<Product> GetAllProductsOfACategory(Category category)
 {
     return DataMapperFactoryMethod.GetCurrentFactory().ProductFactory.GetAllProductsOfACategory(category);
 }
        public void AddProductWithInexistentCategory()
        {
            Product product = new Product();
            product.Name = "prod";
            product.Description = "";
            Category category = new Category();
            product.Categories.Add(category);

            ProductService productService = new ProductService();
            Boolean result = false;
            try
            {
                result = productService.AddProduct(product);
            }
            catch (EntityDoesNotExistException exc)
            {
                Assert.AreEqual("One or more categories of the product do not exist", exc.Message);
            }
            Assert.AreEqual(result, false);
        }