/// <summary> Konstruktor uruchamiany w celu dodania nowego wpisu </summary> public CityEditViewModel(string connectionString) { this.SaveButtonClick = new GalaSoft.MvvmLight.Command.RelayCommand(() => { try { if (this.PostalCodeTextBoxContent == null || this.CityNameTextBoxContent == null) { MessageBox.Show("Wypełnij pole z kodem pocztowym i nazwą"); } else { using (var ctx = new molkomEntities()) { ctx.Cities.Add(GetCityObject()); ctx.SaveChanges(); GlobalVariables.refreshMainWindowFlag = 1; RequestClose?.Invoke(this, EventArgs.Empty); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }); SetDefaultDateFormat(); }
/// <summary> Zapisanie zmian edytowanego elementu do bazy danych </summary> public void SaveEntry() { try { using (var ctx = new molkomEntities()) { var address = ctx.Addresses.FirstOrDefault(x => x.id == this.idOfEntity); if (address != null) { address.name = this.NameTextBoxContent; address.surname = this.SurnameTextBoxContent; address.phoneNumber = this.PhoneTextBoxContent; address.birthdate = new DateTime(Int32.Parse(this.YearOfBirthTextBoxContent), this.MonthOfBirthSelectedIndex + 1, Int32.Parse(this.DayOfBirthTextBoxContent)); address.status = this.Status; if (GetIdOfSelectedCityItem() != 0) { address.city = GetIdOfSelectedCityItem(); } address.dateOfUpdate = System.DateTime.Now; ctx.SaveChanges(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> Konstruktor uruchamiany w celu edycji istniejącego wpisu </summary> public AddressEditViewModel(Addresses entity, string connectionString) { this.idOfEntity = entity.id; this.CitiesComboBoxContent = new ObservableCollection <string>(); this.MonthOfBirthComboBoxContent = new ObservableCollection <string>(); SetDefaultDateFormat(); LoadMonthsComboBoxContent(); LoadCitiesComboBoxContent(connectionString); LoadData(entity); this.SaveButtonClick = new GalaSoft.MvvmLight.Command.RelayCommand(() => { try { if (this.NameTextBoxContent == null || this.SurnameTextBoxContent == null) { MessageBox.Show("Wypełnij pole z imieniem i nazwiskiem"); } else { using (var ctx = new molkomEntities()) { SaveEntry(); GlobalVariables.refreshMainWindowFlag = 1; RequestClose?.Invoke(this, EventArgs.Empty); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }); }
/// <summary> Pobranie identyfikatora miasta z listy rozwijanej </summary> private int GetIdOfSelectedCityItem() { int id = 0; if (this.CitySelectedItem != null && this.CitySelectedItem != "") { string postalCode = this.CitySelectedItem.Split(',')[0].ToString(); using (var ctx = new molkomEntities()) { var _city = ctx.Cities.FirstOrDefault(x => x.postalCode.Equals(postalCode)); if (_city != null) { id = _city.id; } } } return(id); }
/// <summary> Zapisanie zmian edytowanego elementu do bazy danych </summary> public void SaveEntry() { try { using (var ctx = new molkomEntities()) { var address = ctx.Cities.FirstOrDefault(x => x.id == this.idOfEntity); if (address != null) { address.postalCode = PostalCodeTextBoxContent; address.name = CityNameTextBoxContent; address.dateOfCreation = System.DateTime.Now; address.dateOfUpdate = System.DateTime.Now; ctx.SaveChanges(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> Konstruktor uruchamiany w celu dodania nowego wpisu </summary> public AddressEditViewModel(string connectionString) { this.SaveButtonClick = new GalaSoft.MvvmLight.Command.RelayCommand(() => { try { if (this.NameTextBoxContent == null || this.SurnameTextBoxContent == null) { MessageBox.Show("Wypełnij pole z imieniem i nazwiskiem"); } else { using (var ctx = new molkomEntities()) { ctx.Addresses.Add(GetAddressObject()); ctx.SaveChanges(); GlobalVariables.refreshMainWindowFlag = 1; RequestClose?.Invoke(this, EventArgs.Empty); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }); this.Status = true; this.DateOfCreation = System.DateTime.Now; this.DateOfUpdate = System.DateTime.Now; this.CitiesComboBoxContent = new ObservableCollection <string>(); this.MonthOfBirthComboBoxContent = new ObservableCollection <string>(); SetDefaultDateFormat(); LoadMonthsComboBoxContent(); LoadCitiesComboBoxContent(connectionString); }
/// <summary> Ustawienie kontrolek widoku na podstawie wartości pól edytowanego elementu </summary> private void LoadData(Addresses entity) { try { if (entity.name != null) { this.NameTextBoxContent = entity.name; } else { this.NameTextBoxContent = ""; } if (entity.surname != null) { this.SurnameTextBoxContent = entity.surname; } else { this.SurnameTextBoxContent = ""; } if (entity.phoneNumber != null) { this.PhoneTextBoxContent = entity.phoneNumber; } else { this.PhoneTextBoxContent = ""; } if (entity.status != null) { this.Status = Convert.ToBoolean(entity.status); } else { this.Status = false; } if (entity.birthdate != null) { this.DayOfBirthTextBoxContent = Convert.ToDateTime(entity.birthdate).Day.ToString(); this.MonthOfBirthSelectedIndex = Convert.ToDateTime(entity.birthdate).Month - 1; this.YearOfBirthTextBoxContent = Convert.ToDateTime(entity.birthdate).Year.ToString(); } if (entity.city != null) { using (var ctx = new molkomEntities()) { var city = ctx.Cities.FirstOrDefault(x => x.id == entity.city); if (city != null) { this.CitiesComboBoxContent.Add(city.postalCode + ", " + city.name); this.CitySelectedItem = city.postalCode + ", " + city.name; this.CitySelectedValue = city.postalCode + ", " + city.name; } } } if (entity.dateOfCreation != null) { this.DateOfCreation = Convert.ToDateTime(entity.dateOfCreation); } else { this.DateOfCreation = DateTime.Now.ToLocalTime(); } if (entity.dateOfUpdate != null) { this.DateOfUpdate = Convert.ToDateTime(entity.dateOfUpdate); } else { this.DateOfUpdate = DateTime.Now.ToLocalTime(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> Uruchomienie nowego okna w celu edycji istniejącego elementu i odświeżenie listy elementów </summary> public void EditEntry() { try { this.idOfEditedItem = getIdOfSelectedItem(); if (this.currentTableName.Equals("Addresses")) { using (var ctx = new molkomEntities()) { var address = ctx.Addresses.FirstOrDefault(x => x.id.Equals(idOfEditedItem)); if (address != null) { Thread newWindowThread = new Thread(new ThreadStart(() => { SynchronizationContext.SetSynchronizationContext( new DispatcherSynchronizationContext( Dispatcher.CurrentDispatcher)); Window tempWindow = new AddressEditWindow(address, this.connectionString); tempWindow.Closed += (s, e) => Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background); tempWindow.Show(); System.Windows.Threading.Dispatcher.Run(); })); newWindowThread.SetApartmentState(ApartmentState.STA); newWindowThread.IsBackground = true; newWindowThread.Start(); while (true) { if (GlobalVariables.refreshMainWindowFlag == 1) { LoadAdressBookDisplayWindow(); break; } else if (GlobalVariables.refreshMainWindowFlag == 2) { break; } } OnPropertyChanged("DisplaySource"); GlobalVariables.refreshMainWindowFlag = 0; } } } else if (this.currentTableName.Equals("Cities")) { using (var ctx = new molkomEntities()) { var city = ctx.Cities.FirstOrDefault(x => x.id.Equals(idOfEditedItem)); if (city != null) { Thread newWindowThread = new Thread(new ThreadStart(() => { SynchronizationContext.SetSynchronizationContext( new DispatcherSynchronizationContext( Dispatcher.CurrentDispatcher)); Window tempWindow = new CityEditWindow(city, this.connectionString); tempWindow.Closed += (s, e) => Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background); tempWindow.Show(); System.Windows.Threading.Dispatcher.Run(); })); newWindowThread.SetApartmentState(ApartmentState.STA); newWindowThread.IsBackground = true; newWindowThread.Start(); while (true) { if (GlobalVariables.refreshMainWindowFlag == 1) { LoadCitiesDisplayWindow(); break; } else if (GlobalVariables.refreshMainWindowFlag == 2) { break; } } OnPropertyChanged("DisplaySource"); GlobalVariables.refreshMainWindowFlag = 0; } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }