/// <summary> /// The method of adding info about Client and Car to Parking /// </summary> private void AddToBalanceParking() { using (ParkingContext db = new ParkingContext()) { var tempCar = db.Cars.Where(c => c.NumberCar == txbInfoNumber.Text).FirstOrDefault(); int tempNum = Int32.Parse(txbInfoPhone.Text); var tempClient = db.Clients.Where(c => c.Phone == tempNum).FirstOrDefault(); BalanceParking temPlace = new BalanceParking { ClientId = tempClient.Id, CarId = tempCar.Id, DataAdded = DateTime.Parse(txbInfoDate.Text), Place = (int)SenderPlace }; var isExistPlace = db.BalanceParking.Where(pl => pl.CarId == temPlace.CarId).FirstOrDefault(); if (isExistPlace == null) { db.BalanceParking.Add(temPlace); db.HistoryAddedCars.Add(new HistoryAddedCars { ClientId = tempClient.Id, CarId = tempCar.Id, DataAdded = DateTime.Parse(txbInfoDate.Text), Place = (int)SenderPlace }); db.SaveChanges(); StatisticsOfPlaces.UpdateStatisticsPlaces(temPlace.Place, '+'); StatisticsOfPlaces.ShowStatisticsInMainWind(); SetColor.SetColorForPlaces((List <Button>)(this.Owner as MainWindow).ListButtons); this.Close(); } } }
/// <summary> /// Add Car toDatabase /// </summary> private void AddCarToDB() { Car newCar = new Car(); newCar.Manufacture = txbAddManufacture.Text; newCar.Model = txbAddModel.Text; newCar.Color = txbAddColorCar.Text; newCar.NumberCar = txbAddNumberCar.Text; using (ParkingContext db = new ParkingContext()) { var tempCar = db.Cars.Where(c => c.NumberCar == newCar.NumberCar).FirstOrDefault(); if (tempCar == null) { db.Cars.Add(newCar); db.SaveChanges(); } else { MessageBox.Show("I can't to add this car. It exists in Database!", "Warrning!"); } } }
/// <summary> /// Add Car toDatabase /// </summary> private void AddClientToDB() { Client newClient = new Client(); newClient.LastName = txbAddClientLastName.Text; newClient.FirstName = txbAddClientFirstName.Text; newClient.MidleName = txbAddClientMidleName.Text; newClient.Phone = Int32.Parse(txbAddClientPhone.Text); newClient.Adress = txbAddClientAdress.Text; using (ParkingContext db = new ParkingContext()) { var tempClient = db.Clients.Where(c => c.Phone == newClient.Phone).FirstOrDefault(); if (tempClient == null) { db.Clients.Add(newClient); db.SaveChanges(); } else { MessageBox.Show("I can't to add this client. He exist in Database!", "Warrning!"); } } }
/// <summary> /// Take the car out off the parking place /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnMoveFromPlace_Click(object sender, RoutedEventArgs e) { if (CheckAllFieldsClickPlace()) { using (ParkingContext db = new ParkingContext()) { using (var tran = db.Database.BeginTransaction()) { try { BalanceParking bal = db.BalanceParking.Where(b => b.Place == SenderPlace).FirstOrDefault(); //BalanceParking bal = new BalanceParking { Place = (int)SenderPlace }; //db.BalanceParking.Attach(bal); if (bal != null) { db.BalanceParking.Remove(bal); db.HistoryDeletedCars.Add(new HistoryDeletedCars { ClientId = bal.ClientId, CarId = bal.CarId, DataAdded = bal.DataAdded, Place = (int)SenderPlace }); db.SaveChanges(); tran.Commit(); btnMoveFromPlace.IsEnabled = false; btnSaveToPlace.IsEnabled = false; StatisticsOfPlaces.UpdateStatisticsPlaces(bal.Place, '-'); StatisticsOfPlaces.ShowStatisticsInMainWind(); SetColor.SetColorForPlaces((List <Button>)(this.Owner as MainWindow).ListButtons); this.Close(); } } catch (Exception) { tran.Rollback(); } } } } }