[#43] BaseDataWindow: Do not use Context from ContextWindow any more

This commit is contained in:
2024-03-18 10:28:35 +01:00
parent 5715c41a2e
commit 729d2fd76c
7 changed files with 154 additions and 122 deletions

View File

@ -18,16 +18,22 @@ namespace Elwig.Windows {
private bool _branchChanged = false;
private bool _branchUpdate = false;
private void BranchesInitEditing() {
_branchList = new(Context.Branches.OrderBy(b => b.Name).ToList());
private async Task BranchesInitEditing(AppDbContext ctx) {
_branchList = new(await ctx.Branches
.OrderBy(b => b.Name)
.Include(b => b.PostalDest!.AtPlz)
.ToListAsync());
_branches = _branchList.ToDictionary(b => b.ZwstId, b => (string?)b.ZwstId);
_branchIds = _branchList.ToDictionary(b => b, b => b.ZwstId);
ControlUtils.RenewItemsSource(BranchList, _branchList, b => (b as Branch)?.ZwstId);
BranchList_SelectionChanged(null, null);
}
private void BranchesFinishEditing() {
ControlUtils.RenewItemsSource(BranchList, Context.Branches.OrderBy(b => b.Name).ToList(), b => (b as Branch)?.ZwstId);
private async Task BranchesFinishEditing(AppDbContext ctx) {
ControlUtils.RenewItemsSource(BranchList, await ctx.Branches
.OrderBy(b => b.Name)
.Include(b => b.PostalDest!.AtPlz)
.ToListAsync(), b => (b as Branch)?.ZwstId);
_branchList = null;
_branches = null;
_branchIds = null;
@ -37,31 +43,31 @@ namespace Elwig.Windows {
BranchDeleteButton.IsEnabled = false;
}
private async Task BranchesSave() {
private async Task BranchesSave(AppDbContext ctx) {
if (!_branchChanged || _branchList == null || _branches == null || _branchIds == null)
return;
foreach (var (zwstid, _) in _branches.Where(b => b.Value == null)) {
Context.Remove(Context.Branches.Find(zwstid));
ctx.Remove(ctx.Branches.Find(zwstid)!);
}
foreach (var (branch, old) in _branchIds) {
branch.ZwstId = old;
}
foreach (var (old, zwstid) in _branches.Where(b => b.Value != null)) {
Context.Update(Context.Branches.Find(old));
ctx.Update(ctx.Branches.Find(old)!);
}
await Context.SaveChangesAsync();
await ctx.SaveChangesAsync();
foreach (var (old, zwstid) in _branches.Where(b => b.Value != null)) {
await Context.Database.ExecuteSqlAsync($"UPDATE branch SET zwstid = {zwstid} WHERE zwstid = {old}");
await ctx.Database.ExecuteSqlAsync($"UPDATE branch SET zwstid = {zwstid} WHERE zwstid = {old}");
}
await Context.SaveChangesAsync();
await ctx.SaveChangesAsync();
foreach (var branch in _branchList.Where(b => !_branchIds.ContainsKey(b))) {
if (branch.ZwstId == null) continue;
await Context.AddAsync(branch);
ctx.Add(branch);
}
await Context.SaveChangesAsync();
await ctx.SaveChangesAsync();
}
private void BranchList_SelectionChanged(object? sender, SelectionChangedEventArgs? evt) {
@ -91,7 +97,7 @@ namespace Elwig.Windows {
private void BranchAddButton_Click(object sender, RoutedEventArgs evt) {
if (_branchList == null) return;
_branchChanged = true;
var item = Context.CreateProxy<Branch>();
var item = new Branch { ZwstId = "", Name = "" };
_branchList.Add(item);
BranchList.SelectedItem = item;
UpdateButtons();