Browse Source

Финальный билд за субботу. Работает сохранение всех компаний после закрытия программы, но не работает обновление пользователей!

master
Никита 6 years ago
parent
commit
44b59e67a1
  1. 24
      eCompanies/App.xaml.cs
  2. 50
      eCompanies/MainWindow.xaml.cs
  3. 5
      eCompanies/UsersWindow.xaml.cs

24
eCompanies/App.xaml.cs

@ -28,6 +28,7 @@ namespace eCompanies
{ {
using (var db = new CompaniesDBContext()) using (var db = new CompaniesDBContext())
{ {
c.CompanyId = 0;
db.Company.Add(c); db.Company.Add(c);
db.SaveChanges(); db.SaveChanges();
} }
@ -51,16 +52,23 @@ namespace eCompanies
db.SaveChanges(); db.SaveChanges();
} }
} }
public static void removeCompany(List<int> idList) public static void addUser(Users u)
{ {
using (var db = new CompaniesDBContext()) using (var db = new CompaniesDBContext())
{ {
foreach (int id in idList) u.UserId = 0;
{ db.Users.Add(u);
var x = db.Company.Where(i => i.CompanyId.Equals(id)); db.SaveChanges();
foreach (Company c in x) db.Company.Remove(c); }
} }
public static void updateUser(int id)
{
using (var db = new CompaniesDBContext())
{
var x = db.Users.Where(i => i.UserId.Equals(id));
foreach (Users u in x) db.Users.Update(u);
db.SaveChanges(); db.SaveChanges();
} }
} }
@ -103,5 +111,7 @@ namespace eCompanies
MessageBoxButton.YesNo); MessageBoxButton.YesNo);
} }
} }
} }

50
eCompanies/MainWindow.xaml.cs

@ -21,6 +21,12 @@ namespace eCompanies
companyGrid.DataContext = App.getCompanies(); companyGrid.DataContext = App.getCompanies();
} }
internal static void refreshDataGrid(DataGrid dg)
{
dg.CancelEdit();
dg.Items.Refresh();
}
private void updateCompanyItem(Company updatingCompany) private void updateCompanyItem(Company updatingCompany)
{ {
if (updatingCompany == null) return; if (updatingCompany == null) return;
@ -28,8 +34,7 @@ namespace eCompanies
for (int i = 0; i != c.Count(); i++) for (int i = 0; i != c.Count(); i++)
if (c[i].CompanyId.Equals(updatingCompany.CompanyId)) if (c[i].CompanyId.Equals(updatingCompany.CompanyId))
c[i] = updatingCompany; c[i] = updatingCompany;
companyGrid.Items.Refresh(); refreshDataGrid(companyGrid);
} }
private void companyGrid_AddingNewItem(object sender, AddingNewItemEventArgs e) private void companyGrid_AddingNewItem(object sender, AddingNewItemEventArgs e)
@ -39,7 +44,7 @@ namespace eCompanies
newItem.dataGridRowStatus = DataGridRowStatus.NEW; newItem.dataGridRowStatus = DataGridRowStatus.NEW;
var c = ((DataGrid)sender).DataContext as ObservableCollection<Company>; var c = ((DataGrid)sender).DataContext as ObservableCollection<Company>;
if (c.Count != 0) if (c.Count != 0)
newItem.CompanyId = ++c[c.Count - 1].CompanyId; newItem.CompanyId = c[c.Count - 1].CompanyId + 1;
} }
private void companyGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) private void companyGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
@ -61,9 +66,10 @@ namespace eCompanies
private void RowDelete_Click(object sender, RoutedEventArgs e) private void RowDelete_Click(object sender, RoutedEventArgs e)
{ {
((Company)companyGrid.SelectedItem).dataGridRowStatus = DataGridRowStatus.REMOVED; ((Company)companyGrid.SelectedItem).dataGridRowStatus = DataGridRowStatus.REMOVED;
companyGrid.Items.Refresh(); refreshDataGrid(companyGrid);
} }
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{ {
switch (App.saveResultsMsgBox()) switch (App.saveResultsMsgBox())
@ -79,10 +85,40 @@ namespace eCompanies
case MessageBoxResult.No: return; case MessageBoxResult.No: return;
case MessageBoxResult.Yes: e.Cancel = true; return; case MessageBoxResult.Yes: e.Cancel = true; return;
} }
switch (c.dataGridRowStatus)
{
case DataGridRowStatus.REMOVED:
App.removeCompany(c.CompanyId);
break;
case DataGridRowStatus.NEW:
syncUsers(c);
App.addCompany(c);
break;
case DataGridRowStatus.UPDATED:
syncUsers(c);
App.updateCompany(c);
break;
}
}
}
private void syncUsers(Company c)
{
foreach (Users u in c.Users)
{
switch (u.dataGridRowStatus)
{
case DataGridRowStatus.REMOVED:
App.removeUser(u.UserId);
break;
case DataGridRowStatus.NEW:
App.addUser(u);
break;
case DataGridRowStatus.UPDATED:
App.updateUser(u.UserId);
break;
}
} }
// нужно здесь обновить всех редактированных юзеров
// и добавить новых в базу данных
// TODO: переписать все обновления БД для [dataGridRowStatus]
} }
} }
} }

5
eCompanies/UsersWindow.xaml.cs

@ -53,13 +53,14 @@ namespace eCompanies
} }
currentCompany.ContractStatus = contractStatusComboBox.SelectedIndex; currentCompany.ContractStatus = contractStatusComboBox.SelectedIndex;
currentCompany.Name = companyNameBox.Text; currentCompany.Name = companyNameBox.Text;
currentCompany.dataGridRowStatus = DataGridRowStatus.UPDATED;
updateCompanyItem(currentCompany); updateCompanyItem(currentCompany);
} }
private void RowDelete_Click(object sender, RoutedEventArgs e) private void RowDelete_Click(object sender, RoutedEventArgs e)
{ {
((Users)usersGrid.SelectedItem).dataGridRowStatus = DataGridRowStatus.REMOVED; ((Users)usersGrid.SelectedItem).dataGridRowStatus = DataGridRowStatus.REMOVED;
usersGrid.Items.Refresh(); MainWindow.refreshDataGrid(usersGrid);
} }
private void usersGrid_CellEditEnding(object sender, System.Windows.Controls.DataGridCellEditEndingEventArgs e) private void usersGrid_CellEditEnding(object sender, System.Windows.Controls.DataGridCellEditEndingEventArgs e)
@ -76,7 +77,7 @@ namespace eCompanies
newItem.dataGridRowStatus = DataGridRowStatus.NEW; newItem.dataGridRowStatus = DataGridRowStatus.NEW;
var c = ((DataGrid)sender).DataContext as ObservableCollection<Users>; var c = ((DataGrid)sender).DataContext as ObservableCollection<Users>;
if (c.Count != 0) if (c.Count != 0)
newItem.UserId = ++c[c.Count - 1].UserId; newItem.UserId = c[c.Count - 1].UserId + 1;
} }
} }
} }

Loading…
Cancel
Save