Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace JSONProcessing
- {
- using Data.ProductsShop;
- using Models.ProductsShop;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Data.Entity;
- using System.Linq;
- using Data.CarDealer;
- using Models.CarDealer;
- using AutoMapper.QueryableExtensions;
- using DTOs;
- using AutoMapper;
- using MappingProfiles;
- class Startup
- {
- static void Main(string[] args)
- {
- InitializeAutoMapper();
- ProductsShopContext context = new ProductsShopContext();
- //context.Database.Initialize(true);
- //Console.WriteLine(context.Categories.Count());
- //Query1
- //SerializeProductsInRange(context);
- //Query1 with DTO
- //var products = context.Products
- // .Where(p => p.Price >= 500 && p.Price <= 1000 && p.BuyerId == null)
- // .ProjectTo<ProductsInRangeDTO>()
- // .ToList()
- // .OrderBy(p => p.Price);
- //Exporter.Export(products, "products-in-range-DTO");
- //
- //Query2
- //SerializeSuccessfullySoldProducts(context);
- //Query3
- //CategoriesByProductsCount(context);
- //Query4
- //UsersAndProducts(context);
- CarDealerContext carContext = new CarDealerContext();
- carContext.Configuration.ProxyCreationEnabled = false;
- //carContext.Database.Initialize(true);
- //Console.WriteLine(carContext.Sales.Count());
- //Query5
- //OrderedCustomers(carContext);
- //Query6
- //CarsFromMakeToyota(carContext);
- //Query7
- //LocalSuppliers(carContext);
- //Query8
- //CarsWithParts(carContext);
- //Query9
- //TotalSalesByCustomer(carContext);
- //Query10
- //SalesWithAppliedDiscount(carContext);
- }
- private static void InitializeAutoMapper()
- {
- Mapper.Initialize(cfg =>
- {
- cfg.AddProfile<ProductsInRangeDtoProfile>();
- cfg.AddProfile<SuccessfullySoldProductsDtoProfile>();
- });
- }
- private static void SalesWithAppliedDiscount(CarDealerContext carContext)
- {
- var sales = carContext.Sales
- .Select(s => new
- {
- Car = new
- {
- Make = s.Car.Make,
- Model = s.Car.Model,
- TravelledDistance = s.Car.TravelledDistance
- },
- CustomerName = s.Customer.Name,
- Discount = s.Discount,
- Price = s.Car.Parts.Sum(p => p.Price),
- PriceWithDiscount = s.Car.Parts.Sum(p => p.Price) - (s.Car.Parts.Sum(p => p.Price) * s.Discount)
- })
- .ToList();
- Exporter.Export(sales, "sales-discounts");
- }
- private static void TotalSalesByCustomer(CarDealerContext carContext)
- {
- var customers = carContext.Customers
- .Where(c => c.Sales.Count() >= 1)
- .Select(c => new
- {
- c.Name,
- BoughtCars = c.Sales.Count(),
- SpentMoney = c.Sales
- .Sum(s => s.Car.Parts
- .Sum(p => p.Price) - (s.Car.Parts
- .Sum(p => p.Price) * s.Discount))
- })
- .ToList()
- .OrderByDescending(c => c.SpentMoney)
- .ThenByDescending(c => c.BoughtCars);
- Exporter.Export(customers, "customers-total-sales");
- }
- private static void CarsWithParts(CarDealerContext carContext)
- {
- var cars = carContext.Cars
- .Include(c => c.Parts)
- .Select(c => new
- {
- Car = new
- {
- Make = c.Make,
- Model = c.Model,
- TravelledDistance = c.TravelledDistance,
- },
- Parts = c.Parts.Select(p => new
- {
- p.Name,
- p.Price
- })
- })
- .ToList();
- Exporter.Export(cars, "cars-and-parts");
- }
- private static void LocalSuppliers(CarDealerContext carContext)
- {
- var suppliers = carContext.Suppliers
- .Where(s => !s.IsImporter)
- .Select(s => new
- {
- Id = s.Id,
- Name = s.Name,
- PartsCount = s.Parts.Count()
- })
- .ToList();
- Exporter.Export(suppliers, "local-suppliers");
- }
- private static void CarsFromMakeToyota(CarDealerContext carContext)
- {
- var cars = carContext.Cars
- .Where(c => c.Make == "Toyota")
- .Select(c => new
- {
- Id = c.Id,
- Make = c.Make,
- Model = c.Model,
- TravelledDistance = c.TravelledDistance
- })
- .OrderBy(c => c.Model)
- .ThenByDescending(c => c.TravelledDistance)
- .ToList();
- Exporter.Export(cars, "toyota-cars");
- }
- private static void OrderedCustomers(CarDealerContext carContext)
- {
- var customers = carContext.Customers
- .OrderBy(c => c.Birthdate)
- .ThenBy(c => c.IsYoungDriver)
- .Select(c => new
- {
- Id = c.Id,
- Name = c.Name,
- BirthDate = c.Birthdate,
- IsYoungDriver = c.IsYoungDriver,
- Sales = c.Sales.Select(s => new { s.Id, s.CustomerId, s.CarId, s.Discount })
- })
- .ToList();
- Exporter.Export(customers, "ordered-customers");
- }
- private static void UsersAndProducts(ProductsShopContext context)
- {
- var users = context.Users
- .Include(u => u.SoldProducts)
- .Where(u => u.SoldProducts.Count() > 0)
- .Select(u => new
- {
- FirstName = u.FirstName,
- LastName = u.LastName,
- Age = u.Age,
- SoldProducts = new
- {
- Count = u.SoldProducts.Count(),
- Products = u.SoldProducts.Select(p => new
- {
- p.Name,
- p.Price
- })
- }
- })
- .ToList()
- .OrderByDescending(u => u.SoldProducts.Count)
- .ThenBy(u => u.LastName);
- Exporter.Export(new { UsersCount = users.Count(), Users = users }, "users-and-products");
- }
- private static void CategoriesByProductsCount(ProductsShopContext context)
- {
- var categories = context.Categories
- .Include(c => c.Products)
- .Include(c => c.Products.Select(p => p.Price))
- .OrderBy(c => c.Products.Count())
- .Select(c => new
- {
- Name = c.Name,
- ProductsCount = c.Products.Count(),
- AveragePrice = c.Products.Average(p => p.Price),
- TotalRevenue = c.Products.Sum(p => p.Price)
- })
- .ToList();
- Exporter.Export(categories, "categories-by-products");
- }
- private static void SerializeSuccessfullySoldProducts(ProductsShopContext context)
- {
- var users = context.Users
- .Include(u => u.SoldProducts)
- .Include(u => u.SoldProducts.Select(s => s.Buyer))
- .Where(u => u.SoldProducts.Any(p => p.Buyer != null))
- .Select(u => new
- {
- FirstName = u.FirstName,
- LastName = u.LastName,
- SoldProducts = u.SoldProducts.Where(p => p.Buyer != null).Select(p => new
- {
- Name = p.Name,
- Price = p.Price,
- BuyerFirstName = p.Buyer.FirstName,
- BuyerLastName = p.Buyer.LastName
- })
- })
- .ToList()
- .OrderBy(u => u.LastName)
- .ThenBy(u => u.FirstName);
- Exporter.Export(users, "users-sold-products");
- }
- private static void SerializeProductsInRange(ProductsShopContext context)
- {
- var products = context.Products
- .Include("Seller")
- .Where(p => p.Price >= 500 && p.Price <= 1000 && p.BuyerId == null)
- .OrderBy(p => p.Price)
- .Select(p => new
- {
- Name = p.Name,
- Price = p.Price,
- Seller = (p.Seller.FirstName + " " + p.Seller.LastName).Trim()
- })
- .ToList();
- Exporter.Export(products, "products-in-range");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement