Пример #1
0
        private static void ImportSales()
        {
            var objects = new List <Sale>();

            var context = new CarsDBContext();

            double[] discounts = { 1, 0.95, 0.9, 0.85, 0.8, 0.7, 0.6, 0.5 };

            int carsCount      = context.Cars.Count();
            int customersCount = context.Customers.Count();
            int discountCount  = discounts.Length;

            int num = 0;

            foreach (var car in context.Cars.ToList())
            {
                int    customer = ((num + 7) % customersCount) + 1;
                double discount = discounts[num % discountCount];

                var sale = new Sale()
                {
                    CarId      = car.Id,
                    CustomerId = customer,
                    Discount   = discount
                };

                objects.Add(sale);
                num++;
            }

            context.Sales.AddRange(objects);

            context.SaveChanges();
        }
Пример #2
0
        private static void SalesWithAppliedDiscount()
        {
            var context = new CarsDBContext();
            var query   = context.Sales
                          .Select(s => new
            {
                car = new
                {
                    s.Car.Make,
                    s.Car.Model,
                    s.Car.TravelledDistance
                },
                customerName = s.Customer.Name,
                s.Discount,
                price = s.Car.Parts.Sum(p => p.Price)
            })
                          .AsEnumerable()
                          .Select(s => new
            {
                s.car,
                s.customerName,
                s.Discount,
                s.price,
                pricewithDiscount = s.price * (decimal)s.Discount
            });

            var json = JsonConvert.SerializeObject(query, Formatting.Indented);

            File.WriteAllText("..\\..\\Jsons\\sales-discounts.json", json);
        }
Пример #3
0
        private static void ImportCars()
        {
            var json = File.ReadAllText("..\\..\\jsons\\cars.json");

            var objects = JsonConvert.DeserializeObject <List <Car> >(json);

            var context = new CarsDBContext();

            int count      = context.Parts.Count();
            int partsCount = 10;
            int num        = 0;

            foreach (var c in objects)
            {
                for (int i = 0; i < partsCount; i++)
                {
                    c.Parts.Add(context.Parts.Find(num % count + 1));
                }

                num++;
                partsCount++;
                if (partsCount == 21)
                {
                    partsCount = 10;
                }
            }

            context.Cars.AddRange(objects);

            context.SaveChanges();
        }
Пример #4
0
 public IEnumerable <RoadDto> GetAllRoads()
 {
     using (CarsDBContext dBContext = new CarsDBContext())
     {
         IEnumerable <RoadDto> result = AutoMapperConfiguration.MapperConfiguration.Map <List <Road>, List <RoadDto> >(dBContext.Roads.ToList());
         return(result);
     }
 }
Пример #5
0
 public IEnumerable <DriverDto> GetDriversByAge(int age, int driverType)
 {
     using (CarsDBContext dBContext = new CarsDBContext())
     {
         IEnumerable <DriverDto> result = AutoMapperConfiguration.MapperConfiguration.Map <List <Driver>, List <DriverDto> >(dBContext.Drivers.Where(dri => dri.Age > age && dri.DriversLessonType == driverType).ToList());
         return(result);
     }
 }
Пример #6
0
 public IEnumerable <DriverDto> GetDriversByCityName(String cityName)
 {
     using (CarsDBContext dBContext = new CarsDBContext())
     {
         IEnumerable <DriverDto> result = AutoMapperConfiguration.MapperConfiguration.Map <List <Driver>, List <DriverDto> >(dBContext.Drivers.Where(dri => dri.Address.Contains(cityName)).ToList());
         return(result);
     }
 }
Пример #7
0
 public IEnumerable <CarDto> GetCars()
 {
     using (CarsDBContext dBContext = new CarsDBContext())
     {
         IEnumerable <CarDto> result = AutoMapperConfiguration.MapperConfiguration.Map <List <Car>, List <CarDto> > (dBContext.Car.ToList());
         return(result);
     }
 }
Пример #8
0
        static Reports AddReport(CarsDBContext context, string xmlText)
        {
            Reports report = new Reports {
                ReportData = xmlText, RequestedDateTime = DateTime.Now, AddedToDatabase = false
            };

            context.Add(report);
            return(report);
        }
Пример #9
0
        private static void OrderedCustomers()
        {
            var context = new CarsDBContext();

            var query = context.Customers.OrderBy(c => c.BirthDate).ThenBy(c => c.IsYoungDriver);

            var json = JsonConvert.SerializeObject(query, Formatting.Indented);

            File.WriteAllText("..\\..\\Jsons\\ordered-customers.json", json);
        }
Пример #10
0
        public IEnumerable <DriverDto> GetAllDrivers()
        {
            using (CarsDBContext dBContext = new CarsDBContext())
            {
                List <Driver> sourceDrivers = dBContext.Drivers.ToList();

                IEnumerable <DriverDto> result = AutoMapperConfiguration.MapperConfiguration.Map <List <Driver>, List <DriverDto> >(sourceDrivers);
                return(result);
            }
        }
Пример #11
0
        private static void CarsFromMakeToyota()
        {
            var context = new CarsDBContext();

            var query = context.Cars.Where(c => c.Make == "Toyota")
                        .OrderBy(c => c.Model)
                        .ThenByDescending(c => c.TravelledDistance);

            var json = JsonConvert.SerializeObject(query, Formatting.Indented);

            File.WriteAllText("..\\..\\Jsons\\toyota-cars.json", json);
        }
Пример #12
0
        public IndexModel(CarsDBContext context, IHubContext <CarHub> hubContext, ISqlDependencyManager sqlDependencyManager, CarHub carHubDI)
        {
            _context       = context;
            _carHubContext = hubContext;
            //all dependency based stuff is in dependency manager
            _sqlDependencyManager = sqlDependencyManager;
            _carHub = carHubDI;


            //only callback is added to the manager
            _sqlDependencyManager.SetAction(OnDependencyChangeInManager);
        }
Пример #13
0
        private static void ImportCustomers()
        {
            var json = File.ReadAllText("..\\..\\jsons\\customers.json");

            var objects = JsonConvert.DeserializeObject <List <Customer> >(json);

            var context = new CarsDBContext();

            context.Customers.AddRange(objects);

            context.SaveChanges();
        }
Пример #14
0
 public void Configure(IApplicationBuilder app,
                       IHostingEnvironment env,
                       CarsDBContext carsDBContext)
 {
     if (env.IsDevelopment())
     {
         app.UseDeveloperExceptionPage();
     }
     app.UseStaticFiles();
     carsDBContext.CreateSeedData();
     app.UseMvc();
     app.Run(async(context) =>
     {
         await context.Response.WriteAsync("C# can't find anything");
     });
 }
Пример #15
0
        static void AddCarProductCarFeature(CarsDBContext context, DefaultDeserializer deserializer)
        {
            var carProductsQuery = context.CarProducts;
            var carFeaturesQuery = context.CarFeatures;

            foreach (var car in deserializer.Find("Car"))
            {
                int carID = carProductsQuery.Where(s => s.Vin == car.Attributes["VIN"]).First().Id;
                foreach (var feature in car.FindChildren("Feature"))
                {
                    int featureID = carFeaturesQuery.Where(s => s.Code == feature.Attributes["Code"]).First().Id;
                    context.Add(new CarProductCarFeature {
                        CarProductId = carID, InstalledFeatureId = featureID
                    });
                }
            }
        }
Пример #16
0
        private static void LocalSuppliers()
        {
            var context = new CarsDBContext();

            var query = context.Suppliers
                        .Where(s => s.IsImporter == false)
                        .Select(s => new
            {
                s.Id,
                s.Name,
                PartsCount = s.Parts.Count
            });

            var json = JsonConvert.SerializeObject(query, Formatting.Indented);

            File.WriteAllText("..\\..\\Jsons\\local-suppliers.json", json);
        }
Пример #17
0
        public IEnumerable <DriverDto> GetDriversByDriverType(string driverType)
        {
            using (CarsDBContext dBContext = new CarsDBContext())
            {
                var driversByType = (from driver in dBContext.Drivers
                                     join driverLessonType in dBContext.DriverLessonType on driver.DriversLessonType equals driverLessonType.LessonType
                                     where driverLessonType.LessonTypeDriver == driverType
                                     select new DriverDto
                {
                    ID = driver.ID,
                    DriverName = driver.DriverName,
                    DriversLessonType = driver.DriversLessonType,
                    Address = driver.Address,
                    DriversLessonTypeValue = driverLessonType.LessonTypeDriver
                });

                return(driversByType.ToList());
            }
        }
Пример #18
0
        public void AddCar(CarDto mycar)
        {
            Car car = new Car();

            if (mycar != null)
            {
                car = AutoMapperConfiguration.MapperConfiguration.Map <CarDto, Car>(mycar);

                //car.CarColor = mycar.CarColor;
                //car.CarCompany = mycar.CarCompany;
                //car.CarSize = mycar.CarSize;
                //car.Comments = mycar.Comments;
                using (CarsDBContext dBContext = new CarsDBContext())
                {
                    dBContext.Car.Add(car);
                    dBContext.SaveChanges();
                }
            }
        }
Пример #19
0
        public IEnumerable <CarDto> GetCarsByNumOfPassengers(int numOfPassengers)
        {
            using (CarsDBContext dBContext = new CarsDBContext())
            {
                var carsAndPass = from car in dBContext.Car
                                  join carSize in dBContext.CarSize on car.CarSize equals carSize.ID
                                  where carSize.NumOfPassengers > numOfPassengers
                                  select new CarDto
                {
                    CarColor        = car.CarColor,
                    CarCompany      = car.CarCompany,
                    ID              = car.ID,
                    Comments        = car.Comments,
                    NumOfPassengers = carSize.NumOfPassengers,
                    CarSize1        = carSize.CarSize1
                };

                return(carsAndPass.ToList());
            }
        }
Пример #20
0
        static void AddCarProducts(CarsDBContext context, DefaultDeserializer deserializer)
        {
            var carModelsQuery    = context.CarModels;
            var carFactoriesQuery = context.CarFactories;

            foreach (var factory in deserializer.Find("Factory"))
            {
                //finds factory id by correlating same factory name attribute and Name column in CarFactories
                int factoryID = carFactoriesQuery.Where(s => s.Name == factory.Attributes["Name"]).First().Id;
                foreach (var car in factory.FindChildren("Car"))
                {
                    int    carID   = carModelsQuery.Where(s => s.Name == car.FindChildren("Model").First().Value).First().Id;
                    short  carYear = short.Parse(car.FindChildren("ProductionYear").First().Value);
                    string carVIN  = car.Attributes["VIN"];
                    context.Add(new CarProducts {
                        CarModelId = carID, Year = carYear, Vin = carVIN, FactoryId = factoryID
                    });
                }
            }
        }
Пример #21
0
        private static void ImportParts()
        {
            var json = File.ReadAllText("..\\..\\jsons\\parts.json");

            var objects = JsonConvert.DeserializeObject <List <Part> >(json);

            var context = new CarsDBContext();

            int count = context.Suppliers.Count();
            int num   = 0;

            foreach (var p in objects)
            {
                p.SupplierId = num % count + 1;
                num++;
            }

            context.Parts.AddRange(objects);

            context.SaveChanges();
        }
Пример #22
0
        public IHttpActionResult UpdateCar(CarDto myCar)
        {
            using (CarsDBContext dBContext = new CarsDBContext())
            {
                Car car = dBContext.Car.Where(c => c.ID == myCar.ID).FirstOrDefault();
                if (car != null)
                {
                    //IEnumerableCarDto> result = AutoMapperConfiguration.MapperConfiguration.Map<List<Car>, List<CarDto>>(dBContext.carss.Where(dri => dri.Age > age && dri.DriversLessonType == driverType).ToList());

                    car = AutoMapperConfiguration.MapperConfiguration.Map <CarDto, Car>(myCar);

                    //    car.CarColor = myCar.CarColor;
                    //    car.CarCompany = myCar.CarCompany;
                    //    car.CarSize = myCar.CarSize;
                    //    car.Comments = myCar.Comments;
                }


                dBContext.SaveChanges();
                return(Ok(myCar));
            }
        }
Пример #23
0
        public IHttpActionResult AddDriver(DriverDto myDriver)

        {
            Driver driver = new Driver();

            if (driver != null)
            {
                driver = AutoMapperConfiguration.MapperConfiguration.Map <DriverDto, Driver>(myDriver);

                //driver.DriverName = myDriver.DriverName;
                //driver.DriversLessonType = myDriver.DriversLessonType;
                //driver.Age = myDriver.Age;
                //driver.Address = myDriver.Address;
                using (CarsDBContext dBContext = new CarsDBContext())
                {
                    dBContext.Drivers.Add(driver);
                    dBContext.SaveChanges();
                    return(Ok(myDriver));
                }
            }
            return(Ok(myDriver));
        }
Пример #24
0
        static void AddToDatabase(Stream stream)
        {
            DefaultDeserializer deserializer = new DefaultDeserializer(stream);

            deserializer.Deserialize();

            CarsDBContext db = new CarsDBContext();

            //gets reference to added report for later
            Reports addedReport;

            try
            {
                addedReport = AddReport(db, deserializer.Document.ToString());
                db.SaveChanges();
            }
            catch (Exception e)
            {
                throw e;
            }

            try
            {
                AddCarProducts(db, deserializer);
                db.SaveChanges();

                AddCarProductCarFeature(db, deserializer);
                db.SaveChanges();
            }
            catch (Exception e)
            {
                throw e;
            }

            ConfirmReport(context: db, reportToConfirm: addedReport);
            db.SaveChanges();
        }
Пример #25
0
        static void Main(string[] args)
        {
            using (var ctx = new CarsDBContext())
            {
                var car = new Car()
                {
                    Brand = "Audi"
                };
                var driver1 = new Driver()
                {
                    DriverID = 1, Name = "Bill"
                };
                car.CurrentDriver = driver1;
                var driver2 = new Driver()
                {
                    DriverID = 1, Name = "Bill"
                };
                car.PreviousDriver = driver2;

                Debug.WriteLine("Check equality before saving to db");
                Debug.WriteLineIf(driver1.Equals(driver2), "driver1 and 2 are equal");
                Debug.WriteLineIf(driver1.GetHashCode() == driver2.GetHashCode(), "hashcodes are equal");
                Debug.WriteLine("Driver1 hash: {0}", driver1.GetHashCode());
                Debug.WriteLine("Driver2 hash: {0}", driver2.GetHashCode());

                ctx.Cars.Add(car);
                ctx.Drivers.Remove(driver2);
                ctx.SaveChanges();

                Debug.WriteLine("Check equality after saving to db");
                Debug.WriteLineIf(driver1.Equals(driver2), "driver1 and 2 are equal");
                Debug.WriteLineIf(driver1.GetHashCode() == driver2.GetHashCode(), "hashcodes are equal");
                Debug.WriteLine("Driver1 hash: {0}", driver1.GetHashCode());
                Debug.WriteLine("Driver2 hash: {0}", driver2.GetHashCode());
            }
        }
Пример #26
0
        private static void CarsWithTheirListOfParts()
        {
            var context = new CarsDBContext();

            var query = context.Cars
                        .Select(c => new
            {
                car = new
                {
                    c.Make,
                    c.Model,
                    c.TravelledDistance,
                },
                parts = c.Parts.Select(p => new
                {
                    p.Name,
                    p.Price
                })
            });

            var json = JsonConvert.SerializeObject(query, Formatting.Indented);

            File.WriteAllText("..\\..\\Jsons\\cars-and-parts.json", json);
        }
Пример #27
0
        private static void TotalSalesByCustomer()
        {
            var     context             = new CarsDBContext();
            decimal youngDriverDiscount = 0.95m;
            var     query = context.Customers.Where(c => c.Sales.Any())
                            .Select(c => new
            {
                fullName   = c.Name,
                boughtCars = c.Sales.Count(),
                spentMoney = c.Sales.Select(s => new { price = s.Car.Parts.Sum(p => p.Price), s.Discount }),
                c.IsYoungDriver
            })
                            .AsEnumerable()
                            .Select(c => new
            {
                c.fullName,
                c.boughtCars,
                spentMoney = (c.spentMoney.Sum(s => s.price * (decimal)s.Discount)) * (c.IsYoungDriver? youngDriverDiscount : 1)
            });

            var json = JsonConvert.SerializeObject(query, Formatting.Indented);

            File.WriteAllText("..\\..\\Jsons\\customers-total-sales.json", json);
        }
Пример #28
0
 public CarsController(CarsDBContext context)
 {
     _context = context;
 }
Пример #29
0
        static void Main()
        {
            var context = new CarsDBContext();

            context.Database.Initialize(true);
        }
Пример #30
0
 public DetailsModel(CarsDBContext context)
 {
     _context = context;
 }