[] 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

@ -18,16 +18,16 @@ namespace Elwig.Windows {
private bool _attrChanged = false;
private bool _attrUpdate = false;
private void WineAttributesInitEditing() {
_attrList = new(Context.WineAttributes.OrderBy(a => a.Name).ToList());
private async Task WineAttributesInitEditing(AppDbContext ctx) {
_attrList = new(await ctx.WineAttributes.OrderBy(a => a.Name).ToListAsync());
_attrs = _attrList.ToDictionary(a => a.AttrId, a => (string?)a.AttrId);
_attrIds = _attrList.ToDictionary(a => a, a => a.AttrId);
ControlUtils.RenewItemsSource(WineAttributeList, _attrList, a => (a as WineAttr)?.AttrId);
WineAttributeList_SelectionChanged(null, null);
}
private void WineAttributesFinishEditing() {
ControlUtils.RenewItemsSource(WineAttributeList, Context.WineAttributes.OrderBy(a => a.Name).ToList(), a => (a as WineAttr)?.AttrId);
private async Task WineAttributesFinishEditing(AppDbContext ctx) {
ControlUtils.RenewItemsSource(WineAttributeList, await ctx.WineAttributes.OrderBy(a => a.Name).ToListAsync(), a => (a as WineAttr)?.AttrId);
_attrList = null;
_attrs = null;
_attrIds = null;
@ -37,31 +37,31 @@ namespace Elwig.Windows {
WineAttributeDeleteButton.IsEnabled = false;
}
private async Task WineAttributesSave() {
private async Task WineAttributesSave(AppDbContext ctx) {
if (!_attrChanged || _attrList == null || _attrs == null || _attrIds == null)
return;
foreach (var (attrid, _) in _attrs.Where(a => a.Value == null)) {
Context.Remove(Context.WineAttributes.Find(attrid));
ctx.Remove(ctx.WineAttributes.Find(attrid)!);
}
foreach (var (attr, old) in _attrIds) {
attr.AttrId = old;
}
foreach (var (old, attrid) in _attrs.Where(a => a.Value != null)) {
Context.Update(Context.WineAttributes.Find(old));
ctx.Update(ctx.WineAttributes.Find(old)!);
}
await Context.SaveChangesAsync();
await ctx.SaveChangesAsync();
foreach (var (old, attrid) in _attrs.Where(a => a.Value != null)) {
await Context.Database.ExecuteSqlAsync($"UPDATE wine_attribute SET attrid = {attrid} WHERE attrid = {old}");
await ctx.Database.ExecuteSqlAsync($"UPDATE wine_attribute SET attrid = {attrid} WHERE attrid = {old}");
}
await Context.SaveChangesAsync();
await ctx.SaveChangesAsync();
foreach (var attr in _attrList.Where(a => !_attrIds.ContainsKey(a))) {
if (attr.AttrId == null) continue;
await Context.AddAsync(attr);
ctx.Add(attr);
}
await Context.SaveChangesAsync();
await ctx.SaveChangesAsync();
}
private void WineAttributeList_SelectionChanged(object? sender, SelectionChangedEventArgs? evt) {
@ -88,7 +88,7 @@ namespace Elwig.Windows {
private void WineAttributeAddButton_Click(object sender, RoutedEventArgs evt) {
if (_attrList == null) return;
_attrChanged = true;
var item = Context.CreateProxy<WineAttr>();
var item = new WineAttr { AttrId = "", Name = "" };
_attrList.Add(item);
WineAttributeList.SelectedItem = item;
UpdateButtons();