public static void ImportCustomers(CarDealershipContext context, string suppliersAsString) { var customersListDto = new List <CustomerDto>(); var customers = new List <Customer>(); XmlSerializer serializer = new XmlSerializer(typeof(List <CustomerDto>), new XmlRootAttribute("customers")); using (TextReader reader = new StringReader(suppliersAsString)) { customersListDto = (List <CustomerDto>)serializer.Deserialize(reader); } foreach (var item in customersListDto) { var customer = new Customer() { Name = item.Name, BirthDate = item.BirthDate, IsYoungDriver = item.IsYoungDriver }; customers.Add(customer); } context.Customers.AddRange(customers); context.SaveChanges(); }
public static string GetCarsWithListOfParts(CarDealershipContext context) { var cars = context.Cars .Select(e => new CarWithPartsDto { Make = e.Make, Model = e.Model, TravelledDistance = e.TravelledDistance, Parts = e.CarParts.Select(x => new PartListDto() { Name = x.Part.Name, Price = x.Part.Price }).ToList() }) .OrderBy(e => e.Make) .ThenBy(e => e.Model) .ToArray(); var xmlNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }); XmlSerializer serializer = new XmlSerializer(typeof(CarWithPartsDto[]), new XmlRootAttribute("cars")); var sb = new StringBuilder(); serializer.Serialize(new StringWriter(sb), cars, xmlNamespaces); return(sb.ToString()); }
public static void ImportParts(CarDealershipContext context, string partsAsString) { var partListDto = new List <PartDto>(); var parts = new List <Part>(); XmlSerializer serializer = new XmlSerializer(typeof(List <PartDto>), new XmlRootAttribute("parts")); using (TextReader reader = new StringReader(partsAsString)) { partListDto = (List <PartDto>)serializer.Deserialize(reader); } var random = new Random(); var suppliersId = context.Suppliers.Select(x => x.Id).ToArray(); foreach (var item in partListDto) { var part = new Part() { Name = item.Name, Price = item.Price, Quantity = item.Quantity, SupplierId = random.Next(1, suppliersId.Count()) }; parts.Add(part); } context.Parts.AddRange(parts); context.SaveChanges(); }
//Basic operations// //Get operations// /// <summary> /// Find all workers in the database /// </summary> /// <returns>Returns a list of all workers in the database</returns> public List <Worker> GetAllWorkers() { using (workerContext = new CarDealershipContext()) { return(workerContext.Workers.ToList()); } }
private static void AddCar() { var context = new CarDealershipContext(); try { var car = new Car { Brand = "Tesla", Model = "ModelX", Engine = 2.0, TypeOfCar = "Electric", HorsePower = 600, HasElectricWindows = true, HasNavigation = true }; context.CarContext.Add(car); context.SaveChanges(); } finally { context.Dispose(); } }
public static void ImportSuppliers(CarDealershipContext context, string suppliersAsString) { var suppliers = JsonConvert.DeserializeObject <Supplier[]>(suppliersAsString); context.Suppliers.AddRange(suppliers); context.SaveChanges(); }
public static void ImportSales(CarDealershipContext context) { HashSet <Sale> sales = new HashSet <Sale>(); List <decimal> discount = new List <decimal>() { 0.0m, 0.05m, 0.1m, 0.15m, 0.20m, 0.3m, 0.4m, 0.5m }; int[] carsById = context.Cars.Select(x => x.Id).ToArray(); int[] customerById = context.Customers.Select(x => x.Id).ToArray(); var random = new Random(); for (int i = 0; i <= 500; i++) { Sale sale = new Sale() { CarId = carsById[random.Next(0, carsById.Length)], CustomerId = customerById[random.Next(0, customerById.Length)], Discount = discount[random.Next(0, discount.Count)] }; sales.Add(sale); } context.Sales.AddRange(sales); context.SaveChanges(); }
/// <summary> /// Sorts workers /// </summary> /// <returns>List of workers sorted by dealership name in ascending order</returns> public List <Worker> SortWorkersByCarDealership() { using (workerContext = new CarDealershipContext()) { return(workerContext.Workers.OrderBy(x => x.CarDealership.Name).ToList()); } }
public static string GetCarsWithListOfParts(CarDealershipContext context) { var cars = context.Cars .Select(e => new { Make = e.Make, Model = e.Model, TravelledDistance = e.TravelledDistance, Parts = e.CarParts.Select(x => new { Name = x.Part.Name, Price = x.Part.Price }).ToArray() }) .ToArray(); var carsJson = JsonConvert.SerializeObject(cars, new JsonSerializerSettings { Formatting = Formatting.Indented, DateFormatHandling = DateFormatHandling.IsoDateFormat, NullValueHandling = NullValueHandling.Ignore }); return(carsJson); }
public static void ImportCars(CarDealershipContext context, string carsAsString) { var cars = JsonConvert.DeserializeObject <Car[]>(carsAsString); context.Cars.AddRange(cars); context.SaveChanges(); var partsId = context.Parts.Select(x => x.Id).ToArray(); var carsId = context.Cars.Select(x => x.Id).ToArray(); var random = new Random(); var carsParts = new List <PartCar>(); for (int i = 0; i < carsId.Length; i++) { for (int j = 0; j < random.Next(15, 20); j++) { var carPart = new PartCar() { CarId = carsId[i], PartId = partsId[random.Next(1, partsId.Length)] }; if (carsParts.Any(x => x.CarId == carPart.CarId && x.PartId == carPart.PartId)) { continue; } carsParts.Add(carPart); } } context.PartCars.AddRange(carsParts); context.SaveChanges(); }
public static void ImportCustomers(CarDealershipContext context, string customersAsString) { var customers = JsonConvert.DeserializeObject <Customer[]>(customersAsString); context.Customers.AddRange(customers); context.SaveChanges(); }
public static void ImportSuppliers(CarDealershipContext context, string suppliersAsString) { var suppliersListDto = new List <SupplierDto>(); var suppliers = new List <Supplier>(); XmlSerializer serializer = new XmlSerializer(typeof(List <SupplierDto>), new XmlRootAttribute("suppliers")); using (TextReader reader = new StringReader(suppliersAsString)) { suppliersListDto = (List <SupplierDto>)serializer.Deserialize(reader); } foreach (var item in suppliersListDto) { var suplier = new Supplier() { Name = item.Name, IsImporter = item.IsImporter }; suppliers.Add(suplier); } context.Suppliers.AddRange(suppliers); context.SaveChanges(); }
private static void DeleteCar() { using (var context = new CarDealershipContext()) { var student = (from d in context.CarContext where d.Model == "ModelX" select d).Single(); context.CarContext.Remove(student); context.SaveChanges(); } }
private static void InitializeDatabase() { var context = new CarDealershipContext(); using (context) { context.Database.EnsureDeleted(); context.Database.EnsureCreated(); } }
private static void ChangeCar() { using var context = new CarDealershipContext(); var car = (from d in context.CarContext where d.Brand == "Tesla" select d).Single(); car.Model = "ModelS"; context.SaveChanges(); }
public static string GetSalesWithAppliedDicount(CarDealershipContext context) { var sales = context.Sales .Include(e => e.Customer) .Include(e => e.Car) .ThenInclude(e => e.CarParts) .ThenInclude(e => e.Part) .Where(e => e.Discount > 0) .ToArray(); List <SalesWithAppliedDiscount> discountSales = new List <SalesWithAppliedDiscount>(); foreach (var sale in sales) { var currentSale = new SalesWithAppliedDiscount(); currentSale.CustomerName = sale.Customer.Name; currentSale.IsYoungDriver = sale.Customer.IsYoungDriver; currentSale.Car.Model = sale.Car.Model; currentSale.Car.Make = sale.Car.Make; currentSale.Car.TravelledDistance = sale.Car.TravelledDistance; if (currentSale.IsYoungDriver) { currentSale.Discount = sale.Discount + 0.05; } else { currentSale.Discount = sale.Discount; } decimal partsPrice = 0; foreach (var carPart in sale.Car.CarParts) { var currentPartPrice = carPart.Part.Price; partsPrice += currentPartPrice; } currentSale.Price = partsPrice; currentSale.DiscountPrice = (1m - (decimal)currentSale.Discount) * partsPrice; discountSales.Add(currentSale); } var xmlNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }); XmlSerializer serializer = new XmlSerializer(typeof(List <SalesWithAppliedDiscount>), new XmlRootAttribute("sales")); var sb = new StringBuilder(); serializer.Serialize(new StringWriter(sb), discountSales, xmlNamespaces); return(sb.ToString()); }
public CustomerController(CarDealershipContext context) { db = context; if (!db.Customer.Any()) { db.Customer.Add(new Customer { Id = 1, FirstName = "Admin", LastName = "Admin", Email = "*****@*****.**", Password = "******", Role = "admin" }); db.SaveChanges(); } }
public static void ImportCars(CarDealershipContext context, string carsAsString) { var carListDto = new List <CarDto>(); var carsParts = new List <PartCar>(); var cars = new List <Car>(); XmlSerializer serializer = new XmlSerializer(typeof(List <CarDto>), new XmlRootAttribute("cars")); using (TextReader reader = new StringReader(carsAsString)) { carListDto = (List <CarDto>)serializer.Deserialize(reader); } var random = new Random(); foreach (var item in carListDto) { var car = new Car() { Make = item.Make, Model = item.Model, TravelledDistance = item.TravelDistance, }; cars.Add(car); } context.Cars.AddRange(cars); context.SaveChanges(); var partsId = context.Parts.Select(x => x.Id).ToArray(); var carsId = context.Cars.Select(x => x.Id).ToArray(); for (int i = 0; i < carsId.Length; i++) { for (int j = 0; j < random.Next(15, 20); j++) { var carPart = new PartCar() { CarId = carsId[i], PartId = partsId[random.Next(1, partsId.Length)] }; if (carsParts.Any(x => x.CarId == carPart.CarId && x.PartId == carPart.PartId)) { continue; } carsParts.Add(carPart); } } context.PartCars.AddRange(carsParts); context.SaveChanges(); }
public static string TotalSalesByCustomer(CarDealershipContext context) { var customers = context.Customers .Include(e => e.Sales) .ThenInclude(e => e.Car) .ThenInclude(e => e.CarParts) .ThenInclude(e => e.Part) .Where(x => x.Sales.Count > 0) .ToArray(); List <TotalSalesByCustomerDto> salesByCustomer = new List <TotalSalesByCustomerDto>(); foreach (var customer in customers) { var totalSalesByCustomer = new TotalSalesByCustomerDto(); totalSalesByCustomer.FullName = customer.Name; totalSalesByCustomer.BoughtCars = customer.Sales.Count; var spentMoney = 0M; foreach (var sale in customer.Sales) { var partsPrice = 0m; var saleDiscount = sale.Discount; var salePrice = 0m; foreach (var part in sale.Car.CarParts.Select(x => x.Part.Price)) { partsPrice += part; } salePrice = (1m - (decimal)saleDiscount) * partsPrice; spentMoney += salePrice; } totalSalesByCustomer.MoneySpent = Math.Round(spentMoney, 2); salesByCustomer.Add(totalSalesByCustomer); } salesByCustomer = salesByCustomer .OrderByDescending(x => x.MoneySpent) .ThenByDescending(x => x.BoughtCars).ToList(); var xmlNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }); XmlSerializer serializer = new XmlSerializer(typeof(List <TotalSalesByCustomerDto>), new XmlRootAttribute("customers")); var sb = new StringBuilder(); serializer.Serialize(new StringWriter(sb), salesByCustomer, xmlNamespaces); return(sb.ToString()); }
public static void ImportParts(CarDealershipContext context, string partsAsString) { var parts = JsonConvert.DeserializeObject <Part[]>(partsAsString); var random = new Random(); var suppliersId = context.Suppliers.Select(x => x.Id).ToArray(); foreach (var item in parts) { item.SupplierId = suppliersId[random.Next(0, suppliersId.Count())]; } context.Parts.AddRange(parts); context.SaveChanges(); }
private static void ReadCar() { using (var db = new CarDealershipContext()) { var query = from b in db.CarContext orderby b.Model select b; Console.WriteLine("All All student in the database:"); foreach (var item in query) { Console.WriteLine(item.Brand + " " + item.Model); } Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } }
public static string GetSalesWithAppliedDicount(CarDealershipContext context) { var sales = context.Sales .Include(e => e.Customer) .Include(e => e.Car) .ThenInclude(e => e.CarParts) .ThenInclude(e => e.Part) .Where(e => e.Discount > 0) .ToArray(); List <SalesWithAppliedDiscount> discountSales = new List <SalesWithAppliedDiscount>(); foreach (var sale in sales) { var currentSale = new SalesWithAppliedDiscount(); currentSale.customerName = sale.Customer.Name; currentSale.Car.Model = sale.Car.Model; currentSale.Car.Make = sale.Car.Make; currentSale.Car.TravelledDistance = sale.Car.TravelledDistance; currentSale.discount = sale.Discount; decimal partsPrice = 0; foreach (var carPart in sale.Car.CarParts) { var currentPartPrice = carPart.Part.Price; partsPrice += currentPartPrice; } currentSale.price = partsPrice; currentSale.priceWithDiscount = (1m - (decimal)currentSale.discount) * partsPrice; discountSales.Add(currentSale); } var discountSalesString = JsonConvert.SerializeObject(discountSales, new JsonSerializerSettings { Formatting = Formatting.Indented, }); return(discountSalesString); }
private static void ImportData() { using (var context = new CarDealershipContext()) { string suppliersAsString = File.ReadAllText(@"..\..\..\ImportJson\suppliers.json"); Deserializer.ImportSuppliers(context, suppliersAsString); string partsAsString = File.ReadAllText(@"..\..\..\ImportJson\parts.json"); Deserializer.ImportParts(context, partsAsString); string carsasstring = File.ReadAllText(@"..\..\..\importjson\cars.json"); Deserializer.ImportCars(context, carsasstring); string customersAsString = File.ReadAllText(@"..\..\..\ImportJson\customers.json"); Deserializer.ImportCustomers(context, customersAsString); Deserializer.ImportSales(context); } }
public static string GetLocalSuppliers(CarDealershipContext context) { var suppliers = context.Suppliers .Where(e => e.IsImporter == false) .Select(e => new LocalSupplierDto() { Id = e.Id, Name = e.Name, PartsCount = e.Parts.Count }) .OrderByDescending(e => e.PartsCount) .ToArray(); var xmlNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }); XmlSerializer serializer = new XmlSerializer(typeof(LocalSupplierDto[]), new XmlRootAttribute("supliers")); var sb = new StringBuilder(); serializer.Serialize(new StringWriter(sb), suppliers, xmlNamespaces); return(sb.ToString()); }
private static void ExportData() { using (var context = new CarDealershipContext()) { string carsWithDistance = Serializer.OrderedCustomers(context); File.WriteAllText(@"..\..\..\ExportJson\ordered-customers.json", carsWithDistance, Encoding.UTF8); string toyotaCars = Serializer.GetToyotaCars(context); File.WriteAllText(@"..\..\..\ExportJson\toyota-cars.json", toyotaCars, Encoding.UTF8); string localSuppliers = Serializer.GetLocalSuppliers(context); File.WriteAllText(@"..\..\..\ExportJson\local-suppliers.json", localSuppliers, Encoding.UTF8); string carsAndParts = Serializer.GetCarsWithListOfParts(context); File.WriteAllText(@"..\..\..\ExportJson\cars-and-parts.json", carsAndParts, Encoding.UTF8); string salesByCustomers = Serializer.TotalSalesByCustomer(context); File.WriteAllText(@"..\..\..\ExportJson\customers-total-sales.json", salesByCustomers, Encoding.UTF8); string salesWithApliedDiscount = Serializer.GetSalesWithAppliedDicount(context); File.WriteAllText(@"..\..\..\ExportJson\sales-discounts.json", salesWithApliedDiscount, Encoding.UTF8); } }
public static string GetLocalSuppliers(CarDealershipContext context) { var suppliers = context.Suppliers .Where(e => e.IsImporter == false) .Select(e => new LocalSupplierDto() { Id = e.Id, Name = e.Name, PartsCount = e.Parts.Count }) .OrderByDescending(e => e.PartsCount) .OrderBy(e => e.Name) .ThenByDescending(e => e.PartsCount) .ToArray(); var localSuppliers = JsonConvert.SerializeObject(suppliers, new JsonSerializerSettings { Formatting = Formatting.Indented, NullValueHandling = NullValueHandling.Ignore }); return(localSuppliers); }
public static string GetToyotaCars(CarDealershipContext context) { var cars = context.Cars .Where(e => e.Make == "Toyota") .OrderBy(e => e.Model) .ThenByDescending(e => e.TravelledDistance) .Select(e => new { Id = e.Id, Make = e.Make, Model = e.Model, TravelledDistance = e.TravelledDistance }) .ToArray(); var toyotaCars = JsonConvert.SerializeObject(cars, new JsonSerializerSettings { Formatting = Formatting.Indented, NullValueHandling = NullValueHandling.Ignore }); return(toyotaCars); }
private static void ExportData() { using (var context = new CarDealershipContext()) { string carsWithDistance = Serializer.GetCarsWithDistance(context); File.WriteAllText(@"..\..\..\ExportXml\cars.xml", carsWithDistance, Encoding.UTF8); string ferrariCars = Serializer.GetFerrariCars(context); File.WriteAllText(@"..\..\..\ExportXml\ferrari-cars.xml", ferrariCars, Encoding.UTF8); string localSuppliers = Serializer.GetLocalSuppliers(context); File.WriteAllText(@"..\..\..\ExportXml\local-suppliers.xml", localSuppliers, Encoding.UTF8); string carsAndParts = Serializer.GetCarsWithListOfParts(context); File.WriteAllText(@"..\..\..\ExportXml\cars-and-parts.xml", carsAndParts, Encoding.UTF8); string salesByCustomers = Serializer.TotalSalesByCustomer(context); File.WriteAllText(@"..\..\..\ExportXml\customers-total-sales.xml", salesByCustomers, Encoding.UTF8); string salesWithApliedDiscount = Serializer.GetSalesWithAppliedDicount(context); File.WriteAllText(@"..\..\..\ExportXml\sales-discounts.xml", salesWithApliedDiscount, Encoding.UTF8); } }
public static string GetFerrariCars(CarDealershipContext context) { var cars = context.Cars .Where(e => e.Make == "Ferrari") .OrderBy(e => e.Model) .ThenByDescending(e => e.TravelledDistance) .Select(e => new FerrariCarsDto { Id = e.Id, Model = e.Model, TravelledDistance = e.TravelledDistance }) .ToArray(); var xmlNamespaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty }); XmlSerializer serializer = new XmlSerializer(typeof(FerrariCarsDto[]), new XmlRootAttribute("cars")); var sb = new StringBuilder(); serializer.Serialize(new StringWriter(sb), cars, xmlNamespaces); return(sb.ToString()); }
public static string OrderedCustomers(CarDealershipContext context) { var customers = context.Customers .OrderBy(x => x.BirthDate) .ThenBy(x => x.IsYoungDriver) .Select(x => new { Id = x.Id, Name = x.Name, BirthDate = x.BirthDate, IsYoungDriver = x.IsYoungDriver, Sales = x.Sales }).ToArray(); var customersJson = JsonConvert.SerializeObject(customers, new JsonSerializerSettings { Formatting = Formatting.Indented, DateFormatHandling = DateFormatHandling.IsoDateFormat, NullValueHandling = NullValueHandling.Ignore }); return(customersJson); }