private static void InitiliazeDatabase() { var ctx = new CarDealerContext(); ctx.Database.Initialize(true); }
public static void Main(string[] args) { CarDealerContext db = new CarDealerContext(); //Problem - 00 //ResetDatabase(db); //Problem - 09 //string inputXml = File.ReadAllText(@"../../../Datasets/suppliers.xml"); //string result = ImportSuppliers(db, inputXml); //Console.WriteLine(result); //Problem - 10 //string inputXml = File.ReadAllText(@"../../../Datasets/parts.xml"); //string result = ImportParts(db, inputXml); //Console.WriteLine(result); //Problem - 11 //string inputXml = File.ReadAllText(@"../../../Datasets/cars.xml"); //string result = ImportCars(db, inputXml); //Console.WriteLine(result); //Problem - 12 //string inputXml = File.ReadAllText(@"../../../Datasets/customers.xml"); //string result = ImportCustomers(db, inputXml); //Console.WriteLine(result); //Problem - 13 //string inputXml = File.ReadAllText(@"../../../Datasets/sales.xml"); //string result = ImportSales(db, inputXml); //Console.WriteLine(result); //Problem - 14 //EnsureDirectoryPath(directoryPath); //string xml = GetCarsWithDistance(db); //File.WriteAllText(directoryPath + "/cars.xml", xml); //Problem - 15 //EnsureDirectoryPath(directoryPath); //string xml = GetCarsFromMakeBmw(db); //File.WriteAllText(directoryPath + "/bmw-cars.xml", xml); //Problem - 16 //EnsureDirectoryPath(directoryPath); //string xml = GetLocalSuppliers(db); //File.WriteAllText(directoryPath + "/local-suppliers.xml", xml); //Problem - 17 //EnsureDirectoryPath(directoryPath); //string xml = GetCarsWithTheirListOfParts(db); //File.WriteAllText(directoryPath + "/cars-and-parts.xml", xml); //Problem - 18 //EnsureDirectoryPath(directoryPath); //string xml = GetTotalSalesByCustomer(db); //File.WriteAllText(directoryPath + "/customers-total-sales.xml", xml); //Problem - 19 EnsureDirectoryPath(directoryPath); string xml = GetSalesWithAppliedDiscount(db); File.WriteAllText(directoryPath + "/sales-discounts.xml", xml); }
public static void Main(string[] args) { var context = new CarDealerContext(); Console.WriteLine(GetCarsFromMakeBmw(context)); }
//17. Cars with Their List of Parts public static string GetCarsWithTheirListOfParts(CarDealerContext context) { return(null); }
public static string ImportCars(CarDealerContext context, string inputJson) { var carsDTOs = JsonConvert.DeserializeObject <List <CarImportDTO> >(inputJson); var partIds = context .Parts .Select(p => p.Id) .ToHashSet(); var carPartsToAdd = new HashSet <PartCar>(); var carsToAdd = new List <Car>(); foreach (var carDTO in carsDTOs) { var car = new Car() { Make = carDTO.Make, Model = carDTO.Model, TravelledDistance = carDTO.TravelledDistance }; carsToAdd.Add(car); } context.Cars.AddRange(carsToAdd); context.SaveChanges(); foreach (var carDTO in carsDTOs) { carDTO.PartsId = carDTO .PartsId .Distinct() .ToList(); var currentCar = context .Cars .FirstOrDefault(c => c.Make == carDTO.Make && c.Model == carDTO.Model && c.TravelledDistance == carDTO.TravelledDistance); foreach (var partId in carDTO.PartsId) { if (!partIds.Contains(partId)) { continue; } var partCar = new PartCar { CarId = currentCar.Id, PartId = partId }; if (!carPartsToAdd.Contains(partCar)) { carPartsToAdd.Add(partCar); } if (carPartsToAdd.Count > 0) { currentCar.PartCars = carPartsToAdd; context.PartCars.AddRange(carPartsToAdd); carPartsToAdd.Clear(); } } } context.SaveChanges(); return($"Successfully imported {context.Cars.ToList().Count}."); }
public static void Main(string[] args) { var dbContext = new CarDealerContext(); InitializeMapper(); }
//s12. public static string ImportCustomers(CarDealerContext context, string inputXml) => ImportBulkDataFromXml <Customer>(context, inputXml, _ => true);
public static string ImportCars(CarDealerContext context, string inputXml) { return(""); }
public static void Main(string[] args) { var context = new CarDealerContext(); Console.WriteLine(GetOrderedCustomers(context)); }
public static void Main(string[] args) { CarDealerContext db = new CarDealerContext(); //ResetDatabase(db); //Problem 01 //string inputJson = File.ReadAllText("../../../Datasets/suppliers.json"); //string result=ImportSuppliers(db, inputJson); //Console.WriteLine(result); //Problem 02 //string inputJson = File.ReadAllText("../../../Datasets/parts.json"); //string result = ImportParts(db, inputJson); //Console.WriteLine(result); //Problem 03 string inputJson = File.ReadAllText("../../../Datasets/cars.json"); string result = ImportCars(db, inputJson); Console.WriteLine(result); //Problem 04 //string inputJson = File.ReadAllText("../../../Datasets/customers.json"); //string result = ImportCustomers(db, inputJson); //Console.WriteLine(result); //Problem 05 //string inputJson = File.ReadAllText("../../../Datasets/sales.json"); //string result = ImportSales(db, inputJson); //Console.WriteLine(result); //Problem 06 //EnsureDirectoryExists(); //string json = GetOrderedCustomers(db); //File.WriteAllText(ResultsDirectoryPath + "/ordered-customers.json", json); //Problem 07 //EnsureDirectoryExists(); //string json = GetCarsFromMakeToyota(db); //File.WriteAllText(ResultsDirectoryPath + "/toyota-cars.json", json); //Problem 08 //EnsureDirectoryExists(); //string json = GetLocalSuppliers(db); //File.WriteAllText(ResultsDirectoryPath + "/local-suppliers.json", json); //--Import Parts //ImportPartCars(db); //Problem 09 //EnsureDirectoryExists(); //string json = GetCarsWithTheirListOfParts(db); //File.WriteAllText(ResultsDirectoryPath + "/cars-and-parts.json", json); //Problem 10 //InitializeMapper(); //EnsureDirectoryExists(); //string json = GetTotalSalesByCustomer(db); //File.WriteAllText(ResultsDirectoryPath + "/customers-total-sales.json", json); //Problem 11 EnsureDirectoryExists(); string json = GetSalesWithAppliedDiscount(db); File.WriteAllText(ResultsDirectoryPath + "/sales-discounts.json", json); }
public static string ImportCars(CarDealerContext context, string inputJson) {
//s10. public static string ImportSuppliers(CarDealerContext context, string inputJson) => DeserializeAddEntities <Supplier>(context, inputJson, user => { return(true); });
public static string ImportSales(CarDealerContext context, string inputJson) => DeserializeAddEntities <Sale>(context, inputJson, customer => true);
//s13. public static string ImportCustomers(CarDealerContext context, string inputJson) => DeserializeAddEntities <Customer>(context, inputJson, categoryProducts => { return(true); });
private static void BuildDatabaseAndInsertData (string suppliersJson, string partsJson, string carsJson, string customersJson, string salesJson, CarDealerContext db) { ResetDatabase(db); ImportSuppliers(db, suppliersJson); ImportParts(db, partsJson); ImportCars(db, carsJson); ImportCustomers(db, customersJson); ImportSales(db, salesJson); }
private static void ResetDb(CarDealerContext dbContext) { dbContext.Database.EnsureDeleted(); dbContext.Database.EnsureCreated(); // System.Console.WriteLine("Db reset success!!!"); }
public static void Main(string[] args) { var context = new CarDealerContext(); Console.WriteLine(GetTotalSalesByCustomer(context)); }
public static void Main(string[] args) { var dbContext = new CarDealerContext(); ResetDatabase(dbContext); //EXERCISE 9 - Import Suppliers var importSuppliersXml = File.ReadAllText("../../../Datasets/suppliers.xml"); var resultOfSuppliersImport = ImportSuppliers(dbContext, importSuppliersXml); Console.WriteLine(resultOfSuppliersImport); //EXERCISE 10 - Import Parts var importPartsXml = File.ReadAllText("../../../Datasets/parts.xml"); var resultOfPartsImport = ImportParts(dbContext, importPartsXml); Console.WriteLine(resultOfPartsImport); //EXERCISE 11 - Import Cars var importCarsXml = File.ReadAllText("../../../Datasets/cars.xml"); var resultOfCarsImport = ImportCars(dbContext, importCarsXml); Console.WriteLine(resultOfCarsImport); //EXERCISE 12 - Import Customers var importCustomersXml = File.ReadAllText("../../../Datasets/customers.xml"); var resultOfCustomersImport = ImportCustomers(dbContext, importCustomersXml); Console.WriteLine(resultOfCustomersImport); //EXERCISE 13 - Import Sales var importSalesXml = File.ReadAllText("../../../Datasets/sales.xml"); var resultOfSalesImport = ImportSales(dbContext, importSalesXml); Console.WriteLine(resultOfSalesImport); //EXERCISE 14 - Export Cars With Distance var carsWithDistanceXml = GetCarsWithDistance(dbContext); Console.WriteLine(carsWithDistanceXml); File.WriteAllText("../../../ExportedXMLs/carsWithDistance.xml", carsWithDistanceXml); //EXERCISE 15 - Export Cars From Make BMW var bmwCarsXml = GetCarsFromMakeBmw(dbContext); Console.WriteLine(bmwCarsXml); File.WriteAllText("../../../ExportedXMLs/allBMWs.xml", bmwCarsXml); //EXERCISE 16 - Export Local Suppliers var localSuppliers = GetLocalSuppliers(dbContext); Console.WriteLine(localSuppliers); File.WriteAllText("../../../ExportedXMLs/localSuppliers.xml", localSuppliers); //EXERCISE 17 - Export Cars With Their List Of Parts var carsWithTheirParts = GetCarsWithTheirListOfParts(dbContext); Console.WriteLine(carsWithTheirParts); File.WriteAllText("../../../ExportedXMLs/carsWithTheirParts.xml", carsWithTheirParts); //EXERCISE 18 - Export Total Sales By Customer var customersWithSalesAndSpents = GetTotalSalesByCustomer(dbContext); Console.WriteLine(customersWithSalesAndSpents); File.WriteAllText("../../../ExportedXMLs/customersWithSalesAndSpents.xml", customersWithSalesAndSpents); //EXERCISE 19 - Export Sales With Applied Discount var salesWithDiscount = GetSalesWithAppliedDiscount(dbContext); Console.WriteLine(salesWithDiscount); File.WriteAllText("../../../ExportedXMLs/salesWithDiscount.xml", salesWithDiscount); }
private static void ResetDatabase(CarDealerContext context) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); }
public static void Main(string[] args) { var db = new CarDealerContext(); Console.WriteLine(GetCarsWithTheirListOfParts(db)); }
public static void Main(string[] args) { var context = new CarDealerContext(); File.WriteAllText("../../../Datasets/ExportXMLs/sales-discounts.xml", GetSalesWithAppliedDiscount(context)); }
public static void Main(string[] args) { var db = new CarDealerContext(); //ResetDatabase(db); //exercise 8 //string inputSup = File.ReadAllText(@"..\..\..\Datasets\suppliers.json"); //string result = ImportSuppliers(db, inputSup); //Console.WriteLine(result); //exercise 9 //string input = File.ReadAllText(@"..\..\..\Datasets\parts.json"); //string result = ImportParts(db, input); //Console.WriteLine(result); //exercise 10 //string input = File.ReadAllText(@"..\..\..\Datasets\cars.json"); //string result = ImportCars(db, input); //Console.WriteLine(result); //exercise 11 //string input = File.ReadAllText(@"..\..\..\Datasets\customers.json"); //string result = ImportCustomers(db, input); //Console.WriteLine(result); //exercise 12 //string input = File.ReadAllText(@"..\..\..\Datasets\sales.json"); //string result = ImportSales(db, input); //Console.WriteLine(result); //exercise 13 //string result = GetOrderedCustomers(db); //Console.WriteLine(result); ////exercise 14 //string result = GetCarsFromMakeToyota(db); //Console.WriteLine(result); //////exercise 15 //string result = GetLocalSuppliers(db); //Console.WriteLine(result); //exercise 16 //string result = GetCarsWithTheirListOfParts(db); //Console.WriteLine(result); //exercise 17 //string result = GetTotalSalesByCustomer(db); //Console.WriteLine(result); //exercise 18 string result = GetSalesWithAppliedDiscount(db); Console.WriteLine(result); }
public static void Main(string[] args) { var context = new CarDealerContext(); Console.WriteLine(GetCarsWithDistance(context)); }
private static void ResetDatabase(CarDealerContext db) { db.Database.EnsureDeleted(); db.Database.EnsureCreated(); }
public static string GetSalesWithAppliedDiscount(CarDealerContext context) { }
public Deserializer(CarDealerContext context) { this.context = context; }
public static void Main(string[] args) { var context = new CarDealerContext(); Console.WriteLine(GetLocalSuppliers(context)); }
public static void Main(string[] args) { CarDealerContext context = new CarDealerContext(); //ResetDatabase(context); #region 09.Import Suppliers //string inputJson = File.ReadAllText("../../../Datasets/suppliers.json"); //string result = ImportSuppliers(context, inputJson); #endregion #region 10. Import Parts //string inputJson = File.ReadAllText("../../../Datasets/parts.json"); //string result = ImportParts(context, inputJson); #endregion #region 11. Import Cars //string inputJson = File.ReadAllText("../../../Datasets/cars.json"); //string result = ImportCars(context, inputJson); #endregion #region 12. Import Customers //string inputJson = File.ReadAllText("../../../Datasets/customers.json"); //string result = ImportCustomers(context, inputJson); #endregion #region 13. Import Sales //string inputJson = File.ReadAllText("../../../Datasets/sales.json"); //string result = ImportSales(context, inputJson); #endregion #region 14. Export Ordered Customers //string json = GetOrderedCustomers(context); //EnsureDirectoryExists(ResultDirectoryPath); //File.WriteAllText(ResultDirectoryPath + "/ordered-customers.json", json); #endregion #region 15. Export Cars From Make Toyota //string json = GetCarsFromMakeToyota(context); //EnsureDirectoryExists(ResultDirectoryPath); //File.WriteAllText(ResultDirectoryPath + "/toyota-cars.json", json); #endregion #region 16. Export Local Suppliers //string json = GetLocalSuppliers(context); //EnsureDirectoryExists(ResultDirectoryPath); //File.WriteAllText(ResultDirectoryPath + "/local-suppliers.json", json); #endregion #region 17. Export Cars With Their List Of Parts //string json = GetCarsWithTheirListOfParts(context); //EnsureDirectoryExists(ResultDirectoryPath); //File.WriteAllText(ResultDirectoryPath + "/cars-and-parts.json", json); #endregion #region 18. Export Total Sales By Customer //string json = GetTotalSalesByCustomer(context); //EnsureDirectoryExists(ResultDirectoryPath); //File.WriteAllText(ResultDirectoryPath + "/customers-total-sales.json", json); #endregion #region 19. Export Sales With Applied Discount //string json = GetSalesWithAppliedDiscount(context); //EnsureDirectoryExists(ResultDirectoryPath); //File.WriteAllText(ResultDirectoryPath + "/sales-discounts.json", json); #endregion //Console.WriteLine(json); //Console.WriteLine(result); }
//Problem 11 - Import Cars public static string ImportCars(CarDealerContext context, string inputJson) { var carsImport = JsonConvert.DeserializeObject <CarImportDto[]>(inputJson); var carsToAdd = Mapper.Map <CarImportDto[], Car[]>(carsImport); context.AddRange(carsToAdd); context.SaveChanges(); HashSet <int> partIds = context.Parts.Select(x => x.Id).ToHashSet(); HashSet <PartCar> carPartsToAdd = new HashSet <PartCar>(); foreach (var car in carsImport) { car.PartsId = car .PartsId .Distinct() .ToList(); Car currentCar = context. 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 (!carPartsToAdd.Contains(partCar)) { carPartsToAdd.Add(partCar); } } if (carPartsToAdd.Count > 0) { currentCar.PartCars = carPartsToAdd; context.PartCars.AddRange(carPartsToAdd); carPartsToAdd.Clear(); } } context.SaveChanges(); return($"Successfully imported {context.Cars.ToList().Count}."); }
public static void Main() { const string Root = @"../../../Datasets/"; const string CarsXml = "cars.xml"; const string CustomersXml = "customers.xml"; const string PartsXml = "parts.xml"; const string SalesXml = "sales.xml"; const string SuppliersXml = "suppliers.xml"; Mapper.Initialize(cfg => { cfg.AddProfile <CarDealerProfile>(); }); try { using (var context = new CarDealerContext()) { // Database initialize. //context.Database.EnsureCreated(); //Console.WriteLine("Database created successfully!"); // III. Import data // 09. ImportSuppliers //string result = ImportSuppliers(context, File.ReadAllText(Root + SuppliersXml)); // 10. Import Parts //string result = ImportParts(context, File.ReadAllText(Root + PartsXml)); // 11. Import Cars //string result = ImportCars(context, File.ReadAllText(Root + CarsXml)); // 12. Import Customers //string result = ImportCustomers(context, File.ReadAllText(Root + CustomersXml)); // 13. Import Sales //string result = ImportSales(context, File.ReadAllText(Root + SalesXml)); // IV. Query and Export Data // 14. Export Cars With Distance //string result = GetCarsWithDistance(context); // 15. Export Cars From Make BMW //string result = GetCarsFromMakeBmw(context); // 16. Export Local Suppliers //string result = GetLocalSuppliers(context); // 17.Export Cars With Their List Of Parts //string result = GetCarsWithTheirListOfParts(context); // 18. Export Total Sales By Customer //string result = GetTotalSalesByCustomer(context); // 19. Export Sales With Applied Discount string result = GetSalesWithAppliedDiscount(context); Console.WriteLine(result); } } catch (Exception e) { Console.WriteLine(e.Message); } }