public bool AddProduct(Product product) { logger.logInfo("Try to add a new product in db."); if (product == null) { ValidationException e = new ValidationException("Product is null"); logger.logError(e); throw e; } if (product.Categories == null) { ValidationException e = new ValidationException("Product's categories are null"); logger.logError(e); throw e; } CategoryService categoryService = new CategoryService(); foreach (Category categ in product.Categories) { if (categoryService.GetCategoryById(categ.IdCategory) == null) { EntityDoesNotExistException e = new EntityDoesNotExistException("One or more categories of the product do not exist"); logger.logError(e); throw e; } } var validationResults = Validation.Validate<Product>(product); if (product.Name != null) { if (!validationResults.IsValid) { ValidationException e = new ValidationException("Invalid product's name/description."); logger.logError(e); throw e; } else { try { DataMapperFactoryMethod.GetCurrentFactory().ProductFactory.AddProduct(product); } catch (DuplicateException duplicateException) { logger.logError(duplicateException); throw duplicateException; } } } else { ValidationException e = new ValidationException("Invalid product's name - null."); logger.logError(e); throw e; } logger.logInfo("Product added successfully!"); return true; }
public void AddProductExistent() { Product product = new Product(); product.Name = "thr"; product.Description = ""; CategoryService categoryService = new CategoryService(); Category category = categoryService.GetCategoryById(2); product.Categories.Add(category); ProductService productService = new ProductService(); Boolean result = false; try { result = productService.AddProduct(product); } catch (DuplicateException exc) { Assert.AreEqual("The same product already exists - same name, description and category", exc.Message); } Assert.AreEqual(result, false); }
private void CheckProduct(Product product,User user) { ICategoryService categoryService = new CategoryService(); IProductService productService = new ProductService(); IConfiguration configuration = ConfigurationService.GetInstance(); ICollection<Category> categorys = categoryService.GetCategorysForAProduct(product); int nrOfAuctions = 0; foreach(Category category in categorys) { ICollection<Category> parentCAtegorys = categoryService.getParents(category.IdCategory); ICollection<Product> products; foreach(Category parentCategory in parentCAtegorys) { products = productService.GetAllProductsOfACategory(category); foreach(Product productCat in products) { Auction auction = productService.GetAuctionOfAProduct(productCat); if (auction != null && auction.User.Equals(user)) nrOfAuctions++; } } } if(nrOfAuctions >= configuration.GetValue(Constants.MAX_NR_OF_AUCTION_ASSOCIATE_WITH_CATEGORY)) throw new AuctionException("The auction can not be added because the number of auctions per category was exceeded"); }
public void UpdateProductDuplicateName() { Product product = new Product(); product.Name = "Product1"; product.Description = "Description"; CategoryService categoryService = new CategoryService(); Category category = categoryService.GetCategoryById(8); product.Categories.Add(category); ProductService productService = new ProductService(); productService.AddProduct(product); String name = "Product"; int id = 19; Product p = productService.GetProductById(id); Boolean result = false; try { result = productService.UpdateProduct(id, name); } catch (DuplicateException exc) { Assert.AreEqual("The same product already exists - same name, description and category", exc.Message); } Assert.AreEqual(false, result); }
public void GetProductsOfACategory() { CategoryService categoryService = new CategoryService(); ProductService productService = new ProductService(); Category category = categoryService.GetCategoryById(8); ICollection<Product> products = productService.GetAllProductsOfACategory(category); Assert.AreEqual(products.Count(), 2); }
public void AddProductZeroCharactesDescription() { Product product = new Product(); product.Name = "Product"; product.Description = ""; CategoryService categoryService = new CategoryService(); Category category = categoryService.GetCategoryById(2); product.Categories.Add(category); ProductService productService = new ProductService(); Boolean result = false; try { result = productService.AddProduct(product); } catch (ValidationException exc) { Assert.AreEqual("", exc.Message); } Assert.AreEqual(result, true); }