Esempio n. 1
0
        public void CreateProduct(ProductModelItem model)
        {
            Subcategory subcategory = _database.SubcategoryRepository.Get(model.SubcategoryId);

            if (_database.SubcategoryRepository.Get(model.SubcategoryId) != null)
            {
                Category parentCategory = subcategory.ParentCategory;
                Product  product        = _mapper.Map <Product>(model);

                _database.ProductRepository.Create(product);
                _database.Save();

                _database.ProductByCategoryRepository.Create(new ProductByCategory {
                    Product = product, Subcategory = subcategory, Category = parentCategory
                });

                Brand manufacturer;
                var   foundManufacturers = _database.BrandRepository.Find(item => item.Name.ToLower() == model.ManufacturerName.ToLower());
                if (foundManufacturers.Count() == 0)
                {
                    manufacturer = new Brand()
                    {
                        Name = model.ManufacturerName
                    };
                    _database.BrandRepository.Create(manufacturer);
                    _database.Save();
                }
                else
                {
                    manufacturer = foundManufacturers.First();
                }

                _database.ProductByBrandRepository.Create(new ProductByBrand()
                {
                    Brand = manufacturer, Product = product
                });
                _database.Save();
            }
            else
            {
                throw new Exception("Category is not found");
            }
        }
Esempio n. 2
0
 public IActionResult AddProduct([FromBody] ProductModelItem item)
 {
     _productSrvice.CreateProduct(item);
     return(Ok());
 }