// TODO Problem 03 - Import Cars public static string ImportCars(CarDealerContext context, string inputXml) { using (context) { var xmlSerializer = new XmlSerializer(typeof(List <ImportCarDTO>), new XmlRootAttribute("Cars")); var reader = new StringReader(inputXml); using (reader) { var carDtos = (List <ImportCarDTO>)xmlSerializer.Deserialize(reader); var cars = new List <Car>(); var partCars = new List <PartCar>(); foreach (var carDto in carDtos) { var car = new Car() { Make = carDto.Make, Model = carDto.Model, TravelledDistance = carDto.TravelledDistance, }; var parts = carDto .Parts .Where(pdto => context .Parts .Any(p => p.Id == pdto.Id)) .Select(p => p.Id) .Distinct(); foreach (var part in parts) { var partCar = new PartCar() { PartId = part, Car = car }; partCars.Add(partCar); } cars.Add(car); } context.AddRange(cars); context.AddRange(partCars); context.SaveChanges(); return($"Successfully imported {cars.Count}"); } } }
//11. Import Cars public static string ImportCars(CarDealerContext context, string inputJson) { var realParts = context.Parts.Select(x => x.Id).ToHashSet <int>(); var cars = JsonConvert.DeserializeObject <CarImportDto[]>(inputJson) .ToList(); var carsToAdd = cars.Select(c => new Car { Make = c.Make, TravelledDistance = c.TravelledDistance, Model = c.Model }) .ToList(); context.AddRange(carsToAdd); int affectedRows = context.SaveChanges(); var otherCombos = new HashSet <string>(); var result = new List <PartCar>(); var uniqueRelations = new HashSet <string>(); int counter = 0; foreach (var c in cars) { foreach (var pId in c.PartCars.Where(p => realParts.Contains(p))) { var idOfCar = carsToAdd[counter].Id; var currentStr = $"{idOfCar}-{pId}"; if (!uniqueRelations.Contains(currentStr)) { uniqueRelations.Add(currentStr); var current = new PartCar() { CarId = idOfCar, PartId = pId }; result.Add(current); } } counter++; } context.AddRange(result); context.SaveChanges(); return(string.Format(defaultMessage, affectedRows)); }
public static string ImportSales(CarDealerContext context, string inputXml) { var xmlSerializer = new XmlSerializer(typeof(ImportSalesDto[]), new XmlRootAttribute("Sales")); ImportSalesDto[] saleDtos; using (var reader = new StringReader(inputXml)) { saleDtos = ((ImportSalesDto[])xmlSerializer.Deserialize(reader)) .Where(x => context.Cars.Any(c => c.Id == x.CarId)) .ToArray(); } var sales = saleDtos .Where(s => context.Cars.Any(c => c.Id == s.CarId)) .Select(s => new Sale { CarId = s.CarId, CustomerId = s.CustomerId, Discount = s.Discount }) .ToList(); context.AddRange(sales); context.SaveChanges(); return($"Successfully imported {sales.Count}"); }
public static string ImportCustomers(CarDealerContext context, string inputXml) { var xmlSerializer = new XmlSerializer(typeof(ImportCustomerDto[]), new XmlRootAttribute("Customers")); ImportCustomerDto[] customerDtos; using (var reader = new StringReader(inputXml)) { customerDtos = (ImportCustomerDto[])xmlSerializer.Deserialize(reader); } var customers = customerDtos .Select(c => new Customer { Name = c.Name, BirthDate = c.BirthDate, IsYoungDriver = c.IsYoungDriver }) .ToList(); context.AddRange(customers); context.SaveChanges(); return($"Successfully imported {customers.Count}"); }
public static string ImportCars(CarDealerContext context, string inputXml) { var carsDto = XmlConverter.Deserializer <CarInputModel>(inputXml, "Cars"); var allParts = context.Parts.Select(x => x.Id).ToList(); var cars = carsDto .Select(x => new Car { Make = x.Make, Model = x.Model, TravelledDistance = x.TravelledDsitance, PartCars = x.CarPartsInputModel.Select(x => x.Id) .Distinct() .Intersect(allParts) .Select(c => new PartCar { PartId = c }) .ToList() }) .ToList(); context.AddRange(cars); context.SaveChanges(); return($"Successfully imported {cars.Count}"); }
//Problem13 public static string ImportSales(CarDealerContext context, string inputXml) { string rootName = "Sales"; XmlSerializer serializer = new XmlSerializer(typeof(List <ImportSalesDto>), new XmlRootAttribute(rootName)); List <ImportSalesDto> importSalesDtos = (List <ImportSalesDto>)serializer .Deserialize(new StringReader(inputXml)); List <Sale> sales = importSalesDtos .Where(s => context.Cars.Any(c => c.Id == s.CarId)) .Select(s => new Sale { CarId = s.CarId, CustomerId = s.CustomerId, Discount = s.Discount }) .ToList(); context.AddRange(sales); context.SaveChanges(); return($"Successfully imported {sales.Count}"); }
public static string ImportParts(CarDealerContext context, string inputXml) { XmlSerializer serializer = new XmlSerializer(typeof(ImportPartDto[]), new XmlRootAttribute("Parts")); var parts = (ImportPartDto[])serializer.Deserialize(new StringReader(inputXml)); var mappedParts = new List <Part>(); foreach (var importPartDto in parts) { var part = new Part { Name = importPartDto.Name, Price = importPartDto.Price, Quantity = importPartDto.Quantity, SupplierId = importPartDto.SupplierId }; if (context.Suppliers.Any(s => s.Id == part.SupplierId)) { mappedParts.Add(part); } } context.AddRange(mappedParts); context.SaveChanges(); return($"Successfully imported {mappedParts.Count}"); }
public static string ImportSales(CarDealerContext context, string inputXml) { var serializer = new XmlSerializer(typeof(ImportSaleDto[]), new XmlRootAttribute("Sales")); var importSalesDtoData = serializer.Deserialize(new StringReader(inputXml)) as ImportSaleDto[]; var allSales = importSalesDtoData.Select(x => new Sale { CarId = x.CarId, CustomerId = x.CustomerId, Discount = x.Discount }).ToArray(); List <Sale> validSales = new List <Sale>(); var allCarIds = context.Cars.Select(x => new{ Id = x.Id }).ToList(); var allCustomerIds = context.Customers.Select(x => new{ Id = x.Id }).ToList(); foreach (var sale in allSales) { if (allCarIds.Any(x => x.Id == sale.CarId)) { //Customer and Car Ids are valid... hm validSales.Add(sale); } } context.AddRange(validSales); context.SaveChanges(); return($"Successfully imported {context.Sales.Count()}"); }
//Task 10 public static string ImportParts(CarDealerContext context, string inputXml) { var attr = new XmlRootAttribute("Parts"); var serializer = new XmlSerializer(typeof(PartImportDto[]), attr); var partsDto = (PartImportDto[])serializer.Deserialize(new StringReader(inputXml)); var supplierIds = context.Suppliers .Select(p => p.Id) .ToArray(); var parts = new List <Part>(); foreach (var dto in partsDto) { if (supplierIds.Contains(dto.SupplierId)) { var part = Mapper.Map <Part>(dto); parts.Add(part); } } context.AddRange(parts); context.SaveChanges(); return(String.Format(outputResult, parts.Count)); }
//public static string ImportCars(CarDealerContext context, string inputXml) //{ // var carsParsed = XDocument.Parse(inputXml) // .Root // .Elements() // .ToList(); // var cars = new List<Car>(); // var partsID = context.Parts // .Select(x => x.Id) // .ToArray(); // foreach (var car in carsParsed) // { // var currentCar = new Car() // { // Make = car.Element("make").Value, // Model = car.Element("model").Value, // TravelledDistance = long.Parse(car.Element("TraveledDistance").Value) // }; // var partIds = new HashSet<int>(); // foreach (var partid in car.Element("parts").Elements()) // { // var pid = int.Parse(partid.Attribute("id").Value); // partIds.Add(pid); // } // foreach (var pid in partIds) // { // if (partsID.Contains(pid)) // { // var currentPair = new PartCar() // { // Car = currentCar, // PartId = pid // }; // currentCar.PartCars.Add(currentPair); // } // } // cars.Add(currentCar); // } // context.Cars.AddRange(cars); // context.SaveChanges(); // return $"Successfully imported {cars.Count}"; //} //Task 12 public static string ImportCustomers(CarDealerContext context, string inputXml) { var customersParsed = XDocument.Parse(inputXml) .Root .Elements() .ToArray(); var customers = new List <Customer>(); foreach (var customer in customersParsed) { var currentCustomer = new Customer { Name = customer.Element("name").Value, BirthDate = DateTime.Parse(customer.Element("birthDate").Value), IsYoungDriver = Convert.ToBoolean(customer.Element("isYoungDriver").Value) }; customers.Add(currentCustomer); } context.AddRange(customers); context.SaveChanges(); return($"Successfully imported {customers.Count}"); }
//Query 11. Import Cars public static string ImportCars(CarDealerContext context, string inputXml) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(ImportCarDto[]), new XmlRootAttribute("Cars")); var carsDtos = (ImportCarDto[])xmlSerializer.Deserialize(new StringReader(inputXml)); var cars = new List <Car>(); var carParts = new List <PartCar>(); foreach (var car in carsDtos) { var currentCar = Mapper.Map <Car>(car); foreach (var part in car.Parts) { if (!currentCar.PartCars.Select(e => e.PartId).Contains(part.PartId) && part.PartId <= context.Parts.Count()) { currentCar.PartCars.Add(new PartCar() { PartId = part.PartId }); } } cars.Add(currentCar); } context.AddRange(cars); context.SaveChanges(); return($"Successfully imported {cars.Count}"); }
private static void SeedParts() { using (var context = new CarDealerContext()) { var root = new XmlRootAttribute("parts"); var serializer = new XmlSerializer(typeof(ImportPartDto[]), root); var namespaces = new XmlSerializerNamespaces(new[] { new XmlQualifiedName("", "") }); var stringParts = File.ReadAllText("Import/parts.xml"); var partsDto = (ImportPartDto[])serializer.Deserialize(new StringReader(stringParts)); var random = new Random(); var suppliersCount = context.Suppliers.Count(); var parts = new List <Part>(); foreach (var dto in partsDto) { var part = Mapper.Map <Part>(dto); var supplierId = random.Next(1, suppliersCount + 1); part.SupplierId = supplierId; parts.Add(part); } context.AddRange(parts); context.SaveChanges(); } }
public static string ImportCars(CarDealerContext context, string inputXml) { var partIds = context.Parts.Select(p => p.Id).ToList(); var serializer = new XmlSerializer(typeof(List <ImportCarDto>), new XmlRootAttribute("Cars")); var carDtos = (List <ImportCarDto>)serializer.Deserialize(new StringReader(inputXml)); var partCars = new List <PartCar>(); foreach (var carDto in carDtos) { var vehicle = Mapper.Map <Car>(carDto); partCars.AddRange(from partDto in carDto.ImportPartsListDto.Select(p => p.Id).Distinct() where partIds.Contains(partDto) select new PartCar { PartId = partDto, Car = vehicle }); } context.AddRange(partCars); context.SaveChanges(); var count = context.Cars.Count(); return(string.Format(Result, count)); }
//10-Import Parts public static string ImportParts(CarDealerContext context, string inputXml) { var serializer = new XmlSerializer(typeof(ImportPartDto[]), new XmlRootAttribute("Parts")); var partsDto = (ImportPartDto[])serializer.Deserialize(new StringReader(inputXml)); var parts = new List <Part>(); foreach (var partDto in partsDto) { var supplier = context.Suppliers.Find(partDto.SupplierId); if (supplier != null) { var part = Mapper.Map <Part>(partDto); parts.Add(part); } } context.AddRange(parts); var count = context.SaveChanges(); return($"Successfully imported {count}"); }
public static string ImportCars(CarDealerContext context, string inputJson) { InitializeMapper(); var partsIds = context.Parts.Select(x => x.Id); var allCarModels = JsonConvert.DeserializeObject <IEnumerable <CarsModel> >(inputJson); var cars = new List <Car>(); foreach (var car in allCarModels) { var currCar = new Car() { Make = car.Make, Model = car.Model, TravelledDistance = car.TravelledDistance, }; foreach (var part in car.PartsId.Distinct()) { if (partsIds.Contains(part)) { currCar.PartCars.Add(new PartCar { PartId = part }); } } cars.Add(currCar); } context.AddRange(cars); context.SaveChanges(); return($"Successfully imported {allCarModels.Count()}."); }
public static string ImportCars(CarDealerContext context, string inputJson) { var carsImport = JsonConvert.DeserializeObject <CarImportDto[]>(inputJson); var readyCars = Mapper.Map <CarImportDto[], Car[]>(carsImport); context.AddRange(readyCars); context.SaveChanges(); HashSet <int> partIds = context.Parts.Select(x => x.Id).ToHashSet(); HashSet <Part> parts = context.Parts.ToHashSet(); HashSet <Car> cars = context.Cars.ToHashSet(); HashSet <PartCar> addedCarParts = new HashSet <PartCar>(); List <PartCar> partsList = new List <PartCar>(); foreach (var car in carsImport) { car.PartsId = car.PartsId.Distinct().ToList(); Car currentCar = cars.Where(x => x.Make == car.Make && x.Model == car.Model && x.TravelledDistance == car.TravelledDistance).FirstOrDefault(); if (currentCar == null) { continue; } foreach (var id in car.PartsId) { if (!partIds.Contains(id)) { continue; } PartCar partCar = new PartCar { CarId = currentCar.Id, PartId = id }; if (!addedCarParts.Contains(partCar)) { partsList.Add(partCar); addedCarParts.Add(partCar); } } if (partsList != null) { currentCar.PartCars = partsList; context.PartCars.AddRange(partsList); partsList.Clear(); } } int rowsCount = context.SaveChanges(); return($"Successfully imported {cars.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 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()}"); }
//Task3 public static string ImportCars(CarDealerContext context, string inputJson) { List <ImportCarDto> carsDto = JsonConvert.DeserializeObject <List <ImportCarDto> >(inputJson); List <Car> cars = new List <Car>(); foreach (var carDto in carsDto) { Car car = new Car { Make = carDto.Make, Model = carDto.Model, TravelledDistance = carDto.TravelledDistance }; foreach (var partId in carDto.PartsId.Distinct()) { car.PartCars.Add(new PartCar { Car = car, PartId = partId }); } cars.Add(car); } context.AddRange(cars); context.SaveChanges(); return($"Successfully imported {cars.Count}."); }
public static string ImportCars(CarDealerContext context, string inputJson) { var cars = JsonConvert.DeserializeObject <List <CarDto> >(inputJson); var carsToAdd = new List <Car>(); foreach (var carDto in cars) { var vehicle = Mapper.Map <CarDto, Car>(carDto); var parts = carDto.PartsId.Distinct().ToList(); foreach (var part in parts) { vehicle.PartCars.Add(new PartCar { Car = vehicle, PartId = part }); } carsToAdd.Add(vehicle); } context.AddRange(carsToAdd); var count = carsToAdd.Count; context.SaveChanges(); return(string.Format(Result, count)); }
//04.Import customers public static string ImportCustomers(CarDealerContext context, string inputXml) { XmlSerializer serializer = new XmlSerializer(typeof(CustomerImport[]), new XmlRootAttribute("Customers")); using StringReader reader = new StringReader(inputXml); CustomerImport[] dtos = (CustomerImport[])serializer.Deserialize(reader); var customers = new HashSet <Customer>(); foreach (var dto in dtos) { Customer c = new Customer { Name = dto.Name, BirthDate = DateTime.Parse(dto.BirthDate), IsYoungDriver = bool.Parse(dto.IsYoungDriver) }; customers.Add(c); } context.AddRange(customers); context.SaveChanges(); return($"Successfully imported {customers.Count}"); }
public static string ImportParts(CarDealerContext context, string inputXml) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(ImportPartDto[]), new XmlRootAttribute("Parts")); ImportPartDto[] serializedParts; using (var reader = new StringReader(inputXml)) { serializedParts = (ImportPartDto[])xmlSerializer.Deserialize(reader); } var parts = serializedParts.Select(x => new Part { Name = x.Name, Price = x.Price, Quantity = x.Quantity, SupplierId = x.SupplierId }) .Where(x => context.Suppliers.Any(s => s.Id == x.SupplierId)) .ToArray(); context.AddRange(parts); context.SaveChanges(); return($"Successfully imported {parts.Length}"); }
//Query 10. Import Parts public static string ImportParts(CarDealerContext context, string inputXml) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(ImportPartDto[]), new XmlRootAttribute("Parts")); using StringReader stringReader = new StringReader(inputXml); ImportPartDto[] dtos = (ImportPartDto[])xmlSerializer.Deserialize(stringReader); ICollection <Part> parts = new HashSet <Part>(); foreach (var partDto in dtos) { Supplier supplier = context.Suppliers.Find(partDto.SupplierId); if (supplier is null) { continue; } Part part = new Part() { Name = partDto.Name, Price = decimal.Parse(partDto.Price), Quantity = partDto.Quantity, SupplierId = partDto.SupplierId }; parts.Add(part); } context.AddRange(parts); context.SaveChanges(); return($"Successfully imported {parts.Count}"); }
//Query 13. Import Sales public static string ImportSales(CarDealerContext context, string inputXml) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(ImportSaleDto[]), new XmlRootAttribute("Sales")); using StringReader stringReader = new StringReader(inputXml); ImportSaleDto[] dtos = (ImportSaleDto[])xmlSerializer.Deserialize(stringReader); ICollection <Sale> sales = new HashSet <Sale>(); foreach (var saleDto in dtos) { if (context.Cars.Any(c => c.Id == saleDto.CarId)) { Sale sale = new Sale() { CarId = saleDto.CarId, CustomerId = saleDto.CustomerId, Discount = decimal.Parse(saleDto.Discount) }; sales.Add(sale); } } context.AddRange(sales); context.SaveChanges(); return($"Successfully imported {sales.Count}"); }
//05.Import Sales public static string ImportSales(CarDealerContext context, string inputXml) { const string root = "Sales"; var serializer = new XmlSerializer(typeof(SaleInputModel[]), new XmlRootAttribute(root)); var textReader = new StringReader(inputXml); var salesDto = serializer.Deserialize(textReader) as SaleInputModel[]; var carsId = context.Cars.Select(i => i.Id).ToList(); var sales = salesDto .Where(s => carsId.Contains(s.CarId)) .Select(s => new Sale() { CarId = s.CarId, CustomerId = s.CustomerId, Discount = s.Discount }) .ToList(); context.AddRange(sales); context.SaveChanges(); return($"Successfully imported {sales.Count}"); }
//02.Import Parts public static string ImportParts(CarDealerContext context, string inputXml) { const string root = "Parts"; var xmlSerializer = new XmlSerializer(typeof(PartsInputModel[]), new XmlRootAttribute(root)); var textRead = new StringReader(inputXml); var partsDto = xmlSerializer.Deserialize(textRead) as PartsInputModel[]; var supliersId = context.Suppliers.Select(i => i.Id).ToList(); var parts = partsDto .Where(s => supliersId.Contains(s.SupplierId)) .Select(p => new Part { Name = p.Name, Price = p.Price, Quantity = p.Quantity, SupplierId = p.SupplierId }) .ToList(); context.AddRange(parts); context.SaveChanges(); return($"Successfully imported {parts.Count}"); }
public static string ImportCustomers(CarDealerContext context, string inputJson) { var customers = JsonConvert.DeserializeObject <List <Customer> >(inputJson); context.AddRange(customers); context.SaveChanges(); return($"Successfully imported {customers.Count}."); }
public static string ImportSales(CarDealerContext context, string inputJson) { var sales = JsonConvert.DeserializeObject <List <Sale> >(inputJson); context.AddRange(sales); context.SaveChanges(); return($"Successfully imported {sales.Count}."); }
public static string ImportSuppliers(CarDealerContext context, string inputJson) { var suppliers = JsonConvert.DeserializeObject <Supplier[]>(inputJson); context.AddRange(suppliers); context.SaveChanges(); return($"Successfully imported {suppliers.Length}."); }
public static string ImportSuppliers(CarDealerContext context, string inputJson) { var data = JsonConvert.DeserializeObject <Supplier[]>(inputJson); context.AddRange(data); context.SaveChanges(); return($"Successfully imported {data.Count()}."); }