예제 #1
0
        public ActionResult Create([Bind(Include = "ID,Name,ReleaseDate,Model,Price")] CarsModel carsModel)
        {
            if (ModelState.IsValid)
            {
                db.cars.Add(carsModel);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(carsModel));
        }
예제 #2
0
        public ActionResult Create([Bind(Include = "ID,Year,Make,Model,Color")] Cars cars)
        {
            if (ModelState.IsValid)
            {
                db.Car.Add(cars);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(cars));
        }
예제 #3
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();
        }
예제 #4
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();
        }
예제 #5
0
        public async Task <IActionResult> AddCars(string make, string model, string color, int year)
        {
            List <Cars> cars = await CD.GetCars();

            Cars newCar = new Cars();

            if (ModelState.IsValid)
            {
                newCar.Make  = make;
                newCar.Model = model;
                newCar.Color = color;
                newCar.Year  = year;
                _context.Cars.Add(newCar);
                _context.SaveChanges();
                //cars.Add(newCar);
                CD.AddCars(newCar);
            }
            return(RedirectToAction("Index", cars));
        }
예제 #6
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();
        }
예제 #7
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();
        }
예제 #8
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();
                }
            }
        }
예제 #9
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();
        }
예제 #10
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));
            }
        }
예제 #11
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));
        }
예제 #12
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());
            }
        }