private void SaveButton_Click(object sender, RoutedEventArgs e) { PersonInfoModel pInfo = (PersonInfoModel)this.DataContext; if (String.IsNullOrEmpty(pInfo.FirstName)) { MessageBox.Show("Укажите имя"); return; } if (String.IsNullOrEmpty(pInfo.LastName)) { MessageBox.Show("Укажите фамилию"); return; } if (String.IsNullOrEmpty(pInfo.Sex)) { MessageBox.Show("Укажите пол"); return; } using (DataBaseConnector dbService = new DataBaseConnector()) { bool savef = false; if (pInfo != null && pInfo.ViewModelStatus == RecordActions.Inserted) { pInfo.UUID = Guid.NewGuid().ToString(); dbService.HandlePersonInfoTable(pInfo, null, RecordActions.Inserted); savef = true; } else if (pInfo != null && pInfo.ViewModelStatus == RecordActions.Updated) { dbService.HandlePersonInfoTable(pInfo, t => (t.UUID == pInfo.UUID), RecordActions.Updated); savef = true; } if (pInfo.Payments != null && pInfo.Payments.Count > 0) { foreach (var payment in pInfo.Payments) { if (String.IsNullOrEmpty(payment.PersonUUID) == true) { payment.PersonUUID = pInfo.UUID; payment.UUID = Guid.NewGuid().ToString(); payment.ViewModelStatus = RecordActions.Inserted; } if (payment.ViewModelStatus == RecordActions.Inserted) dbService.HandlePersonPaymentsTable(payment, null, RecordActions.Inserted); else if (payment.ViewModelStatus == RecordActions.Updated) dbService.HandlePersonPaymentsTable(payment, t => (t.UUID == payment.UUID), RecordActions.Updated); } if (removedPayments.Count>0) { foreach (var payment in removedPayments) { if (String.IsNullOrEmpty(payment.UUID) == false) { dbService.HandlePersonPaymentsTable(payment, t => (t.UUID == payment.UUID), RecordActions.Deleted); } } removedPayments = new List<PersonPaymentsModel>(); } savef = true; } if (savef==true) { int result = dbService.SaveChanges(); if (result > 0) { if (ParentListView != null) ParentListView.UpdatePersonList(pInfo, pInfo.ViewModelStatus); InitialOperations(); ms.Position = 0; oldContextKeeper.Serialize(ms, pInfo); } } } }
private void DeleteButton_Click(object sender, RoutedEventArgs e) { PersonInfoModel pInfo = (PersonInfoModel)this.DataContext; using (DataBaseConnector dbService = new DataBaseConnector()) { dbService.HandlePersonInfoTable(pInfo, t => (t.UUID == pInfo.UUID), RecordActions.Deleted); if (pInfo.Payments != null && pInfo.Payments.Count > 0) { foreach (var payment in pInfo.Payments) { dbService.HandlePersonPaymentsTable(payment, t => (t.UUID == payment.UUID), RecordActions.Deleted); } if (removedPayments.Count > 0) { foreach (var payment in removedPayments) { dbService.HandlePersonPaymentsTable(payment, t => (t.UUID == payment.UUID), RecordActions.Deleted); } removedPayments = new List<PersonPaymentsModel>(); } } int result = dbService.SaveChanges(); if (result > 0) { if (ParentListView!=null) ParentListView.UpdatePersonList(pInfo, RecordActions.Deleted); this.Close(); } } }