static void Main(string[] args) { List <Product> ProductList = new List <Product>(); using (var context = new ProductContext()) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); //List<Product> ProductList = new List<Product>(); for (int i = 1; i <= 10; i++) { Product newProduct = new Product { Name = "ProductName" + i, Price = 10 }; ProductList.Add(newProduct); } context.AddRange(ProductList); context.SaveChanges(); } var html = File.ReadAllText("C:/Users/konuk/source/repos/ScribanQuete2ConsoleApp/ScribanQuete2ConsoleApp/View/HTMLPage1.html"); var template = Template.Parse(html); var result = template.Render(new { Products = ProductList }); ProductController.Write(result); }
public static string ImportCategories(ProductContext context, string jsonString) { var sb = new StringBuilder(); var deserialisedCategories = JsonConvert.DeserializeObject <CategoryDto[]>(jsonString); var validCategories = new List <Category>(); foreach (var obj in deserialisedCategories) { if (!IsValid(obj)) { sb.AppendLine(FailureMessage); continue; } var category = new Category { Name = obj.Name }; validCategories.Add(category); sb.AppendLine(String.Format(SuccessMessage, obj.Name)); } context.AddRange(validCategories); context.SaveChanges(); return(sb.ToString().Trim()); }
public static void ImportProductsCateogories(ProductContext context) { var products = context.Products.ToArray(); var categories = context.Categories.ToArray(); var validCategoryProducts = new List <CategoryProduct>(); var rnd = new Random(); foreach (var product in products) { int count = 0; int[] indexes = new int[5]; while (count < 4) { int index = rnd.Next(0, categories.Length - 1); if (!indexes.Contains(index)) { indexes[count] = index; var categoryProduct = new CategoryProduct { Product = product, Category = categories[index] }; validCategoryProducts.Add(categoryProduct); count++; } } } context.AddRange(validCategoryProducts); context.SaveChanges(); }
public static string ImportProducts(ProductContext context, string xmlString) { var serializer = new XmlSerializer(typeof(ProductDto[]), new XmlRootAttribute("products")); var deserializedProducts = (ProductDto[])serializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(xmlString))); var sb = new StringBuilder(); var users = context.Users.ToArray(); var rnd = new Random(); var validProducts = new List <Product>(); foreach (var obj in deserializedProducts) { if (!IsValid(obj)) { sb.AppendLine(FailureMessage); continue; } var sellerIndex = rnd.Next(0, users.Length - 1); int?buyerIndex; while (true) { buyerIndex = rnd.Next(0, users.Length - 1); if (sellerIndex != buyerIndex) { var product = new Product() { Name = obj.Name, Price = obj.Price, Seller = users[sellerIndex], Buyer = users[buyerIndex.Value], }; if ((buyerIndex * sellerIndex) % 5 == 2) { product.Buyer = null; } validProducts.Add(product); sb.AppendLine(String.Format(SuccessMessage, obj.Name)); break; } } } context.AddRange(validProducts); context.SaveChanges(); return(sb.ToString().Trim()); }
public DatabaseFixture() { _dbOptions = new DbContextOptionsBuilder <ProductContext>() .UseInMemoryDatabase(databaseName: "in-memory") .Options; DbContext = new ProductContext(_dbOptions); DbContext.AddRange(GetFakeProduct()); DbContext.SaveChanges(); }
private ProductContext SetupDb() { var options = new DbContextOptionsBuilder <ProductContext>() .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()) .Options; var context = new ProductContext(options); context.AddRange(productList); context.SaveChanges(); return(context); }
public static string ImportProducts(ProductContext context, string jsonString) { var sb = new StringBuilder(); var users = context.Users.ToArray(); var rnd = new Random(); var deserialisedProducts = JsonConvert.DeserializeObject <ProductDto[]>(jsonString); var validProducts = new List <Product>(); foreach (var obj in deserialisedProducts) { if (!IsValid(obj)) { sb.AppendLine(FailureMessage); continue; } int sellerIndex = rnd.Next(0, users.Length - 1); int?buyerIndex; while (true) { buyerIndex = rnd.Next(0, users.Length - 1); if (buyerIndex != sellerIndex) { var product = new Product { Name = obj.Name, Price = obj.Price, Buyer = users[buyerIndex.Value], Seller = users[sellerIndex] }; if (buyerIndex % (sellerIndex + 1) == 2) { product.Buyer = null; } validProducts.Add(product); break; } } } context.AddRange(validProducts); context.SaveChanges(); return(sb.ToString().Trim()); }
public ProductControllerTest() { _dbOptions = new DbContextOptionsBuilder <ProductContext>() .UseInMemoryDatabase( databaseName: "inmemory" ).Options; using (var dbContext = new ProductContext( _dbOptions )) { dbContext.AddRange(GetTestProducts()); dbContext.SaveChanges(); } }
public async Task SeedAsync(ProductContext context, IHostingEnvironment env, IOptions <ProductSettings> settings) { var policy = CreatePolicy(nameof(ProductContextSeed)); await policy.ExecuteAsync(async() => { using (context) { context.Database.Migrate(); if (!context.Products.Any()) { context.AddRange(GetPredifinedProducts()); await context.SaveChangesAsync(); } await context.SaveChangesAsync(); } }); }
public static string ImportUsers(ProductContext context, string xmlString) { var sb = new StringBuilder(); var serializer = new XmlSerializer(typeof(UserDto[]), new XmlRootAttribute("users")); var deserializedUsers = (UserDto[])serializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(xmlString))); var validUsers = new List <User>(); foreach (var obj in deserializedUsers) { if (!IsValid(obj)) { sb.AppendLine(FailureMessage); continue; } var user = new User { FirstName = obj.FirstName, LastName = obj.LastName, Age = obj.Age }; validUsers.Add(user); sb.AppendLine(String.Format(SuccessUserMessage, obj.FirstName, obj.LastName)); } context.AddRange(validUsers); context.SaveChanges(); return(sb.ToString()); }
public static string ImportUsers(ProductContext context, string jsonString) { var sb = new StringBuilder(); var deserialisedUsers = JsonConvert.DeserializeObject <UserDto[]>(jsonString); var validUsers = new List <User>(); foreach (var obj in deserialisedUsers) { if (!IsValid(obj)) { sb.AppendLine(FailureMessage); continue; } var user = new User { FirstName = obj.FirstName, LastName = obj.LastName, Age = obj.Age.Value }; validUsers.Add(user); sb.AppendLine(String.Format(SuccessUserMessage, obj.FirstName, obj.LastName)); } context.AddRange(validUsers); context.SaveChanges(); return(sb.ToString()); }
public void AddInventory(List <Inventory> inventorys) { _myContext.AddRange(inventorys); }