//EXERCISE 11 - Import Cars public static string ImportCars(CarDealerContext context, string inputXml) { const string rootName = "Cars"; var cars = new List <Car>(); var carParts = new List <PartCar>(); var partsCount = context.Parts.Count(); var resultCarDtos = XmlConverter.Deserializer <CarDTO>(inputXml, rootName); foreach (var car in resultCarDtos) { var newCar = new Car { Make = car.Make, Model = car.Model, TravelledDistance = car.TraveledDistance }; cars.Add(newCar); foreach (var partId in car.CarParts.Select(x => new { partId = x.PartId }).Distinct()) { var newCarPart = new PartCar { PartId = partId.partId, Car = newCar }; carParts.Add(newCarPart); } } context.Cars.AddRange(cars); context.PartCars.AddRange(carParts); context.SaveChanges(); return($"Successfully imported {cars.Count}"); }
public static string ImportCategoryProducts(ProductShopContext context, string inputXml) { const string root = "CategoryProducts"; var categoriesProductsDto = XmlConverter.Deserializer <CategoryProductImportModel>(inputXml, root); var validCategories = context.Categories .Select(x => x.Id); var validProducts = context.Products .Select(x => x.Id); var categoriesProducts = categoriesProductsDto .Where(x => validCategories.Contains(x.CategoryId) && validProducts.Contains(x.ProductId)) .Select(x => new CategoryProduct { CategoryId = x.CategoryId, ProductId = x.ProductId }) .ToArray(); context.CategoryProducts.AddRange(categoriesProducts); context.SaveChanges(); return($"Successfully imported {categoriesProducts.Count()}"); }
public static string ImportCategoryProducts(ProductShopContext context, string inputXml) { string root = "CategoryProducts"; var categoriesProductsDtos = XmlConverter.Deserializer <InputCategoriesProductsModel>(inputXml, root); List <CategoryProduct> categoryProducts = new List <CategoryProduct>(); foreach (var categoryProductDto in categoriesProductsDtos) { if (context.Categories.Any(c => c.Id == categoryProductDto.CategoryId) && // check if both exist context.Products.Any(p => p.Id == categoryProductDto.ProductId)) { CategoryProduct categoryProduct = new CategoryProduct() { CategoryId = categoryProductDto.CategoryId, ProductId = categoryProductDto.ProductId }; categoryProducts.Add(categoryProduct); } } context.CategoryProducts.AddRange(categoryProducts); context.SaveChanges(); return($"Successfully imported {categoryProducts.Count()}"); }
public static string ImportSales(CarDealerContext context, string inputXml) { var root = "Sales"; var data = XmlConverter.Deserializer <ImportSaleDTO>(inputXml, root); List <Sale> sales = new List <Sale>(); foreach (var saleDTO in data) { var existCar = context.Cars.Any(c => c.Id == saleDTO.CarId); if (!existCar) { continue; } var sale = new Sale { CarId = saleDTO.CarId, CustomerId = saleDTO.CustomerId, Discount = saleDTO.Discount }; sales.Add(sale); } context.Sales.AddRange(sales); context.SaveChanges(); return($"Successfully imported {sales.Count}"); }
public static string ImportCars(CarDealerContext context, string inputXml) { const string rootElement = "Cars"; var carsResult = XmlConverter.Deserializer <ImportCarDto>(inputXml, rootElement); var carsToImport = new List <Car>(); foreach (var carDto in carsResult) { var uniqueParts = carDto.Parts.Select(p => p.Id).Distinct().ToArray(); var realParts = uniqueParts.Where(id => context.Parts.Any(p => p.Id == id)); var car = new Car { Make = carDto.Make, Model = carDto.Model, TravelledDistance = carDto.TraveledDistance, PartCars = realParts.Select(id => new PartCar { PartId = id }).ToArray() }; carsToImport.Add(car); } context.Cars.AddRange(carsToImport); context.SaveChanges(); return($"Successfully imported {carsToImport.Count}"); }
public static string ImportSuppliers(CarDealerContext context, string inputXml) { // ръчно //var xmlSerializer = new XmlSerializer(typeof(SupplierInputModel[]), // new XmlRootAttribute("Suppliers")); //var textRead = new StringReader(inputXml); //var suppliersDto = xmlSerializer.Deserialize(textRead) as SupplierInputModel[]; // с хелпъра на Стоян Шопов !!!! var suppliersDto = XmlConverter.Deserializer <SupplierInputModel>(inputXml, ("Suppliers")); var suppliers = suppliersDto.Select(x => new Supplier { Name = x.Name, IsImporter = x.IsImporter }) .ToList(); context.Suppliers.AddRange(suppliers); context.SaveChanges(); return($"Successfully imported {suppliers.Count}"); }
public static string ImportParts(CarDealerContext context, string inputXml) { const string root = "Parts"; var parstDto = XmlConverter.Deserializer <PartInputModel>(inputXml, root); /* * XmlSerializer xmlSerializer = new XmlSerializer(typeof(PartInputModel[]), new XmlRootAttribute(root)); * var textReader = new StringReader(inputXml); * var partInputModels = xmlSerializer.Deserialize(textReader) as PartInputModel[]; */ var supplierIds = context.Suppliers .Select(x => x.Id) .ToList(); var parts = parstDto .Where(s => supplierIds.Contains(s.SupplierId)) .Select(x => new Part { Name = x.Name, Price = x.Price, Quantity = x.Quantity, SupplierId = x.SupplierId }).ToList(); context.Parts.AddRange(parts); context.SaveChanges(); return($"Successfully imported {parts.Count}"); }
//01.ImportUsers public static string ImportUsers(ProductShopContext context, string inputXml) { //Basic way of deserializing the input //var xmlSerializer = new XmlSerializer(typeof(UsersInputModel[]), // new XmlRootAttribute("Users")); //var textReader = new StringReader(inputXml); //var usersDto = xmlSerializer // .Deserialize(textReader) as UsersInputModel[]; //Using XmlConverter created by StoyanShopov const string root = "Users"; var usersDto = XmlConverter .Deserializer <UsersInputModel>(inputXml, root); var users = usersDto .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()}"); }
//Task4 public static string ImportCategoryProducts(ProductShopContext context, string inputXml) { const string rootAttribute = "CategoryProducts"; ImportCategoryProductDto[] importCategoriesProductsDto = XmlConverter.Deserializer <ImportCategoryProductDto>(inputXml, rootAttribute); //int[] validCategoryIds = context.Categories // .Select(c => c.Id) // .ToArray(); //int[] validProductIds = context.Products // .Select(p => p.Id) // .ToArray(); //.Where(cp => validCategoryIds.Contains(cp.CategoryId) && //validProductIds.Contains(cp.ProductId)) List <CategoryProduct> categoryProducts = importCategoriesProductsDto .Where(i => context.Categories.Any(c => c.Id == i.CategoryId) && context.Products.Any(p => p.Id == i.ProductId)) .Select(cp => new CategoryProduct { CategoryId = cp.CategoryId, ProductId = cp.ProductId }) .ToList(); context.CategoryProducts.AddRange(categoryProducts); context.SaveChanges(); return($"Successfully imported {categoryProducts.Count}"); }
//04 public static string ImportCategoryProducts(ProductShopContext context, string inputXml) { var catProFromXML = XmlConverter.Deserializer <ImportCategoryProductDTO>(inputXml, "CategoryProducts"); var productsId = context.Products.Select(i => i.Id).ToList(); var categoiesId = context.Categories.Select(c => c.Id).ToList(); var categoryProducts = new List <CategoryProduct>(); foreach (var item in catProFromXML) { if (productsId.Contains(item.ProductId) && categoiesId.Contains(item.CategoryId)) { var newCatPro = new CategoryProduct { CategoryId = item.CategoryId, ProductId = item.ProductId }; categoryProducts.Add(newCatPro); } } context.CategoryProducts.AddRange(categoryProducts); context.SaveChanges(); return($"Successfully imported {categoryProducts.Count}"); }
// 11. Import Cars public static string ImportCars(CarDealerContext context, string inputXml) { //Import the cars from the provided file cars.xml. Select unique car part ids. If the part id doesn’t exists, skip the part record. var carsDtos = XmlConverter.Deserializer <ImportCarsDto>(inputXml, "Cars"); var cars = new List <Car>(); foreach (var car in carsDtos) { var partsIds = car.Parts .Select(p => p.Id) .Distinct() .Where(id => context.Parts.Any(p => p.Id == id)) .ToList(); var currCar = new Car() { Make = car.Make, Model = car.Model, TravelledDistance = car.TravelledDistance, PartCars = partsIds.Select(id => new PartCar() { PartId = id }) .ToList() }; cars.Add(currCar); } context.Cars.AddRange(cars); context.SaveChanges(); return($"Successfully imported {cars.Count}"); }
//01. Import Users public static string ImportUsers(ProductShopContext context, string inputXml) { const string roodElement = "Users"; var usersResult = XmlConverter.Deserializer <ImportUserDto>(inputXml, roodElement); /* * List<User> users = new List<User>(); * * foreach (var importUserDtio in usersResult) * { * var user = new User * { * FirstName = importUserDtio.FirstName, * LastName = importUserDtio.LastName, * Age = importUserDtio.Age * }; * * users.Add(user); * } */ 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}"); }
public static string ImportSales(CarDealerContext context, string inputXml) { const string root = "Sales"; var saleInputModels = XmlConverter.Deserializer <SalesInputModel>(inputXml, root); var cars = context.Cars .Select(x => x.Id) .ToList(); var sales = saleInputModels .Where(s => cars.Contains(s.CarId)) .Select(x => new Sale { CarId = x.CarId, CustomerId = x.CustomerId, Discount = x.Discount }) .ToList(); context.Sales.AddRange(sales); context.SaveChanges(); return($"Successfully imported {sales.Count()}"); // return null; }
//11. Import Cars public static string ImportCars(CarDealerContext context, string inputXml) { //2:39:36, 2:49:12 var carsDtos = XmlConverter.Deserializer <ImportCarDto>(inputXml, "Cars"); var cars = new List <Car>(); foreach (var carDto in carsDtos) { var uniqueParts = carDto.Parts.Select(x => x.Id).Distinct().ToArray(); var realPartds = uniqueParts.Where(id => context.Parts.Any(i => i.Id == id)); var car = new Car { Make = carDto.Make, Model = carDto.Model, TravelledDistance = carDto.TraveledDistance, PartCars = realPartds.Select(id => new PartCar { PartId = id }) .ToArray() }; cars.Add(car); } context.Cars.AddRange(cars); context.SaveChanges(); return($"Successfully imported {cars.Count}"); }
//03. Import Categories public static string ImportCategories(ProductShopContext context, string inputXml) { const string rootElement = "Categories"; var categoriesDtio = XmlConverter.Deserializer <ImportCategoryDto>(inputXml, rootElement); List <Category> categories = new List <Category>(); foreach (var dto in categoriesDtio) { if (dto.Name == null) { continue; } var category = new Category { Name = dto.Name }; categories.Add(category); } context.Categories.AddRange(categories); context.SaveChanges(); return($"Successfully imported {categories.Count}"); }
//Task 04. public static string ImportCategoryProducts (ProductShopContext context, string inputXml) { var categoryProductsDtos = XmlConverter .Deserializer <ImportCategoriesProductsDto>(inputXml, "CategoryProducts"); List <CategoryProduct> categoryProducts = new List <CategoryProduct>(); foreach (var catProdDto in categoryProductsDtos) { if (context.Categories.Any(x => x.Id == catProdDto.CategoryId) && context.Products.Any(y => y.Id == catProdDto.ProductId)) { var newCategoryProduct = new CategoryProduct { CategoryId = catProdDto.CategoryId, ProductId = catProdDto.ProductId }; categoryProducts.Add(newCategoryProduct); } } context.CategoryProducts.AddRange(categoryProducts); context.SaveChanges(); return($"Successfully imported {categoryProducts.Count}"); }
public static string ImportCars(CarDealerContext context, string inputXml) { InitializeMapper(); var partsIds = context.Parts.Select(x => x.Id); var carsDTOs = XmlConverter.Deserializer <CarInputModel>(inputXml, "Cars") .Select(x => new CarInputModel { Make = x.Make, Model = x.Model, TraveledDistance = x.TraveledDistance, Parts = x.Parts.Select(x => x.PartId) .Distinct() .Intersect(partsIds) .Select(x => new MiniPartsInputModel { PartId = x }) .ToArray(), }); var cars = carsDTOs .Select(x => new Car { Make = x.Make, Model = x.Model, TravelledDistance = x.TraveledDistance, PartCars = x.Parts.Select(p => new PartCar { PartId = p.PartId }).ToList() }); context.AddRange(cars); context.SaveChanges(); return($"Successfully imported {cars.Count()}"); }
public static string ImportCars(CarDealerContext context, string inputXml) { const string root = "Cars"; var carsDto = XmlConverter.Deserializer <CarInputModel>(inputXml, root); var allParts = context.Parts.Select(x => x.Id) .ToList(); var cars = carsDto .Select(x => new Car { Make = x.Make, Model = x.Model, TravelledDistance = x.TraveledDistance, PartCars = x.CarPartsInputModel.Select(y => y.Id) .Distinct() .Intersect(allParts) .Select(pc => new PartCar { PartId = pc }) .ToList() }) .ToList(); context.Cars.AddRange(cars); context.SaveChanges(); return($"Successfully imported {cars.Count}"); }
//11 public static string ImportCars(CarDealerContext context, string inputXml) { var rootElement = "Cars"; var carsDTO = XmlConverter.Deserializer <CarsDTO>(inputXml, rootElement); var cars = new List <Car>(); foreach (var carDTO in carsDTO) { var uniqueParts = carDTO.Parts.Select(s => s.Id).Distinct().ToArray(); var realParts = uniqueParts.Where(id => context.Parts.Any(i => i.Id == id)); var car = new Car { Make = carDTO.Make, Model = carDTO.Model, TravelledDistance = carDTO.TraveledDistance, PartCars = realParts.Select(id => new PartCar() { PartId = id, }).ToArray() }; cars.Add(car); } context.Cars.AddRange(cars); context.SaveChanges(); return($"Successfully imported {cars.Count}"); }
//Problem - 13 public static string ImportSales(CarDealerContext context, string inputXml) { const string rootElement = "Sales"; ImportSaleDTO[] saleDTOs = XmlConverter.Deserializer <ImportSaleDTO>(inputXml, rootElement).ToArray(); var carsCount = context.Cars.Count(); List <Sale> sales = new List <Sale>(); foreach (var saleDto in saleDTOs) { if (saleDto.CarId <= carsCount) { var sale = new Sale { CarId = saleDto.CarId, CustomerId = saleDto.CustomerId, Discount = saleDto.Discount }; sales.Add(sale); } } context.Sales.AddRange(sales); context.SaveChanges(); return($"Successfully imported {sales.Count}"); }
} // Query 12. Import Customers public static string ImportSales(CarDealerContext context, string inputXml) { var root = "Sales"; var saleDto = XmlConverter.Deserializer <SalesInputModel>(inputXml, root); //ToDO wHERE CARID IS NOT MISSING var carsId = context.Cars .Select(x => x.Id) .ToList(); var sales = saleDto .Where(x => carsId.Contains(x.CarId)) .Select(x => new Sale { CarId = x.CarId, CustomerId = x.CustomerId, Discount = x.Discount }) .ToList(); context.AddRange(sales); context.SaveChanges(); return($"Successfully imported {sales.Count()}"); } //Query 13. Import Sales
public static string ImportCategoryProducts(ProductShopContext context, string inputXml) // Query 4. Import Categories and Products { var root = "CategoryProducts"; var categorys = context.Categories.Select(c => c.Id).ToArray(); var products = context.Products.Select(p => p.Id).ToArray(); var categoryProductDto = XmlConverter.Deserializer <CategoryProductInputModel>(inputXml, root); var categoryProduct = categoryProductDto .Where(cp => categorys.Contains(cp.CategoryId) && products.Contains(cp.ProductId)) .Select(cp => new CategoryProduct { CategoryId = cp.CategoryId, ProductId = cp.ProductId }) .ToList(); context.AddRange(categoryProduct); context.SaveChanges(); var result = $"Successfully imported {categoryProduct.Count}"; return(result); }
public static string ImportCategoryProducts(ProductShopContext context, string inputXml) { var categoriesProductsDto = XmlConverter.Deserializer <CategoryProductInputModel>(inputXml, ("CategoryProducts")); var categoriesIds = context.Categories .Select(x => x.Id) .ToList(); var productsIds = context.Products .Select(x => x.Id) .ToList(); var categoryProducts = categoriesProductsDto .Where(x => categoriesIds.Contains(x.CategoryId) && productsIds.Contains(x.ProductId)) .Select(c => new CategoryProduct { CategoryId = c.CategoryId, ProductId = c.ProductId }) .ToList(); context.CategoryProducts.AddRange(categoryProducts); context.SaveChanges(); return($"Successfully imported {categoryProducts.Count()}"); }
public static string ImportParts(CarDealerContext context, string inputXml) { const string root = "Parts"; var partInputModels = XmlConverter.Deserializer <PartsInputModel>(inputXml, root); var supplierIds = context.Suppliers .Select(x => x.Id) .ToList(); var parts = partInputModels .Where(s => supplierIds.Contains(s.SupplierId)) .Select(x => new Part { Name = x.Name, Price = x.Price, Quantity = x.Quantity, SupplierId = x.SupplierId }) .ToList(); context.Parts.AddRange(parts); context.SaveChanges(); return($"Successfully imported {parts.Count()}"); }
/// <summary> /// 10th Exercise - ImportParts from Xml to PartInputModel and deserialize it and add them in the DataBase /// </summary> public static string ImportParts(CarDealerContext context, string inputXml) { var root = "Parts"; var partsDto = XmlConverter.Deserializer <PartInputModel>(inputXml, root); //var serialise = new XmlSerializer(typeof(PartInputModel[]), new XmlRootAttribute("Parts")); //var textRead = new StringReader(inputXml); //var partsDto = serialise.Deserialize(textRead) as PartInputModel[]; var suppId = context.Suppliers .Select(x => x.Id) .ToList(); var parts = partsDto .Where(x => suppId.Contains(x.SupplierId)) .Select(p => new Part { Name = p.Name, Price = p.Price, Quantity = p.Quantity, SupplierId = p.SupplierId }) .ToList(); context.Parts.AddRange(parts); context.SaveChanges(); return($"Successfully imported {parts.Count}"); }
public static string ImportCars(CarDealerContext context, string inputXml) { const string root = "Cars"; //var cars = new List<Car>(); var carsDto = XmlConverter.Deserializer <CarInputModel>(inputXml, root); var allParts = context.Parts.Select(x => x.Id).ToList(); var cars = carsDto .Select(x => new Car { Make = x.Make, Model = x.Model, TravelledDistance = x.TraveledDistance, PartCars = x.CarPartsInputModel.Select(x => x.Id) .Distinct() .Intersect(allParts) .Select(pc => new PartCar { PartId = pc }) .ToList() }); //var allParts = context.Parts.Select(x => x.Id).ToList(); //foreach (var currCar in carsDto) //{ // var distinctedparts = currCar.CarPartsInputModel.Select(x => x.Id).Distinct(); // var parts = distinctedparts.Intersect(allParts); // var car = new Car // { // Make = currCar.Make, // Model = currCar.Model, // TravelledDistance = currCar.TraveledDistance, // }; // foreach (var part in parts) // { // var partCar = new PartCar // { // PartId = part // }; // car.PartCars.Add(partCar); // } // cars.Add(car); //} context.AddRange(cars); context.SaveChanges(); return($"Successfully imported {cars.Count()}"); }
// 11. Import Cars public static string ImportCars(CarDealerContext context, string inputXml) { var carsDto = XmlConverter.Deserializer <CarInputModel>(inputXml, "Cars"); var cars = new List <Car>(); var allParts = context .Parts .Select(x => x.Id) .ToList(); var linqCars = carsDto .Select(x => new Car { Make = x.Make, Model = x.Model, TravelledDistance = x.TraveledDistance, PartCars = x.CarPartsInputModel.Select(x => x.Id) .Distinct() .Intersect(allParts) .Select(pc => new PartCar { PartId = pc }) .ToList() }) .ToList(); foreach (var currentCar in carsDto) { var distinctParts = currentCar.CarPartsInputModel.Select(x => x.Id).Distinct(); var carParts = distinctParts .Intersect(allParts) .Select(pc => new PartCar { PartId = pc }) .ToList(); var car = new Car { Make = currentCar.Make, Model = currentCar.Model, TravelledDistance = currentCar.TraveledDistance, PartCars = carParts }; cars.Add(car); } context.AddRange(cars); context.SaveChanges(); return($"Successfully imported {cars.Count()}"); }
public static string ImportProducts(ProductShopContext context, string inputXml) { InitializeAutoMapper(); var productsDto = XmlConverter.Deserializer <ProductImportModel>(inputXml, "Products"); var products = mapper.Map <IEnumerable <Product> >(productsDto); context.Products.AddRange(products); var result = context.SaveChanges(); return($"Successfully imported {result}"); }
public static string ImportUsers(ProductShopContext context, string inputXml) { InitializeAutoMapper(); var usersDto = XmlConverter.Deserializer <UserImportModel>(inputXml, "Users"); var users = mapper.Map <IEnumerable <User> >(usersDto); context.Users.AddRange(users); var result = context.SaveChanges(); return($"Successfully imported {result}"); }
public static string ImportCategories(ProductShopContext context, string inputXml) { InitializeAutoMapper(); var categoriesDto = XmlConverter.Deserializer <CategoryImportModel>(inputXml, "Categories").Where(x => x.Name != null); var categories = mapper.Map <IEnumerable <Category> >(categoriesDto); context.Categories.AddRange(categories); var result = context.SaveChanges(); return($"Successfully imported {result}"); }