Advertisement
Guest User

Untitled

a guest
Apr 4th, 2017
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.62 KB | None | 0 0
  1. namespace JSONProcessing
  2. {
  3.     using Data.ProductsShop;
  4.     using Models.ProductsShop;
  5.     using Newtonsoft.Json;
  6.     using System;
  7.     using System.Collections.Generic;
  8.     using System.IO;
  9.     using System.Data.Entity;
  10.     using System.Linq;
  11.     using Data.CarDealer;
  12.     using Models.CarDealer;
  13.     using AutoMapper.QueryableExtensions;
  14.     using DTOs;
  15.     using AutoMapper;
  16.     using MappingProfiles;
  17.  
  18.     class Startup
  19.     {
  20.         static void Main(string[] args)
  21.         {
  22.             InitializeAutoMapper();
  23.  
  24.             ProductsShopContext context = new ProductsShopContext();
  25.             //context.Database.Initialize(true);
  26.             //Console.WriteLine(context.Categories.Count());
  27.  
  28.             //Query1
  29.             //SerializeProductsInRange(context);
  30.  
  31.  
  32.             //Query1 with DTO
  33.             //var products = context.Products
  34.             //    .Where(p => p.Price >= 500 && p.Price <= 1000 && p.BuyerId == null)
  35.             //    .ProjectTo<ProductsInRangeDTO>()
  36.             //    .ToList()
  37.             //    .OrderBy(p => p.Price);
  38.  
  39.             //Exporter.Export(products, "products-in-range-DTO");
  40.             //
  41.  
  42.             //Query2
  43.             //SerializeSuccessfullySoldProducts(context);
  44.  
  45.             //Query3
  46.             //CategoriesByProductsCount(context);
  47.  
  48.             //Query4
  49.             //UsersAndProducts(context);
  50.  
  51.             CarDealerContext carContext = new CarDealerContext();
  52.             carContext.Configuration.ProxyCreationEnabled = false;
  53.             //carContext.Database.Initialize(true);
  54.             //Console.WriteLine(carContext.Sales.Count());
  55.  
  56.             //Query5
  57.             //OrderedCustomers(carContext);
  58.  
  59.             //Query6
  60.             //CarsFromMakeToyota(carContext);
  61.  
  62.             //Query7
  63.             //LocalSuppliers(carContext);
  64.  
  65.             //Query8
  66.             //CarsWithParts(carContext);
  67.  
  68.             //Query9
  69.             //TotalSalesByCustomer(carContext);
  70.  
  71.             //Query10
  72.             //SalesWithAppliedDiscount(carContext);
  73.         }
  74.  
  75.         private static void InitializeAutoMapper()
  76.         {
  77.             Mapper.Initialize(cfg =>
  78.             {
  79.                 cfg.AddProfile<ProductsInRangeDtoProfile>();
  80.                 cfg.AddProfile<SuccessfullySoldProductsDtoProfile>();
  81.             });
  82.         }
  83.  
  84.         private static void SalesWithAppliedDiscount(CarDealerContext carContext)
  85.         {
  86.             var sales = carContext.Sales
  87.                             .Select(s => new
  88.                             {
  89.                                 Car = new
  90.                                 {
  91.                                     Make = s.Car.Make,
  92.                                     Model = s.Car.Model,
  93.                                     TravelledDistance = s.Car.TravelledDistance
  94.                                 },
  95.                                 CustomerName = s.Customer.Name,
  96.                                 Discount = s.Discount,
  97.                                 Price = s.Car.Parts.Sum(p => p.Price),
  98.                                 PriceWithDiscount = s.Car.Parts.Sum(p => p.Price) - (s.Car.Parts.Sum(p => p.Price) * s.Discount)
  99.                             })
  100.                             .ToList();
  101.  
  102.             Exporter.Export(sales, "sales-discounts");
  103.         }
  104.  
  105.         private static void TotalSalesByCustomer(CarDealerContext carContext)
  106.         {
  107.             var customers = carContext.Customers
  108.                 .Where(c => c.Sales.Count() >= 1)
  109.                 .Select(c => new
  110.                 {
  111.                     c.Name,
  112.                     BoughtCars = c.Sales.Count(),
  113.                     SpentMoney = c.Sales
  114.                         .Sum(s => s.Car.Parts
  115.                                        .Sum(p => p.Price) - (s.Car.Parts
  116.                                                                   .Sum(p => p.Price) * s.Discount))
  117.                 })
  118.                 .ToList()
  119.                 .OrderByDescending(c => c.SpentMoney)
  120.                 .ThenByDescending(c => c.BoughtCars);
  121.  
  122.             Exporter.Export(customers, "customers-total-sales");
  123.         }
  124.  
  125.         private static void CarsWithParts(CarDealerContext carContext)
  126.         {
  127.             var cars = carContext.Cars
  128.                             .Include(c => c.Parts)
  129.                             .Select(c => new
  130.                             {
  131.                                 Car = new
  132.                                 {
  133.                                     Make = c.Make,
  134.                                     Model = c.Model,
  135.                                     TravelledDistance = c.TravelledDistance,
  136.                                 },
  137.                                 Parts = c.Parts.Select(p => new
  138.                                 {
  139.                                     p.Name,
  140.                                     p.Price
  141.                                 })
  142.                             })
  143.                             .ToList();
  144.  
  145.             Exporter.Export(cars, "cars-and-parts");
  146.         }
  147.  
  148.         private static void LocalSuppliers(CarDealerContext carContext)
  149.         {
  150.             var suppliers = carContext.Suppliers
  151.                 .Where(s => !s.IsImporter)
  152.                 .Select(s => new
  153.                 {
  154.                     Id = s.Id,
  155.                     Name = s.Name,
  156.                     PartsCount = s.Parts.Count()
  157.                 })
  158.                 .ToList();
  159.  
  160.             Exporter.Export(suppliers, "local-suppliers");
  161.         }
  162.  
  163.         private static void CarsFromMakeToyota(CarDealerContext carContext)
  164.         {
  165.             var cars = carContext.Cars
  166.                 .Where(c => c.Make == "Toyota")
  167.                 .Select(c => new
  168.                 {
  169.                     Id = c.Id,
  170.                     Make = c.Make,
  171.                     Model = c.Model,
  172.                     TravelledDistance = c.TravelledDistance
  173.                 })
  174.                 .OrderBy(c => c.Model)
  175.                 .ThenByDescending(c => c.TravelledDistance)
  176.                 .ToList();
  177.  
  178.             Exporter.Export(cars, "toyota-cars");
  179.         }
  180.  
  181.         private static void OrderedCustomers(CarDealerContext carContext)
  182.         {
  183.             var customers = carContext.Customers
  184.                             .OrderBy(c => c.Birthdate)
  185.                             .ThenBy(c => c.IsYoungDriver)
  186.                             .Select(c => new
  187.                             {
  188.                                 Id = c.Id,
  189.                                 Name = c.Name,
  190.                                 BirthDate = c.Birthdate,
  191.                                 IsYoungDriver = c.IsYoungDriver,
  192.                                 Sales = c.Sales.Select(s => new { s.Id, s.CustomerId, s.CarId, s.Discount })
  193.                             })
  194.                             .ToList();
  195.  
  196.             Exporter.Export(customers, "ordered-customers");
  197.         }
  198.  
  199.         private static void UsersAndProducts(ProductsShopContext context)
  200.         {
  201.             var users = context.Users
  202.                             .Include(u => u.SoldProducts)
  203.                             .Where(u => u.SoldProducts.Count() > 0)
  204.                             .Select(u => new
  205.                             {
  206.                                 FirstName = u.FirstName,
  207.                                 LastName = u.LastName,
  208.                                 Age = u.Age,
  209.                                 SoldProducts = new
  210.                                 {
  211.                                     Count = u.SoldProducts.Count(),
  212.                                     Products = u.SoldProducts.Select(p => new
  213.                                     {
  214.                                         p.Name,
  215.                                         p.Price
  216.                                     })
  217.                                 }
  218.                             })
  219.                             .ToList()
  220.                             .OrderByDescending(u => u.SoldProducts.Count)
  221.                             .ThenBy(u => u.LastName);
  222.  
  223.             Exporter.Export(new { UsersCount = users.Count(), Users = users }, "users-and-products");
  224.         }
  225.  
  226.         private static void CategoriesByProductsCount(ProductsShopContext context)
  227.         {
  228.             var categories = context.Categories
  229.                             .Include(c => c.Products)
  230.                             .Include(c => c.Products.Select(p => p.Price))
  231.                             .OrderBy(c => c.Products.Count())
  232.                             .Select(c => new
  233.                             {
  234.                                 Name = c.Name,
  235.                                 ProductsCount = c.Products.Count(),
  236.                                 AveragePrice = c.Products.Average(p => p.Price),
  237.                                 TotalRevenue = c.Products.Sum(p => p.Price)
  238.                             })
  239.                             .ToList();
  240.  
  241.             Exporter.Export(categories, "categories-by-products");
  242.         }
  243.  
  244.         private static void SerializeSuccessfullySoldProducts(ProductsShopContext context)
  245.         {
  246.             var users = context.Users
  247.                 .Include(u => u.SoldProducts)
  248.                 .Include(u => u.SoldProducts.Select(s => s.Buyer))
  249.                 .Where(u => u.SoldProducts.Any(p => p.Buyer != null))
  250.                 .Select(u => new
  251.                 {
  252.                     FirstName = u.FirstName,
  253.                     LastName = u.LastName,
  254.                     SoldProducts = u.SoldProducts.Where(p => p.Buyer != null).Select(p => new
  255.                     {
  256.                         Name = p.Name,
  257.                         Price = p.Price,
  258.                         BuyerFirstName = p.Buyer.FirstName,
  259.                         BuyerLastName = p.Buyer.LastName
  260.                     })
  261.                 })
  262.                 .ToList()
  263.                 .OrderBy(u => u.LastName)
  264.                 .ThenBy(u => u.FirstName);
  265.  
  266.             Exporter.Export(users, "users-sold-products");
  267.         }
  268.  
  269.         private static void SerializeProductsInRange(ProductsShopContext context)
  270.         {
  271.             var products = context.Products
  272.                             .Include("Seller")
  273.                             .Where(p => p.Price >= 500 && p.Price <= 1000 && p.BuyerId == null)
  274.                             .OrderBy(p => p.Price)
  275.                             .Select(p => new
  276.                             {
  277.                                 Name = p.Name,
  278.                                 Price = p.Price,
  279.                                 Seller = (p.Seller.FirstName + " " + p.Seller.LastName).Trim()
  280.                             })
  281.                             .ToList();
  282.                            
  283.             Exporter.Export(products, "products-in-range");
  284.         }
  285.     }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement