public int CreateProduct(CreateProductDto productDto) { CategoryProduct categoryProduct; if (productDto.IdCategoryProduct == 0) { var myCategoryProduct = _genericRepository.Filter <CategoryProduct> (p => p.Name == productDto.NameCategory.ToLower()).FirstOrDefault(); categoryProduct = myCategoryProduct == null ? CategoryProduct.Create(productDto.NameCategory) : myCategoryProduct; } else { categoryProduct = _genericRepository.Filter <CategoryProduct> (p => p.IdCategoryProduct == productDto.IdCategoryProduct).FirstOrDefault(); } var product = Product.Create(categoryProduct, productDto.NameProduct , productDto.Description, decimal.Parse(productDto.Price)); for (var image = 1; image <= productDto.QuantityImages; image++) { var imageProduct = ImageProduct.Create(image); product.ImageProducts.Add(imageProduct); } _genericRepository.Add(product); _genericRepository.SaveChanges(); return(product.IdProduct); }
public void Handle(AddProductCommand command) { if (!command.IsValid()) { RaiseValidationErrors(command); return; } var product = Product.Create( command.CategoryId, command.ProductName, command.Description, command.Price, command.ImageUrl ); if (product.CategoryId.HasValue) { var categoryProduct = CategoryProduct.Create( product.Id, product.CategoryId.Value, product ); categoriesRepository.AddCategoryProduct(categoryProduct); } productsRepository.Add(product); }
public int UpdateProduct(ProductDto productDto) { var product = _genericRepository.Filter <Product>(p => p.IdProduct == productDto.IdProduct) .Include(p => p.CategoryProduct) .FirstOrDefault(); CategoryProduct categoryProduct; if (productDto.IdCategoryProduct == 0) { var myCategoryProduct = _genericRepository.Filter <CategoryProduct> (p => p.Name == productDto.CategoryProductName.ToLower()).FirstOrDefault(); categoryProduct = myCategoryProduct == null ? CategoryProduct.Create(productDto.CategoryProductName) : myCategoryProduct; } else { categoryProduct = _genericRepository.Filter <CategoryProduct> (p => p.IdCategoryProduct == productDto.IdCategoryProduct).FirstOrDefault(); } product.Update(categoryProduct, productDto.ProductName, productDto.Description , decimal.Parse(productDto.Price)); _genericRepository.SaveChanges(); return(productDto.IdProduct); }