public static void AddCar(Car car) { using (STOContext db = new STOContext()) { db.Cars.Add(car); db.SaveChanges(); } }
public static void AddWorker(Worker workerToAdd) { using (STOContext db = new STOContext()) { db.Workers.Add(workerToAdd); db.SaveChanges(); } }
public static void AddOwner(Owner ownerToAdd) { using (STOContext db = new STOContext()) { db.Owners.Add(ownerToAdd); db.SaveChanges(); } }
public static List <Owner> FindOwners(int ownerID, int driverLicense, string fioOwner, string adress, int phone) { List <Owner> owners = new List <Owner>(); using (STOContext db = new STOContext()) { if (ownerID != 0) { owners = db.Owners.Where(k => k.OwnerID == ownerID).ToList(); } if (driverLicense != 0) { if (owners.Count != 0) { owners = owners.Where(k => k.DriverLicense == driverLicense).ToList(); } else { owners = db.Owners.Where(k => k.DriverLicense == driverLicense).ToList(); } } if (fioOwner != null) { if (owners.Count != 0) { owners = owners.Where(k => k.FioOwner == fioOwner).ToList(); } else { owners = db.Owners.Where(k => k.FioOwner == fioOwner).ToList(); } } if (adress != null) { if (owners.Count != 0) { owners = owners.Where(k => k.Adress == adress).ToList(); } else { owners = db.Owners.Where(k => k.Adress == adress).ToList(); } } if (phone != 0) { if (owners.Count != 0) { owners = owners.Where(k => k.Phone == phone).ToList(); } else { owners = db.Owners.Where(k => k.Phone == phone).ToList(); } } } return(owners); }
public static Owner FindOwnerById(int id) { Owner owner = null; using (STOContext db = new STOContext()) { owner = db.Owners.Where(k => k.OwnerID == id).ToList().FirstOrDefault(); } return(owner); }
public static Car FindCarById(int id) { Car car = null; using (STOContext db = new STOContext()) { car = db.Cars.Where(k => k.CarID == id).ToList().FirstOrDefault(); } return(car); }
public static Worker FindWorkerById(int id) { Worker worker = null; using (STOContext db = new STOContext()) { worker = db.Workers.Where(k => k.WorkerID == id).ToList().FirstOrDefault(); } return(worker); }
public static void DeleteWorker(Worker workerToDelete) { using (STOContext db = new STOContext()) { if (workerToDelete != null) { db.Workers.Remove(workerToDelete); db.SaveChanges(); } } }
public static void UpdataWorker(Worker worker) { using (STOContext db = new STOContext()) { if (worker != null) { db.Workers.Update(worker); db.SaveChanges(); } } }
public static List <Owner> GetAll() { List <Owner> all = new List <Owner>(); using (STOContext db = new STOContext()) { all = db.Owners.ToList(); } return(all); }
public static void DeleteCar(Car car) { using (STOContext db = new STOContext()) { if (car != null) { db.Cars.Remove(car); db.SaveChanges(); } } }
public static void UpdataCar(Car car) { using (STOContext db = new STOContext()) { if (car != null) { db.Cars.Update(car); db.SaveChanges(); } } }
public static void UpdataOwner(Owner owner) { using (STOContext db = new STOContext()) { if (owner != null) { db.Owners.Update(owner); db.SaveChanges(); } } }
public static void DeleteOwner(Owner ownerToDelete) { using (STOContext db = new STOContext()) { if (ownerToDelete != null) { db.Owners.Remove(ownerToDelete); db.SaveChanges(); } } }
public static List <Car> GetPage(int pageNumber, int sizeOfPage) { List <Car> all = new List <Car>(); using (STOContext db = new STOContext()) { all = db.Cars.Include(t => t.Model) .Include(t => t.Colour).Include(t => t.StateNumber). Skip(pageNumber * sizeOfPage).Take(sizeOfPage).ToList(); } return(all); }
public static void Initialize(STOContext db) { db.Database.EnsureCreated(); int carsNumber = 25; int ordersNumber = 50; int ownersNumber = 10; int workersNumber = 10; InitializeOwners(db, ownersNumber); InitializeWorkers(db, workersNumber); InitializeCars(db, carsNumber, ownersNumber); InitializeOrders(db, ordersNumber, carsNumber, workersNumber); }
private static void InitializeOrders(STOContext db, int ordersNumber, int carsNumber, int workersNumber) { //проверка, занесены ли данные в Orders if (db.Orders.Any()) { return; //бд иницилизирована } int carID; DateTime dateReceipt; DateTime?dateCompletion; int workerID; Random randomObj = new Random(DateTime.Now.Month * 30 + DateTime.Now.Year * 365 + DateTime.Now.Day + DateTime.Now.Minute); for (int OrderID = 1; OrderID <= ordersNumber; OrderID++) { var date = new DateTime(randomObj.Next(2000, 2020), randomObj.Next(1, 12), randomObj.Next(1, 28)); carID = db.Cars.Skip(randomObj.Next(0, db.Cars.Count() - 2)).First().CarID; dateReceipt = date; var twoDate = new DateTime(randomObj.Next(1, 10), randomObj.Next(1, 12), randomObj.Next(1, 28)); if (OrderID % 5 == 0) { dateCompletion = date.Add(new TimeSpan(twoDate.Ticks)); } else { dateCompletion = null; } workerID = db.Workers.Skip(randomObj.Next(0, db.Workers.Count() - 2)).First().WorkerID; db.Orders.Add(new Order { CarID = carID, DateReceipt = dateReceipt, DateCompletion = dateCompletion, WorkerID = workerID }); } //сохранение изменений в бд, связанную с объектом контекста db.SaveChanges(); }
private static void InitializeWorkers(STOContext db, int workersNumber) { //проверка, занесены ли данные в Workers if (db.Workers.Any()) { return; //бд иницилизирована } string fioWorker; DateTime dateOfEmployment; DateTime?dateOfDismissal; decimal salary; Random randomObj = new Random(1); for (int WorkerID = 1; WorkerID <= workersNumber; WorkerID++) { var date = new DateTime(randomObj.Next(1990, 2018), randomObj.Next(1, 12), randomObj.Next(1, 28)); fioWorker = MyRandom.RandomString(15); var twoDate = new DateTime(randomObj.Next(1, 10), randomObj.Next(1, 12), randomObj.Next(1, 28)); if (WorkerID % 5 == 0) { dateOfDismissal = date.Add(new TimeSpan(twoDate.Ticks)); } else { dateOfDismissal = null; } dateOfEmployment = date; salary = randomObj.Next(100, 1000); db.Workers.Add(new Worker { FioWorker = fioWorker, DateOfDismissal = dateOfDismissal, DateOfEmployment = dateOfEmployment, Salary = salary, }); } //сохранение изменений в бд, связанную с объектом контекста db.SaveChanges(); }
private static void InitializeCars(STOContext db, int carsNumber, int ownersNumber) { //проверка, занесены ли данные в Cars if (db.Cars.Any()) { return; //бд иницилизирована } int ownerID; string model; int power; string colour; string stateNumber; int yearOfIssue; int bodyNumber; int engineNumber; Random randomObj = new Random(1); for (int CarID = 1; CarID <= carsNumber; CarID++) { ownerID = db.Owners.Skip(randomObj.Next(0, db.Owners.Count() - 2)).First().OwnerID; model = MyRandom.RandomString(10); power = randomObj.Next(10, 100); colour = MyRandom.RandomString(10); stateNumber = MyRandom.RandomString(9); yearOfIssue = randomObj.Next(1, 4); bodyNumber = randomObj.Next(1, 10); engineNumber = randomObj.Next(1, 10); db.Cars.Add(new Car { OwnerID = ownerID, Model = model, Power = power, Colour = colour, StateNumber = stateNumber, YearOfIssue = yearOfIssue, BodyNumber = bodyNumber, EngineNumber = engineNumber }); } //сохранение изменений в бд, связанную с объектом контекста db.SaveChanges(); }
public static List <Worker> FindWorkers(string fioWorker, DateTime dateOfEmployment, DateTime?dateOfDismissal, decimal salary) { List <Worker> workers = new List <Worker>(); using (STOContext db = new STOContext()) { if (fioWorker != null && fioWorker != "") { workers = db.Workers.Where(k => k.FioWorker == fioWorker).ToList(); } if (dateOfEmployment != null) { if (workers.Count != 0) { workers = workers.Where(k => k.DateOfEmployment == dateOfEmployment).ToList(); } else { workers = db.Workers.Where(k => k.DateOfEmployment == dateOfEmployment).ToList(); } } if (dateOfDismissal != null) { if (workers.Count != 0) { workers = workers.Where(k => k.DateOfDismissal == dateOfDismissal).ToList(); } else { workers = db.Workers.Where(k => k.DateOfDismissal == dateOfDismissal).ToList(); } } if (salary != 0) { workers = db.Workers.Where(k => k.Salary == salary).ToList(); } } return(workers); }
public static List <Order> FindOrders(DateTime dateReceipt, DateTime?dateCompletion) { List <Order> orders = new List <Order>(); using (STOContext db = new STOContext()) { if (dateReceipt != null) { orders = db.Orders.Where(k => k.DateReceipt == dateReceipt).ToList(); } if (dateCompletion != null) { if (orders.Count != 0) { orders = orders.Where(k => k.DateCompletion == dateCompletion).ToList(); } else { orders = db.Orders.Where(k => k.DateCompletion == dateCompletion).ToList(); } } } return(orders); }
private static void InitializeOwners(STOContext db, int ownersNumber) { //проверка, занесены ли данные в Owners if (db.Owners.Any()) { return; //бд иницилизирована } int ownerID; int driverLicense; string fioOwner; string adress; int phone; Random randomObj = new Random(1); for (int OwnerID = 1; OwnerID <= ownersNumber; OwnerID++) { ownerID = OwnerID; driverLicense = randomObj.Next(1, 3000); fioOwner = MyRandom.RandomString(15); adress = MyRandom.RandomString(15); phone = randomObj.Next(1, 1000000); db.Owners.Add(new Owner { DriverLicense = driverLicense, FioOwner = fioOwner, Adress = adress, Phone = phone }); } //сохранение изменений в бд, связанную с объектом контекста db.SaveChanges(); }
public static List <Car> FindCar(string model, int power, string colour, string stateNumber, int yearOfIssue, int bodyNumber, int engineNumber) { List <Car> cars = new List <Car>(); using (STOContext db = new STOContext()) { if (model != null) { cars = db.Cars.Where(k => k.Model == model).ToList(); } if (power != 0) { if (cars.Count != 0) { cars = cars.Where(k => k.Power == power).ToList(); } else { cars = db.Cars.Where(k => k.Power == power).ToList(); } } if (colour != null) { if (cars.Count != 0) { cars = cars.Where(k => k.Colour == colour).ToList(); } else { cars = db.Cars.Where(k => k.Colour == colour).ToList(); } } if (stateNumber != null) { if (cars.Count != 0) { cars = cars.Where(k => k.StateNumber == stateNumber).ToList(); } else { cars = db.Cars.Where(k => k.StateNumber == stateNumber).ToList(); } } if (yearOfIssue != 0) { if (cars.Count != 0) { cars = cars.Where(k => k.YearOfIssue == yearOfIssue).ToList(); } else { cars = db.Cars.Where(k => k.YearOfIssue == yearOfIssue).ToList(); } } if (bodyNumber != 0) { if (cars.Count != 0) { cars = cars.Where(k => k.BodyNumber == bodyNumber).ToList(); } else { cars = db.Cars.Where(k => k.BodyNumber == bodyNumber).ToList(); } } if (engineNumber != 0) { if (cars.Count != 0) { cars = cars.Where(k => k.EngineNumber == engineNumber).ToList(); } else { cars = db.Cars.Where(k => k.EngineNumber == engineNumber).ToList(); } } } return(cars); }