public ActionResult MyCars(int page = 1, string user = null, string search = null) { var db = new CarsDbContext(); var pageSize = 5; var carsQuery = db.Cars.AsQueryable(); if (search != null) { carsQuery = carsQuery .Where(c => c.Make.ToLower().Contains(search.ToLower()) || c.Model.ToLower().Contains(search.ToLower())); } if (user != null) { carsQuery = carsQuery .Where(c => c.Owner.Email == user); } var cars = carsQuery .OrderByDescending(c => c.Id) .Skip((page - 1) * pageSize) .Take(pageSize) .Select(c => new HomeIndexCarModel { Id = c.Id, ImageUrl = c.ImageUrl, Make = c.Make, Model = c.Model, Year = c.Year, IsRented = c.IsRented }) .ToList(); ViewBag.CurrentPage = page; return(View(cars)); }
private void SeedCarModels(CarsDbContext context) { var models = new List <CarModel>() { new CarModel() { Id = 1, Description = "Model 1" }, new CarModel() { Id = 2, Description = "Model 2" } }; foreach (var model in models) { context.CarModels.Add(model); context.SaveChanges(); } }
static void Main() { Database.SetInitializer (new MigrateDatabaseToLatestVersion <CarsDbContext, Configuration>()); var db = new CarsDbContext(); //PROBLEM 6 (Populating from json)..i have disabled some configs to speed up the process /*db.Configuration.AutoDetectChangesEnabled = false; * for (int i = 0; i < 5; i++) * { * DbPopulator.PopulateFromJsonFile(db, "../../data." + i + ".json"); * } * * db.Configuration.AutoDetectChangesEnabled = true;*/ //PROBLEM 7 (it saves the files in Cars.Client/bin/debug folder) //CarQueriesProccessor.ProccessQueriesFromXml(db, "../../Queries.xml"); }
public static void Main() { Console.OutputEncoding = Encoding.UTF8; using (var context = new CarsDbContext()) { //ResetDatabase(context); var cars = context .Cars .Include(c => c.Engine) .Include(c => c.Make) .Include(c => c.LicencePlate) .Include(c => c.CarDealerships) .ThenInclude(x => x.Dealership) .ToArray(); Console.WriteLine(); } }
private void SeedCars(CarsDbContext context) { if (!context.Cars.Any()) { var cars = new Car[] { new Car("Car 1", context.Owners.First(o => o.ID == 1), context.CarModels.First(m => m.ID == 1)), new Car("Car 2", context.Owners.First(o => o.ID == 3), context.CarModels.First(m => m.ID == 1)), new Car("Car 3", context.Owners.First(o => o.ID == 2), context.CarModels.First(m => m.ID == 1)), new Car("Car 4", context.Owners.First(o => o.ID == 1), context.CarModels.First(m => m.ID == 2)), new Car("Car 5", context.Owners.First(o => o.ID == 2), context.CarModels.First(m => m.ID == 1)), new Car("Car 6", context.Owners.First(o => o.ID == 1), context.CarModels.First(m => m.ID == 2)) }; foreach (var car in cars) { context.Cars.Add(car); } } }
public ActionResult Index() { var db = new CarsDbContext(); var cars = db.Cars .Where(c => !c.IsRented) .OrderByDescending(c => c.Id) .Take(3) .Select(c => new HomeIndexCarModel { Id = c.Id, ImageUrl = c.ImageUrl, Make = c.Make, Model = c.Model, Year = c.Year }) .ToList(); return(View(cars)); }
public ActionResult Rentings() { var db = new CarsDbContext(); var userId = this.User.Identity.GetUserId(); var rentings = db.Rentings .OrderByDescending(r => r.Id) .Where(r => r.UserId == userId) .Select(r => new UserRentingModel { CarName = r.Car.Make + " " + r.Car.Model + " (" + r.Car.Year + ")", Days = r.Days, RentedOn = r.RentedOn, TotalPrice = r.TotalPrice, CarImageUrl = r.Car.ImageUrl }) .ToList(); return(View(rentings)); }
public ActionResult Edit(int id) { using (var db = new CarsDbContext()) { var car = db.Cars.Find(id); var carViewModel = new CarViewModel { Id = car.Id, Make = car.Make, Model = car.Model, Color = car.Color, Year = car.Year, Engine = car.Engine, PricePerDay = car.PricePerDay, OwnerId = car.OwnerId }; return(View(carViewModel)); } }
static void Main(string[] args) { using (var db = new CarsDbContext()) { //ResetDatabase(db); var plates = db.LicensePlates.ToArray(); var cars = db.Cars .Include(c => c.Engine) .Include(c => c.Make) .Include(c => c.LicensePlate) .Include(c => c.CarDealerships) .ThenInclude(cd => cd.Dealership) .OrderBy(c => c.ProductionYear) .ToArray(); cars[1].LicensePlate = plates.Single(p => p.LicensePlateId == 3); db.SaveChanges(); foreach (var car in cars) { Console.WriteLine($"{car.Make.Name}"); foreach (var carDealer in car.CarDealerships) { var dealership = carDealer.Dealership; Console.WriteLine($"--{dealership.Name}"); } Console.WriteLine($"---Fuel { car.Engine.FuelType}"); Console.WriteLine($"---Transmission: { car.Transmission}"); var licensePlateNum = car.LicensePlate != null ? car.LicensePlate.Name : "No Plate"; Console.WriteLine($"-----LicensePlate: {licensePlateNum}"); Console.WriteLine("-------------------"); } Console.WriteLine(); } }
public ActionResult Edit(CarViewModel carModel) { if (ModelState.IsValid) { using (var db = new CarsDbContext()) { var car = db.Cars.Find(carModel.Id); car.Make = carModel.Make; car.Model = carModel.Model; car.Color = carModel.Color; car.Year = carModel.Year; car.Engine = carModel.Engine; car.PricePerDay = carModel.PricePerDay; db.SaveChanges(); } return(RedirectToAction("Details", new { id = carModel.Id })); } return(View(carModel)); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, CarsDbContext dbContext) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseSpaStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller}/{action=Index}/{id?}"); }); app.UseSpa(spa => { // To learn more about options for serving an Angular SPA from ASP.NET Core, // see https://go.microsoft.com/fwlink/?linkid=864501 spa.Options.SourcePath = "ClientApp"; if (env.IsDevelopment()) { spa.UseAngularCliServer(npmScript: "start"); } }); //DbSeeder.Seed(dbContext); }
public static void Main() { // I am using SQL EXPRESS ! // I am using SQL EXPRESS ! // I am using SQL EXPRESS ! // Also using Newtonsoft JSON parser, // so you will need to download that as well - it's in NuGet // Messages appear on the console to let you know it's working var db = new CarsDbContext(); //string jsonFilesDirPath = @".\json"; //db.Configuration.AutoDetectChangesEnabled = false; //JsonImporter.GetParsedCars(jsonFilesDirPath, db); //db.Configuration.AutoDetectChangesEnabled = true; string xmlFilePath = @".\Queries.xml"; string outputFilePath = @".\exports\"; XmlExporter.ExportQueriesFromFile(xmlFilePath, outputFilePath, db); }
public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var db = new CarsDbContext(); var car = db.Cars .Where(c => c.Id == id) .Select(c => new CarDetailsModel() { Id = c.Id, Make = c.Make, Model = c.Model, EngineType = c.EngineType, ImageUrl = c.ImageUrl, Year = c.Year, Color = c.Color, Power = c.Power, PricePerDay = c.PricePerDay, Engine = c.Engine, IsRented = c.IsRented, TotalRents = c.Rentings.Count, ContactInformation = c.Owner.Email, }) .FirstOrDefault(); if (car == null) { return(HttpNotFound()); } return(View(car)); }
private void SeedCars(CarsDbContext context) { var cars = new List <Car>() { new Car() { Id = 1, OwnerId = 1, ModelId = 1, Description = "Car 1" }, new Car() { Id = 2, OwnerId = 3, ModelId = 1, Description = "Car 2" }, new Car() { Id = 3, OwnerId = 2, ModelId = 1, Description = "Car 3" }, new Car() { Id = 4, OwnerId = 1, ModelId = 2, Description = "Car 4" }, new Car() { Id = 5, OwnerId = 2, ModelId = 1, Description = "Car 5" }, new Car() { Id = 6, OwnerId = 1, ModelId = 2, Description = "Car 6" }, }; foreach (var car in cars) { context.Cars.Add(car); context.SaveChanges(); } }
private void SeedOwners(CarsDbContext context) { var owners = new List <Owner>() { new Owner() { Id = 1, FullName = "Pesho Peshov" }, new Owner() { Id = 2, FullName = "Gosho Goshov" }, new Owner() { Id = 3, FullName = "Tosho Toshov" } }; foreach (var owner in owners) { context.Owners.Add(owner); context.SaveChanges(); } }
private static void Seed(CarsDbContext context) { Make[] makes = new[] { new Make { Name = "Bmw" }, new Make { Name = "Mercedes" }, new Make { Name = "Audi" }, new Make { Name = "VW" }, }; Engine[] engines = new[] { new Engine { Capacity = 2.0, Cyllinders = 4, FuelType = FuelType.Petrol, HorsePower = 150, }, new Engine { Capacity = 3.0, Cyllinders = 6, FuelType = FuelType.Electric, HorsePower = 250, }, new Engine { Capacity = 3.5, Cyllinders = 8, FuelType = FuelType.Diesel, HorsePower = 350, } }; Car[] cars = new[] { new Car { Engine = engines[0], Make = makes[3], Doors = 4, Model = "Polo", ProductionYear = new DateTime(2008), Transmission = Transmission.Automatic, }, new Car { Engine = engines[1], Make = makes[1], Doors = 2, Model = "C - 300 ", ProductionYear = new DateTime(2012), Transmission = Transmission.Automatic, }, new Car { Engine = engines[2], Make = makes[0], Doors = 2, Model = "3 series", ProductionYear = new DateTime(2018), Transmission = Transmission.Manual, } }; context.Cars.AddRange(cars); Dealership[] dealerships = new[] { new Dealership { Name = "PetkoCarAUTO" }, new Dealership { Name = "MobileAutoHouse" } }; context.Dealerships.AddRange(dealerships); CarDealership[] carDealerships = new [] { new CarDealership { Car = cars[0], Dealership = dealerships[0] }, new CarDealership { Car = cars[1], Dealership = dealerships[1] }, new CarDealership { Car = cars[0], Dealership = dealerships[1] } }; context.CarDealerships.AddRange(carDealerships); LicensePlate[] licensePlates = new[] { new LicensePlate { Number = "СТ5500АВ" }, new LicensePlate { Number = "СО5210НЕ" }, new LicensePlate { Number = "С1130БЕ" } }; context.LicensePlate.AddRange(licensePlates); context.SaveChanges(); }
public CarsController(CarsDbContext db) { _db = db; }
private static void Seed(CarsDbContext db) { //Tuk si suzdavame dannite i gi vkarvame v bazata chrez CarsDbContext klasa : var Makes = new[] { new Make() { Name = "Ford" }, new Make() { Name = "Mercedes" }, new Make() { Name = "Audi" }, new Make() { Name = "BMW" }, new Make() { Name = "Лада" }, new Make() { Name = "Трабант" } }; db.Makes.AddRange(Makes); var Engines = new[] { new Engine() { Capacity = 1.6, Cyllinders = 4, FuelType = FuelType.Petrol, HorsePower = 95 }, new Engine() { Capacity = 3.0, Cyllinders = 8, FuelType = FuelType.Diesel, HorsePower = 318 }, new Engine() { Capacity = 1.2, Cyllinders = 3, FuelType = FuelType.Gas, HorsePower = 60 } }; db.Engine.AddRange(Engines); var LicencePlates = new[] { new LicencePlate() { Number = "CB1235AB" }, new LicencePlate() { Number = "BG8856CD" }, new LicencePlate() { Number = "BR6732BG" } }; db.LicencePlates.AddRange(LicencePlates); var Dealerships = new[] { new Dealership() { Name = "SoftUniAuto", }, new Dealership() { Name = "FastAndFurious Auto" }, new Dealership() { Name = "AutoHera" } }; db.Dealerships.AddRange(Dealerships); var Cars = new[] { new Car() { Engine = Engines[2], Make = Makes[3], Doors = 4, Model = "Кашон", LicencePlate = LicencePlates[0], YearOfProduction = new DateTime(1958, 1, 1), Transmission = Transmission.Manual, FuelType = FuelType.Diesel }, new Car() { Engine = Engines[1], Make = Makes[4], Doors = 2, Model = "Кашон-432", LicencePlate = LicencePlates[1], YearOfProduction = new DateTime(1954, 1, 1), Transmission = Transmission.Automatic, FuelType = FuelType.Gas }, new Car() { Engine = Engines[0], Make = Makes[0], Doors = 4, Model = "Escort", LicencePlate = LicencePlates[2], YearOfProduction = new DateTime(1955, 1, 1), Transmission = Transmission.Automatic, FuelType = FuelType.Electric } }; db.Cars.AddRange(Cars); //populvame si Mapping tablicata s danni var CarDealerships = new[] { new CarDealership() { Car = Cars[0], Dealership = Dealerships[0] }, new CarDealership() { Car = Cars[1], Dealership = Dealerships[1] }, new CarDealership() { Car = Cars[2], Dealership = Dealerships[2] }, }; db.CarDealerships.AddRange(CarDealerships); //Seivame si dannite v bazata db.SaveChanges(); }
public CarController(CarsDbContext context) { _context = context; }
public HomeController() { db = new CarsDbContext(); }
public ModelsRepository(CarsDbContext context) : base(context) { }
protected override void Seed(CarsDbContext context) { this.SeedOwners(context); this.SeedCarModels(context); this.SeedCars(context); }
public HomeController(CarsDbContext context) { this.context = context; }
private static void ImportData() { Database.SetInitializer(new MigrateDatabaseToLatestVersion <CarsDbContext, Configuration>()); var cars = new List <Car>(); var files = Directory.GetFiles(Environment.CurrentDirectory).Where(fileName => fileName.EndsWith(".json")).ToList(); foreach (var file in files) { var fileContent = File.ReadAllText(file); var fileCars = JsonConvert.DeserializeObject <IEnumerable <Car> >(fileContent); cars.AddRange(fileCars); Console.WriteLine("{0} read.", file); } var manufacturerNames = new HashSet <string>(StringComparer.OrdinalIgnoreCase); var cityNames = new HashSet <string>(StringComparer.OrdinalIgnoreCase); var dealerNames = new HashSet <string>(StringComparer.OrdinalIgnoreCase); foreach (var car in cars) { manufacturerNames.Add(car.ManufacturerName); cityNames.Add(car.Dealer.City); dealerNames.Add(car.Dealer.Name); } var db = new CarsDbContext(); db.Configuration.AutoDetectChangesEnabled = false; // Add cities Console.WriteLine("Adding cities..."); foreach (var cityName in cityNames) { db.Cities.AddOrUpdate(c => c.Name, new City { Name = cityName }); } db.SaveChanges(); Console.WriteLine("Cities added."); // Add manufacturers Console.WriteLine("Adding manufacturers..."); foreach (var manufacturerName in manufacturerNames) { db.Manufacturers.AddOrUpdate(m => m.Name, new Manufacturer { Name = manufacturerName }); } db.SaveChanges(); Console.WriteLine("Manufacturers added."); // Add dealers Console.WriteLine("Adding dealers..."); foreach (var dealerName in dealerNames) { db.Dealers.AddOrUpdate(d => d.Name, new Dealer { Name = dealerName }); } db.SaveChanges(); Console.WriteLine("Dealers added."); // Add cars Console.Write("Adding cars"); for (int carIndex = 0; carIndex < cars.Count; carIndex++) { var car = cars[carIndex]; var databaseCarCity = db.Cities.FirstOrDefault(x => x.Name == car.Dealer.City); if (databaseCarCity == null) { throw new ArgumentException("Invalid city name!"); } var databaseCar = new Cars.Models.Car { Dealer = db.Dealers.FirstOrDefault(d => d.Name == car.Dealer.Name), Manufacturer = db.Manufacturers.FirstOrDefault(x => x.Name == car.ManufacturerName), Model = car.Model, Price = car.Price, TransmissionType = car.TransmissionType, Year = (short)car.Year, }; db.Configuration.AutoDetectChangesEnabled = true; if (!databaseCar.Dealer.Cities.Any(c => c.Name == databaseCarCity.Name)) { databaseCarCity.Dealers.Add(databaseCar.Dealer); } db.Cars.Add(databaseCar); if (carIndex % 100 == 0) { db.SaveChanges(); Console.Write("."); } db.Configuration.AutoDetectChangesEnabled = false; } Console.WriteLine(); db.SaveChanges(); Console.WriteLine("Cars added."); }
protected BaseRepo(CarsDbContext context) { _context = context; }
public CarDealersController(CarsDbContext context) { _context = context; }
public ManufacturerRepo(CarsDbContext context) : base(context) { }
static void Main(string[] args) { Console.OutputEncoding = Encoding.UTF8; //choose another server or DB name //var ob = new DbContextOptionsBuilder(); //ob.UseSqlServer(@"Server=(Name of Server);Database= CarsCars;Integrated Security=True;"); //var context = new CarsDbContext(ob.Options); using (var context = new CarsDbContext()) { RestartDb(context); var cars = context.Cars .Select(s => new { s.Transmission, ProductYear = s.ProductionYear, s.Model, s.Make, s.Engine, s.LicensePlate, CarDealerShips = s.CarDealerShips.Select(c => new { c.DealerShip }).ToArray() }).ToArray(); foreach (var x in cars) { Console.WriteLine($"{x.Make.Name} {x.Model}"); Console.WriteLine($"Dealer: "); foreach (var y in x.CarDealerShips) { Console.WriteLine($"--{y.DealerShip.Name}"); } if (x.CarDealerShips.Count() == 0) { Console.WriteLine("--no dealer"); } Console.WriteLine($"----Fuel: {x.Engine.FuelType}"); Console.WriteLine($"----Transmission: {x.Transmission}"); Console.WriteLine($"----Capacity: {x.Engine.Capacity}"); Console.WriteLine($"----HorsePower: {x.Engine.HorsePower}"); Console.WriteLine($"----ProductionYear: {x.ProductYear}"); if (x.LicensePlate != null) { Console.WriteLine($"------Plate Number: {x.LicensePlate.Number}"); } else { Console.WriteLine($"------Plate Number: no number"); } Console.WriteLine("=================================================="); } //var cars = context.Cars // .Include(c => c.CarDealerShips) // .ThenInclude(c => c.DealerShip) // .Include(c => c.Engine) // .Include(c => c.Make) // .Include(c => c.LicensePlate) // .OrderBy(c => c.ProductionYear) // .ToArray(); //var pl = context.LicensePlates.ToArray(); //cars[0].LicensePlate = pl[2]; //cars[2].LicensePlate = pl[1]; //foreach (var x in cars) //{ // Console.WriteLine($"{x.Make.Name} {x.Model}"); // Console.WriteLine($"--Dealership: "); // foreach (var z in x.CarDealerShips) // { // Console.WriteLine($"----{z.DealerShip.Name}"); // } // Console.WriteLine($"--Fuel: {x.Engine.FuelType}"); // Console.WriteLine($"--Transmission: {x.Transmission}"); // string y = x.LicensePlate != null ? x.LicensePlate.Number : "No plate"; // Console.WriteLine($"--Plate Number: {y}"); // Console.WriteLine("-------------------------"); //} } Console.WriteLine(); }
public CarsForSalesApiController(CarsDbContext context) { _context = context; }
private static void Seed(CarsDbContext context) { var makes = new[] { new Make { Name = "Ford" }, new Make { Name = "Mercedes" }, new Make { Name = "Audi" }, new Make { Name = "Bmw" }, new Make { Name = "АЗЛК" }, new Make { Name = "Лада" }, new Make { Name = "Трабант" }, }; context.Makes.AddRange(makes); var engines = new[] { new Engine { Capacity = 1.6, Cyllinders = 4, FuelType = FuelType.Petrol, HorsePower = 95 }, new Engine { Capacity = 3.0, Cyllinders = 8, FuelType = FuelType.Electric, HorsePower = 318 }, new Engine { Capacity = 1.2, Cyllinders = 3, FuelType = FuelType.Gas, HorsePower = 60 } }; context.Engines.AddRange(engines); var licensePlates = new[] { new LicensePlate { Number = "CB1234AN" }, new LicensePlate { Number = "CB4567BC" }, new LicensePlate { Number = "BP9999AA" }, }; context.LicensePlates.AddRange(licensePlates); var cars = new[] { new Car { Engine = engines[2], Make = makes[6], Doors = 4, Model = "Кашон-P50", ProductionYear = new DateTime(1958, 1, 1), Transmission = Transmission.Manuel, LicensePlate = licensePlates[1] }, new Car { Engine = engines[1], Make = makes[4], Doors = 3, Model = "Москвич-423", ProductionYear = new DateTime(1954, 1, 1), Transmission = Transmission.Automatic }, new Car { Engine = engines[0], Make = makes[0], Doors = 4, Model = "Escort", ProductionYear = new DateTime(1955, 1, 1), Transmission = Transmission.Automatic, LicensePlate = licensePlates[2] } }; context.Cars.AddRange(cars); var dealerships = new[] { new DealerShip { Name = "SoftUni-Auto" }, new DealerShip { Name = "Fast and Furious Auto" } }; context.DealerShips.AddRange(dealerships); var cardealerships = new[] { new CarDealerShip { Car = cars[0], DealerShip = dealerships[0] }, new CarDealerShip { Car = cars[0], DealerShip = dealerships[1] }, new CarDealerShip { Car = cars[1], DealerShip = dealerships[1] }, }; context.CarDealerShips.AddRange(cardealerships); context.SaveChanges(); }
public CitiesController(CarsDbContext context) { _context = context; }