//01.Import Users public static string ImportUsers(ProductShopContext context, string inputXml) { //Because of Judge InitializeMapper(); const string root = "Users"; XmlSerializer xmlSerializer = new XmlSerializer(typeof(ImportUserModel[]), new XmlRootAttribute(root)); var textReader = new StringReader(inputXml); var usersDto = xmlSerializer .Deserialize(textReader) as ImportUserModel[]; var mapUser = Mapper.Map <User[]>(usersDto); context.Users.AddRange(mapUser); context.SaveChanges(); return($"Successfully imported {mapUser.Count()}"); }
//Query 5. Import Categories and Products public static string ImportCategoryProducts(ProductShopContext context, string inputJson) { //string inputJson = File.ReadAllText("../../../Datasets/categories-products.json"); //string result = ImportCategoryProducts (db, inputJson); //Console.WriteLine(result); JsonSerializerSettings settings = new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }; CategoryProduct[] categoryProducts = JsonConvert.DeserializeObject <CategoryProduct[]>(inputJson, settings); context.CategoryProducts.AddRange(categoryProducts); context.SaveChanges(); return($"Successfully imported {categoryProducts.Count()}"); }
public static string ImportUsers(ProductShopContext context, string inputXml) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(ImportUserDto[]), new XmlRootAttribute("Users")); var usersDto = (ImportUserDto[])xmlSerializer.Deserialize(new StringReader(inputXml)); var users = new List <User>(); foreach (var userDto in usersDto) { var user = Mapper.Map <User>(userDto); users.Add(user); } context.Users.AddRange(users); context.SaveChanges(); return($"Successfully imported {users.Count}"); }
//Problem 04 public static string ImportCategoryProducts(ProductShopContext context, string inputXml) { const string root = "CategoryProducts"; var categoryProducts = XMLConverter.Deserializer <ImportCategoryAndProdcutDTO>(inputXml, root); var categoryProductsResult = categoryProducts .Where(c => context.Categories.Any(s => s.Id == c.CategoryId) && context.Products.Any(s => s.Id == c.ProductId)) .Select(c => new CategoryProduct { CategoryId = c.CategoryId, ProductId = c.ProductId }) .ToList(); context.CategoryProducts.AddRange(categoryProductsResult); context.SaveChanges(); return($"Successfully imported {categoryProductsResult.Count}"); }
//02. Import Products public static string ImportProducts(ProductShopContext context, string inputXml) { var rootAttributeName = "Products"; var productsDTO = XMLConverter.Deserializer <ImportProductDTO>(inputXml, rootAttributeName); var products = productsDTO.Select(p => new Product { Name = p.Name, Price = p.Price, SellerId = p.SellerId, BuyerId = p.BuyerId }); context.Products.AddRange(products); context.SaveChanges(); return($"Successfully imported {products.Count()}"); }
public static string ImportCategories(ProductShopContext context, string inputXml) { XmlSerializer serializer = new XmlSerializer(typeof(ImportCategoryDto[]), new XmlRootAttribute("Categories")); var categoriesDto = (ImportCategoryDto[])serializer.Deserialize(new StringReader(inputXml)); var categories = new List <Category>(); foreach (var categoryDto in categoriesDto) { var category = Mapper.Map <Category>(categoryDto); categories.Add(category); } context.Categories.AddRange(categories); var count = context.SaveChanges(); return($"Successfully imported {count}"); }
public static string ImportCategories(ProductShopContext context, string inputXml) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(ImportCategoryDto[]), new XmlRootAttribute("Categories")); List <Category> categories = new List <Category>(); using (var reader = new StringReader(inputXml)) { var importCategories = (ImportCategoryDto[])xmlSerializer.Deserialize(reader); foreach (var currentCategory in importCategories) { var category = Mapper.Map <Category>(currentCategory); categories.Add(category); } } context.Categories.AddRange(categories); context.SaveChanges(); return($"Successfully imported {categories.Count}"); }
//Task 3 public static string ImportCategories(ProductShopContext context, string inputXml) { var attr = new XmlRootAttribute("Categories"); var serializer = new XmlSerializer(typeof(CategoryImportDto[]), attr); var categoryDtos = (CategoryImportDto[])serializer.Deserialize(new StringReader(inputXml)); var categories = new List <Category>(); foreach (var categoryDto in categoryDtos) { var category = Mapper.Map <Category>(categoryDto); categories.Add(category); } context.Categories.AddRange(categories); context.SaveChanges(); return(String.Format(outputResult, categories.Count)); }
public static string ImportUsers(ProductShopContext context, string inputXml) { const string root = "Users"; var userDtos = XmlConverter.Deserializer <UserImportModel>(inputXml, root); var users = userDtos .Select(x => new User { Age = x.Age, FirstName = x.FirstName, LastName = x.LastName }) .ToList(); context.Users.AddRange(users); context.SaveChanges(); return($"Successfully imported {users.Count}"); }
private static void ImportXmlUsers(ProductShopContext context, string path) { var xmlString = File.ReadAllText(path); var seriazlier = new XmlSerializer(typeof(UserDto[]), new XmlRootAttribute("users")); var deserialiedUsers = (UserDto[])seriazlier.Deserialize(new StringReader(xmlString)); var users = new List <User>(); foreach (var userDto in deserialiedUsers) { var user = Mapper.Map <User>(userDto); users.Add(user); Console.WriteLine($"User {user.FirstName} {user.LastName} successfully added!"); } context.Users.AddRange(users); context.SaveChanges(); }
//Task 1 public static string ImportUsers(ProductShopContext context, string inputXml) { var attr = new XmlRootAttribute("Users"); var serializer = new XmlSerializer(typeof(UserImportDto[]), attr); var userDtos = (UserImportDto[])serializer.Deserialize(new StringReader(inputXml)); var users = new List <User>(); foreach (var userDto in userDtos) { var user = Mapper.Map <User>(userDto); users.Add(user); } context.Users.AddRange(users); context.SaveChanges(); return(String.Format(outputResult, users.Count)); }
public static string ImportProducts(ProductShopContext context, string inputXml) { var xmlSerializer = new XmlSerializer(typeof(ImportProductDto[]), new XmlRootAttribute("Products")); ImportProductDto[] productDtos; using (var reader = new StringReader(inputXml)) { productDtos = (ImportProductDto[])xmlSerializer.Deserialize(reader); } var products = Mapper.Map <Product[]>(productDtos); context.Products.AddRange(products); context.SaveChanges(); return($"Successfully imported {products.Length}"); }
public static string ImportUsers(ProductShopContext context, string inputXml) { var xmlSerializer = new XmlSerializer(typeof(ImportUserDto[]), new XmlRootAttribute("Users")); ImportUserDto[] userDtos; using (var reader = new StringReader(inputXml)) { userDtos = (ImportUserDto[])xmlSerializer.Deserialize(reader); } var users = Mapper.Map <User[]>(userDtos); context.Users.AddRange(users); context.SaveChanges(); return($"Successfully imported {users.Length}"); }
//Import Data public static string ImportUsers(ProductShopContext context, string inputXml) { const string rootElement = "Users"; var usersResult = XMLConverter.Deserializer <ImportUserDTO>(inputXml, rootElement); var users = usersResult .Select(u => new User { FirstName = u.FirstName, LastName = u.LastName, Age = u.Age }) .ToArray(); context.Users.AddRange(users); context.SaveChanges(); return($"Successfully imported {users.Length}"); }
//Query 3. Import Categories public static string ImportCategories(ProductShopContext context, string inputXml) { var xmlSerializer = new XmlSerializer(typeof(ImportCategoryDto[]), new XmlRootAttribute("Categories")); using (var reader = new StringReader(inputXml)) { var categoriesFromDto = (ImportCategoryDto[])xmlSerializer.Deserialize(reader); var categories = Mapper.Map <Category[]>(categoriesFromDto); categories = categories.Where(x => x != null).ToArray(); context.Categories.AddRange(categories); context.SaveChanges(); } return($"Successfully imported {context.Categories.Count()}"); }
public static string ImportProducts(ProductShopContext context, string inputXml) { string root = "Products"; var productsDto = XmlConverter.Deserializer <InputProductsModel>(inputXml, root); var products = productsDto.Select(x => new Product { Name = x.Name, Price = x.Price, SellerId = x.SellerId, BuyerId = x.BuyerId }) .ToList(); context.Products.AddRange(products); context.SaveChanges(); return($"Successfully imported {products.Count}"); }
public static string ImportUsers(ProductShopContext context, string inputJson) { var dtoUsers = JsonConvert.DeserializeObject <List <UserInputModel> >(inputJson); var config = new MapperConfiguration(cfg => { cfg.AddProfile <ProductShopProfile>(); }); IMapper mapper = config.CreateMapper(); var users = mapper.Map <IEnumerable <User> >(dtoUsers); context.Users.AddRange(users); ; context.SaveChanges(); return($"Successfully imported {users.Count()}"); }
public static string ImportCategories(ProductShopContext context, string inputXml) { var categoriesDto = new List <ImportCategoryDTO>(); var serializer = new XmlSerializer(categoriesDto.GetType(), new XmlRootAttribute("Categories")); using (var stream = CreateStreamFromString(inputXml)) { categoriesDto = (List <ImportCategoryDTO>)serializer.Deserialize(stream); } foreach (var categoryDto in categoriesDto.Where(c => c.Name != null)) { var category = Mapper.Map <Category>(categoryDto); context.Categories.Add(category); } var count = context.SaveChanges(); return($"Successfully imported {count}"); }
// 02. Import Products public static string ImportProducts(ProductShopContext context, string inputXml) { XmlSerializer serializer = new XmlSerializer(typeof(ImportProductDto[]), new XmlRootAttribute("Products")); var productsDto = (ImportProductDto[])serializer.Deserialize(new StringReader(inputXml)); var products = productsDto.Select(p => new Product { Name = p.Name, Price = p.Price, SellerId = p.SellerId, BuyerId = p.BuyerId }); context.Products.AddRange(products); context.SaveChanges(); return($"Successfully imported {products.Count()}"); }
public static string ImportProducts(ProductShopContext context, string inputXml) { var productsDto = new List <ImportProductDTO>(); var serializer = new XmlSerializer(productsDto.GetType(), new XmlRootAttribute("Products")); using (var stream = CreateStreamFromString(inputXml)) { productsDto = (List <ImportProductDTO>)serializer.Deserialize(stream); } foreach (var productDto in productsDto.Where(p => p.Name != null)) { var product = Mapper.Map <Product>(productDto); context.Add(product); } var count = context.SaveChanges(); return($"Successfully imported {count}"); }
//03. Import Categories public static string ImportCategories(ProductShopContext context, string inputJson) { var categories = JsonConvert.DeserializeObject <Category[]>(inputJson); var listOfNotNullCategories = new List <Category>(); foreach (var c in categories) { if (c.Name != null) { listOfNotNullCategories.Add(c); } } context.Categories.AddRange(listOfNotNullCategories); context.SaveChanges(); return($"Successfully imported {listOfNotNullCategories.Count}"); }
public static string ImportUsers(ProductShopContext context, string inputXml) { var root = "Users"; var usersInfo = XMLConverter.Deserializer <ImportUserDTO>(inputXml, root); var users = usersInfo .Select(u => new User { FirstName = u.FirstName, LastName = u.LastName, Age = u.Age }) .ToList(); context.Users.AddRange(users); context.SaveChanges(); return($"Successfully imported {users.Count}"); }
//Problem 03 public static string ImportCategories(ProductShopContext context, string inputXml) { const string root = "Categories"; var categories = XMLConverter.Deserializer <ImportCategoryDTO>(inputXml, root) .Where(c => c.Name != null) .ToList(); var categoriesResult = categories .Select(c => new Category { Name = c.Name }) .ToList(); context.Categories.AddRange(categoriesResult); context.SaveChanges(); return($"Successfully imported {categoriesResult.Count}"); }
//Task 1 public static string ImportUsers(ProductShopContext context, string inputXml) { const string rootAttribute = "Users"; ImportUserDto[] importUsersDto = XmlConverter.Deserializer <ImportUserDto>(inputXml, rootAttribute); List <User> users = importUsersDto .Select(u => new User { FirstName = u.FirstName, LastName = u.LastName, Age = u.Age }) .ToList(); context.AddRange(users); context.SaveChanges(); return($"Successfully imported {users.Count}"); }
public static string ImportCategories(ProductShopContext context, string inputJson) { Category[] categories = JsonConvert.DeserializeObject <Category[]>(inputJson); var categoriesToAdd = new List <Category>(); foreach (var cat in categories) { if (cat.Name == null) { continue; } categoriesToAdd.Add(cat); } context.Categories.AddRange(categoriesToAdd); context.SaveChanges(); return($"Successfully imported {categoriesToAdd.Count}"); }
public static string ImportCategories(ProductShopContext context, string inputXml) { var xmlSerialixer = new XmlSerializer(typeof(CategoryInputModel[]), new XmlRootAttribute("Categories")); var textReader = new StringReader(inputXml); var categoriesDto = xmlSerialixer.Deserialize(textReader) as CategoryInputModel[]; var categories = categoriesDto .Where(x => x.Name != null) .Select(x => new Category { Name = x.Name }) .ToArray(); context.Categories.AddRange(categories); context.SaveChanges(); return($"Successfully imported {categories.Count()}"); }
// problem 2 public static string ImportProducts(ProductShopContext context, string inputXml) { const string rootElement = "Products"; var productDtos = XMLConverter.Deserializer <ImportProductDto>(inputXml, rootElement); var products = productDtos.Select(p => new Product { Name = p.Name, Price = p.Price, BuyerId = p.BuyerId, SellerId = p.SellerId }) .ToArray(); context.Products.AddRange(products); context.SaveChanges(); return($"Successfully imported {products.Length}"); }
public static string ImportProducts(ProductShopContext context, string inputXml) { var config = new MapperConfiguration(cfg => cfg.AddProfile <ProductShopProfile>()); var mapper = config.CreateMapper(); var serializer = new XmlSerializer(typeof(ProductImportDTO[]), new XmlRootAttribute("Products")); var products = (ProductImportDTO[])serializer.Deserialize(new StringReader(inputXml)); var count = 0; foreach (var p in products) { var cProd = mapper.Map <ProductImportDTO, Product>(p); if (cProd.Name != null) { context.Products.Add(cProd); count++; } } context.SaveChanges(); return($"Successfully imported {count}"); }
public static string ImportProducts(ProductShopContext context, string inputXml) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(ImportProductDto[]), new XmlRootAttribute("Products")); var productsDto = (ImportProductDto[])xmlSerializer.Deserialize(new StringReader(inputXml)); var products = new List <Product>(); foreach (var productDto in productsDto) { var product = Mapper.Map <Product>(productDto); products.Add(product); } context.Products.AddRange(products); context.SaveChanges(); return($"Successfully imported {products.Count}"); }
//EXERCISE 1 - Import Users public static string ImportUsers(ProductShopContext context, string inputXml) { const string rootName = "Users"; var usersResult = XmlConverter.Deserializer <ImportUserDto>(inputXml, rootName); var users = new List <User>(); foreach (var user in usersResult) { var newUser = new User() { FirstName = user.FirstName, LastName = user.LastName, Age = user.Age }; users.Add(newUser); } context.Users.AddRange(users); context.SaveChanges(); return($"Successfully imported {users.Count}"); }