Exemple #1
0
 public Company GetCompany(Guid id)
 {
     using (var context = new CarApiContext(_optionsBuilder.Options))
     {
         return(context.Companies.SingleOrDefault(o => o.Id == id));
     }
 }
Exemple #2
0
 public ICollection <Company> GetCompanies()
 {
     using (var context = new CarApiContext(_optionsBuilder.Options))
     {
         return(context.Companies.ToList());
     }
 }
Exemple #3
0
 public void AddCompany(Company company)
 {
     using (var context = new CarApiContext(_optionsBuilder.Options))
     {
         context.Companies.Add(company);
         context.SaveChanges();
     }
 }
Exemple #4
0
 public void UpdateCar(Car car)
 {
     using (var context = new CarApiContext(_optionsBuilder.Options))
     {
         context.Cars.Update(car);
         context.SaveChanges();
     }
 }
Exemple #5
0
 public void DeleteCar(Guid id)
 {
     using (var context = new CarApiContext(_optionsBuilder.Options))
     {
         var Car = GetCar(id);
         context.Cars.Remove(Car);
         context.SaveChanges();
     }
 }
Exemple #6
0
        public void DeleteCompany(Guid id)
        {
            using (var context = new CarApiContext(_optionsBuilder.Options))
            {
                var cars = GetCars().Where(c => c.CompanyId == id);
                foreach (var car in cars)
                {
                    context.Cars.Remove(car);
                }

                var company = GetCompany(id);
                context.Companies.Remove(company);
                context.SaveChanges();
            }
        }
        public static void EnsureSeedData(this CarApiContext context)
        {
            if (!context.Cars.Any() || !context.Companies.Any())
            {
                var companyId = Guid.NewGuid();
                context.Companies.Add(new Company()
                {
                    Id = companyId, Name = "Charlies Gravel Transports Ltd.", Address = "Concrete Road 8, 111 11 Newcastle"
                });
                context.Cars.Add(new Car
                {
                    Id        = Guid.NewGuid(),
                    CompanyId = companyId,
                    VIN       = "YS2R4X20005399401",
                    RegNr     = "ABC123"
                });
                context.Cars.Add(new Car
                {
                    Id        = Guid.NewGuid(),
                    CompanyId = companyId,
                    VIN       = "VLUR4X20009093588",
                    RegNr     = "DEF456"
                });
                context.Cars.Add(new Car
                {
                    Id        = Guid.NewGuid(),
                    CompanyId = companyId,
                    VIN       = "VLUR4X20009048066",
                    RegNr     = "GHI789"
                });

                companyId = Guid.NewGuid();
                context.Companies.Add(new Company()
                {
                    Id = companyId, Name = "Jonnies Bulk Ltd.", Address = "Balk Road 12, 222 22 London"
                });
                context.Cars.Add(new Car
                {
                    Id        = Guid.NewGuid(),
                    CompanyId = companyId,
                    VIN       = "YS2R4X20005388011",
                    RegNr     = "JKL012"
                });
                context.Cars.Add(new Car
                {
                    Id        = Guid.NewGuid(),
                    CompanyId = companyId,
                    VIN       = "YS2R4X20005387949",
                    RegNr     = "MNO345"
                });

                companyId = Guid.NewGuid();
                context.Companies.Add(new Company()
                {
                    Id = companyId, Name = "Harolds Road Transports Ltd.", Address = "Budget Avenue 1, 333 33 Birmingham"
                });
                context.Cars.Add(new Car
                {
                    Id        = Guid.NewGuid(),
                    CompanyId = companyId,
                    VIN       = "YS2R4X20005387765",
                    RegNr     = "PQR678"
                });
                context.Cars.Add(new Car
                {
                    Id        = Guid.NewGuid(),
                    CompanyId = companyId,
                    VIN       = "YS2R4X20005387055",
                    RegNr     = "STU901"
                });
            }
            else
            {
                foreach (var car in context.Cars)
                {
                    car.Disabled = false;
                }
            }
            context.SaveChanges();
        }