/// <summary> /// Create a data /// </summary> /// <param name="serviceProvider"></param> public static void CreateData(IServiceProvider serviceProvider) { using (var context = new BelezaWebContext(serviceProvider.GetRequiredService <DbContextOptions <BelezaWebContext> >())) { // Look for any Products if (context.Products.Any()) { return; // Data was already exists } var product = new Models.Product { Name = "L'Oréal Professionnel Expert Absolut Repair Cortex Lipidium - Máscara de Reconstrução 500g", Sku = 43264 }; product.Warehouses.Add(new Models.Warehouse { Locality = "SP", Quantity = 12, Type = Common.WarehouseType.ECOMMERCE.ToString() }); product.Warehouses.Add(new Models.Warehouse { Locality = "MOEMA", Quantity = 3, Type = Common.WarehouseType.PHYSICAL_STORE.ToString() }); context.Products.Add(product); context.SaveChanges(); } }
/// <summary> /// Build a Product /// </summary> /// <returns></returns> public static Product BuildProduct(BelezaWebContext db, out int idItem, bool saveData = true) { idItem = 0; var product = new Product { Sku = 909090, Name = "Test Product" }; product.Warehouses.Add(new Models.Warehouse { Locality = "SP", Quantity = 250, Type = Common.WarehouseType.ECOMMERCE.ToString() }); product.Warehouses.Add(new Models.Warehouse { Locality = "MOEMA", Quantity = 255, Type = Common.WarehouseType.PHYSICAL_STORE.ToString() }); if (saveData) { db.Products.Add(product); db.SaveChanges(); idItem = product.Id; } return(product); }
/// <summary> /// Create a product /// </summary> /// <param name="product"></param> /// <returns></returns> public async Task <ProductDTO> CreateProduct(ProductDTO product) { try { //Configure a map var config = GetMapperConfigurationToProduct(); //Create a Imapper IMapper iMapper = config.CreateMapper(); var productToSave = iMapper.Map <ProductDTO, Product>(product); _context.Products.Add(productToSave); _context.SaveChanges(); var itemResult = GetProduct(productToSave.Sku).Result; return(await Task.FromResult <ProductDTO>(itemResult)); } catch (Exception ex) { throw ex; } }