[#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,20 @@ namespace Elwig.Windows {
private bool _actChanged = false; private bool _actChanged = false;
private bool _actUpdate = false; private bool _actUpdate = false;
private void AreaCommitmentTypesInitEditing() { private async Task AreaCommitmentTypesInitEditing(AppDbContext ctx) {
_actList = new(Context.AreaCommitmentTypes.OrderBy(v => v.VtrgId).ToList()); _actList = new(await ctx.AreaCommitmentTypes
.OrderBy(v => v.VtrgId)
.Include(t => t.WineVar)
.Include(t => t.WineAttr)
.ToListAsync());
_acts = _actList.ToDictionary(v => v.VtrgId, v => (string?)v.VtrgId); _acts = _actList.ToDictionary(v => v.VtrgId, v => (string?)v.VtrgId);
_actIds = _actList.ToDictionary(v => v, v => v.VtrgId); _actIds = _actList.ToDictionary(v => v, v => v.VtrgId);
ControlUtils.RenewItemsSource(AreaCommitmentTypeList, _actList, a => (a as AreaComType)?.VtrgId); ControlUtils.RenewItemsSource(AreaCommitmentTypeList, _actList, a => (a as AreaComType)?.VtrgId);
AreaCommitmentTypeList_SelectionChanged(null, null); AreaCommitmentTypeList_SelectionChanged(null, null);
} }
private void AreaCommitmentTypesFinishEditing() { private async Task AreaCommitmentTypesFinishEditing(AppDbContext ctx) {
ControlUtils.RenewItemsSource(AreaCommitmentTypeList, Context.AreaCommitmentTypes.OrderBy(v => v.SortId).ToList(), v => (v as AreaComType)?.VtrgId); ControlUtils.RenewItemsSource(AreaCommitmentTypeList, await ctx.AreaCommitmentTypes.OrderBy(v => v.SortId).ToListAsync(), v => (v as AreaComType)?.VtrgId);
_actList = null; _actList = null;
_acts = null; _acts = null;
_actIds = null; _actIds = null;
@ -37,31 +41,31 @@ namespace Elwig.Windows {
AreaCommitmentTypeDeleteButton.IsEnabled = false; AreaCommitmentTypeDeleteButton.IsEnabled = false;
} }
private async Task AreaCommitmentTypesSave() { private async Task AreaCommitmentTypesSave(AppDbContext ctx) {
if (!_actChanged || _actList == null || _acts == null || _actIds == null) if (!_actChanged || _actList == null || _acts == null || _actIds == null)
return; return;
foreach (var (vtrgid, _) in _acts.Where(a => a.Value == null)) { foreach (var (vtrgid, _) in _acts.Where(a => a.Value == null)) {
Context.Remove(Context.AreaCommitmentTypes.Find(vtrgid)); ctx.Remove(ctx.AreaCommitmentTypes.Find(vtrgid)!);
} }
foreach (var (attr, old) in _actIds) { foreach (var (attr, old) in _actIds) {
attr.VtrgId = old; attr.VtrgId = old;
} }
foreach (var (old, vtrgid) in _acts.Where(a => a.Value != null)) { foreach (var (old, vtrgid) in _acts.Where(a => a.Value != null)) {
Context.Update(Context.AreaCommitmentTypes.Find(old)); ctx.Update(ctx.AreaCommitmentTypes.Find(old)!);
} }
await Context.SaveChangesAsync(); await ctx.SaveChangesAsync();
foreach (var (old, vtrgid) in _acts.Where(a => a.Value != null)) { foreach (var (old, vtrgid) in _acts.Where(a => a.Value != null)) {
await Context.Database.ExecuteSqlAsync($"UPDATE area_commitment_type SET vtrgid = {vtrgid} WHERE vtrgid = {old}"); await ctx.Database.ExecuteSqlAsync($"UPDATE area_commitment_type SET vtrgid = {vtrgid} WHERE vtrgid = {old}");
} }
await Context.SaveChangesAsync(); await ctx.SaveChangesAsync();
foreach (var type in _actList.Where(a => !_actIds.ContainsKey(a))) { foreach (var type in _actList.Where(a => !_actIds.ContainsKey(a))) {
if (type.VtrgId == null) continue; if (type.VtrgId == null) continue;
await Context.AddAsync(type); ctx.Add(type);
} }
await Context.SaveChangesAsync(); await ctx.SaveChangesAsync();
} }
private void AreaCommitmentTypeList_SelectionChanged(object? sender, SelectionChangedEventArgs? evt) { private void AreaCommitmentTypeList_SelectionChanged(object? sender, SelectionChangedEventArgs? evt) {
@ -90,7 +94,7 @@ namespace Elwig.Windows {
private void AreaCommitmentTypeAddButton_Click(object sender, RoutedEventArgs evt) { private void AreaCommitmentTypeAddButton_Click(object sender, RoutedEventArgs evt) {
if (_actList == null) return; if (_actList == null) return;
_actChanged = true; _actChanged = true;
var item = Context.CreateProxy<AreaComType>(); var item = new AreaComType { VtrgId = "", SortId = "" };
_actList.Add(item); _actList.Add(item);
AreaCommitmentTypeList.SelectedItem = item; AreaCommitmentTypeList.SelectedItem = item;
UpdateButtons(); UpdateButtons();

View File

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

View File

@ -1,5 +1,6 @@
using Elwig.Helpers; using Elwig.Helpers;
using Elwig.Models.Entities; using Elwig.Models.Entities;
using LinqKit;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
@ -18,19 +19,19 @@ namespace Elwig.Windows {
private bool _modChanged = false; private bool _modChanged = false;
private bool _modUpdate = false; private bool _modUpdate = false;
private void ModifiersInitEditing() { private async Task ModifiersInitEditing(AppDbContext ctx) {
SeasonList.IsEnabled = false; SeasonList.IsEnabled = false;
var year = (SeasonList.SelectedItem as Season)?.Year; var year = (SeasonList.SelectedItem as Season)?.Year;
_modList = new(Context.Modifiers.Where(m => m.Year == year).OrderBy(m => m.Ordering).ToList()); _modList = new(await ctx.Modifiers.Where(m => m.Year == year).OrderBy(m => m.Ordering).ToListAsync());
_mods = _modList.ToDictionary(m => m.ModId, m => (string?)m.ModId); _mods = _modList.ToDictionary(m => m.ModId, m => (string?)m.ModId);
_modIds = _modList.ToDictionary(m => m, m => m.ModId); _modIds = _modList.ToDictionary(m => m, m => m.ModId);
ControlUtils.RenewItemsSource(SeasonModifierList, _modList, m => (m as Modifier)?.ModId); ControlUtils.RenewItemsSource(SeasonModifierList, _modList, m => (m as Modifier)?.ModId);
SeasonModifierList_SelectionChanged(null, null); SeasonModifierList_SelectionChanged(null, null);
} }
private void ModifiersFinishEditing() { private async Task ModifiersFinishEditing(AppDbContext ctx) {
var year = (SeasonList.SelectedItem as Season)?.Year; var year = (SeasonList.SelectedItem as Season)?.Year;
ControlUtils.RenewItemsSource(SeasonModifierList, Context.Modifiers.Where(m => m.Year == year).OrderBy(m => m.Ordering).ToList(), m => (m as Modifier)?.ModId); ControlUtils.RenewItemsSource(SeasonModifierList, await ctx.Modifiers.Where(m => m.Year == year).OrderBy(m => m.Ordering).ToListAsync(), m => (m as Modifier)?.ModId);
_modList = null; _modList = null;
_mods = null; _mods = null;
_modIds = null; _modIds = null;
@ -43,7 +44,7 @@ namespace Elwig.Windows {
SeasonList.IsEnabled = true; SeasonList.IsEnabled = true;
} }
private async Task ModifiersSave() { private async Task ModifiersSave(AppDbContext ctx) {
if (!_modChanged || _modList == null || _mods == null || _modIds == null) if (!_modChanged || _modList == null || _mods == null || _modIds == null)
return; return;
@ -52,26 +53,26 @@ namespace Elwig.Windows {
var year = (SeasonList.SelectedItem as Season)?.Year; var year = (SeasonList.SelectedItem as Season)?.Year;
foreach (var (modid, _) in _mods.Where(m => m.Value == null)) { foreach (var (modid, _) in _mods.Where(m => m.Value == null)) {
Context.Remove(Context.Modifiers.Find(year, modid)!); ctx.Remove(ctx.Modifiers.Find(year, modid)!);
} }
foreach (var (mod, old) in _modIds) { foreach (var (mod, old) in _modIds) {
mod.ModId = old; mod.ModId = old;
} }
foreach (var (old, modid) in _mods.Where(m => m.Value != null)) { foreach (var (old, modid) in _mods.Where(m => m.Value != null)) {
Context.Update(Context.Modifiers.Find(year, old)!); ctx.Update(ctx.Modifiers.Find(year, old)!);
} }
await Context.SaveChangesAsync(); await ctx.SaveChangesAsync();
foreach (var (old, modid) in _mods.Where(m => m.Value != null)) { foreach (var (old, modid) in _mods.Where(m => m.Value != null)) {
await Context.Database.ExecuteSqlAsync($"UPDATE modifier SET modid = {modid} WHERE (year, modid) = ({year}, {old})"); await ctx.Database.ExecuteSqlAsync($"UPDATE modifier SET modid = {modid} WHERE (year, modid) = ({year}, {old})");
} }
await Context.SaveChangesAsync(); await ctx.SaveChangesAsync();
foreach (var mod in _modList.Where(m => !_modIds.ContainsKey(m))) { foreach (var mod in _modList.Where(m => !_modIds.ContainsKey(m))) {
if (mod.ModId == null) continue; if (mod.ModId == null) continue;
await Context.AddAsync(mod); await ctx.AddAsync(mod);
} }
await Context.SaveChangesAsync(); await ctx.SaveChangesAsync();
} }
private void SeasonModifierUpButton_Click(object sender, RoutedEventArgs evt) { private void SeasonModifierUpButton_Click(object sender, RoutedEventArgs evt) {
@ -102,11 +103,7 @@ namespace Elwig.Windows {
if (_modList == null || SeasonList.SelectedItem is not Season s) return; if (_modList == null || SeasonList.SelectedItem is not Season s) return;
_modChanged = true; _modChanged = true;
var idx = (SeasonModifierList.SelectedIndex != -1) ? SeasonModifierList.SelectedIndex + 1 : _modList.Count; var idx = (SeasonModifierList.SelectedIndex != -1) ? SeasonModifierList.SelectedIndex + 1 : _modList.Count;
var item = new Modifier { var item = new Modifier { Year = s.Year, ModId = "", Name = "" };
Year = s.Year,
ModId = "",
Name = "",
};
_modList.Insert(idx, item); _modList.Insert(idx, item);
SeasonModifierList.SelectedIndex = idx; SeasonModifierList.SelectedIndex = idx;
UpdateButtons(); UpdateButtons();

View File

@ -12,27 +12,27 @@ namespace Elwig.Windows {
private bool _seasonChanged = false; private bool _seasonChanged = false;
private bool _seasonUpdate = false; private bool _seasonUpdate = false;
private void SeasonsInitEditing() { private async Task SeasonsInitEditing(AppDbContext ctx) {
ControlUtils.RenewItemsSource(SeasonList, Context.Seasons.OrderByDescending(s => s.Year).ToList(), s => (s as Season)?.Year); ControlUtils.RenewItemsSource(SeasonList, await ctx.Seasons.OrderByDescending(s => s.Year).ToListAsync(), s => (s as Season)?.Year);
SeasonList_SelectionChanged(null, null); SeasonList_SelectionChanged(null, null);
} }
private void SeasonsFinishEditing() { private async Task SeasonsFinishEditing(AppDbContext ctx) {
ControlUtils.RenewItemsSource(SeasonList, Context.Seasons.OrderByDescending(s => s.Year).ToList(), s => (s as Season)?.Year); ControlUtils.RenewItemsSource(SeasonList, await ctx.Seasons.OrderByDescending(s => s.Year).Include(s => s.Modifiers).ToListAsync(), s => (s as Season)?.Year);
_seasonChanged = false; _seasonChanged = false;
} }
private async Task SeasonsSave() { private async Task SeasonsSave(AppDbContext ctx) {
if (!_seasonChanged || SeasonList.SelectedItem is not Season s) if (!_seasonChanged || SeasonList.SelectedItem is not Season s)
return; return;
Context.Update(s); ctx.Update(s);
await Context.SaveChangesAsync(); await ctx.SaveChangesAsync();
} }
private async void SeasonList_SelectionChanged(object? sender, SelectionChangedEventArgs? evt) { private void SeasonList_SelectionChanged(object? sender, SelectionChangedEventArgs? evt) {
_seasonUpdate = true; _seasonUpdate = true;
if (SeasonList.SelectedItem is Season s) { if (SeasonList.SelectedItem is Season s) {
SeasonModifierList.ItemsSource = await Context.Modifiers.Where(m => m.Year == s.Year).OrderBy(m => m.Ordering).ToListAsync(); SeasonModifierList.ItemsSource = s.Modifiers.OrderBy(m => m.Ordering).ToList();
SeasonMaxKgPerHaInput.Text = s.MaxKgPerHa.ToString(); SeasonMaxKgPerHaInput.Text = s.MaxKgPerHa.ToString();
SeasonVatNormalInput.Text = (s.VatNormal * 100).ToString(); SeasonVatNormalInput.Text = (s.VatNormal * 100).ToString();
SeasonVatFlatrateInput.Text = (s.VatFlatrate * 100).ToString(); SeasonVatFlatrateInput.Text = (s.VatFlatrate * 100).ToString();

View File

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

View File

@ -18,16 +18,16 @@ namespace Elwig.Windows {
private bool _cultChanged = false; private bool _cultChanged = false;
private bool _cultUpdate = false; private bool _cultUpdate = false;
private void WineCultivationsInitEditing() { private async Task WineCultivationsInitEditing(AppDbContext ctx) {
_cultList = new(Context.WineCultivations.OrderBy(c => c.Name).ToList()); _cultList = new(await ctx.WineCultivations.OrderBy(c => c.Name).ToListAsync());
_cults = _cultList.ToDictionary(c => c.CultId, c => (string?)c.CultId); _cults = _cultList.ToDictionary(c => c.CultId, c => (string?)c.CultId);
_cultIds = _cultList.ToDictionary(c => c, c => c.CultId); _cultIds = _cultList.ToDictionary(c => c, c => c.CultId);
ControlUtils.RenewItemsSource(WineCultivationList, _cultList, c => (c as WineCult)?.CultId); ControlUtils.RenewItemsSource(WineCultivationList, _cultList, c => (c as WineCult)?.CultId);
WineCultivationList_SelectionChanged(null, null); WineCultivationList_SelectionChanged(null, null);
} }
private void WineCultivationsFinishEditing() { private async Task WineCultivationsFinishEditing(AppDbContext ctx) {
ControlUtils.RenewItemsSource(WineCultivationList, Context.WineCultivations.OrderBy(c => c.Name).ToList(), c => (c as WineCult)?.CultId); ControlUtils.RenewItemsSource(WineCultivationList, await ctx.WineCultivations.OrderBy(c => c.Name).ToListAsync(), c => (c as WineCult)?.CultId);
_cultList = null; _cultList = null;
_cults = null; _cults = null;
_cultIds = null; _cultIds = null;
@ -37,31 +37,31 @@ namespace Elwig.Windows {
WineCultivationDeleteButton.IsEnabled = false; WineCultivationDeleteButton.IsEnabled = false;
} }
private async Task WineCultivationsSave() { private async Task WineCultivationsSave(AppDbContext ctx) {
if (!_cultChanged || _cultList == null || _cults == null || _cultIds == null) if (!_cultChanged || _cultList == null || _cults == null || _cultIds == null)
return; return;
foreach (var (cultid, _) in _cults.Where(c => c.Value == null)) { foreach (var (cultid, _) in _cults.Where(c => c.Value == null)) {
Context.Remove(Context.WineCultivations.Find(cultid)); ctx.Remove(ctx.WineCultivations.Find(cultid)!);
} }
foreach (var (cult, old) in _cultIds) { foreach (var (cult, old) in _cultIds) {
cult.CultId = old; cult.CultId = old;
} }
foreach (var (old, cultid) in _cults.Where(c => c.Value != null)) { foreach (var (old, cultid) in _cults.Where(c => c.Value != null)) {
Context.Update(Context.WineCultivations.Find(old)); ctx.Update(ctx.WineCultivations.Find(old)!);
} }
await Context.SaveChangesAsync(); await ctx.SaveChangesAsync();
foreach (var (old, cultid) in _cults.Where(c => c.Value != null)) { foreach (var (old, cultid) in _cults.Where(c => c.Value != null)) {
await Context.Database.ExecuteSqlAsync($"UPDATE wine_cultivation SET cultid = {cultid} WHERE cultid = {old}"); await ctx.Database.ExecuteSqlAsync($"UPDATE wine_cultivation SET cultid = {cultid} WHERE cultid = {old}");
} }
await Context.SaveChangesAsync(); await ctx.SaveChangesAsync();
foreach (var cult in _cultList.Where(c => !_cultIds.ContainsKey(c))) { foreach (var cult in _cultList.Where(c => !_cultIds.ContainsKey(c))) {
if (cult.CultId == null) continue; if (cult.CultId == null) continue;
await Context.AddAsync(cult); ctx.Add(cult);
} }
await Context.SaveChangesAsync(); await ctx.SaveChangesAsync();
} }
private void WineCultivationList_SelectionChanged(object? sender, SelectionChangedEventArgs? evt) { private void WineCultivationList_SelectionChanged(object? sender, SelectionChangedEventArgs? evt) {
@ -82,7 +82,7 @@ namespace Elwig.Windows {
private void WineCultivationAddButton_Click(object sender, RoutedEventArgs evt) { private void WineCultivationAddButton_Click(object sender, RoutedEventArgs evt) {
if (_cultList == null) return; if (_cultList == null) return;
_cultChanged = true; _cultChanged = true;
var item = Context.CreateProxy<WineCult>(); var item = new WineCult { CultId = "", Name = "" };
_cultList.Add(item); _cultList.Add(item);
WineCultivationList.SelectedItem = item; WineCultivationList.SelectedItem = item;
UpdateButtons(); UpdateButtons();

View File

@ -133,18 +133,37 @@ namespace Elwig.Windows {
protected override async Task OnRenewContext() { protected override async Task OnRenewContext() {
await base.OnRenewContext(); await base.OnRenewContext();
using var ctx = new AppDbContext();
FillInputs(App.Client); FillInputs(App.Client);
ControlUtils.RenewItemsSource(SeasonList, await Context.Seasons.OrderByDescending(s => s.Year).ToListAsync(), s => (s as Season)?.Year, null, ControlUtils.RenewSourceDefault.First); ControlUtils.RenewItemsSource(SeasonList, await ctx.Seasons
.OrderByDescending(s => s.Year)
.ToListAsync(), s => (s as Season)?.Year, null, ControlUtils.RenewSourceDefault.First);
var year = (SeasonList.SelectedItem as Season)?.Year; var year = (SeasonList.SelectedItem as Season)?.Year;
ControlUtils.RenewItemsSource(BranchList, await Context.Branches.OrderBy(b => b.Name).ToListAsync(), b => (b as Branch)?.ZwstId, null, ControlUtils.RenewSourceDefault.First); ControlUtils.RenewItemsSource(BranchList, await ctx.Branches
ControlUtils.RenewItemsSource(WineAttributeList, await Context.WineAttributes.OrderBy(a => a.Name).ToListAsync(), a => (a as WineAttr)?.AttrId, null, ControlUtils.RenewSourceDefault.First); .OrderBy(b => b.Name)
ControlUtils.RenewItemsSource(AreaCommitmentTypeWineVariantInput, await Context.WineVarieties.OrderBy(s => s.Name).ToListAsync(), s => (s as WineVar)?.SortId); .Include(b => b.PostalDest!.AtPlz)
var attrList = await Context.WineAttributes.OrderBy(a => a.Name).Cast<object>().ToListAsync(); .ToListAsync(), b => (b as Branch)?.ZwstId, null, ControlUtils.RenewSourceDefault.First);
ControlUtils.RenewItemsSource(WineAttributeList, await ctx.WineAttributes
.OrderBy(a => a.Name)
.ToListAsync(), a => (a as WineAttr)?.AttrId, null, ControlUtils.RenewSourceDefault.First);
ControlUtils.RenewItemsSource(AreaCommitmentTypeWineVariantInput, await ctx.WineVarieties
.OrderBy(s => s.Name)
.ToListAsync(), s => (s as WineVar)?.SortId);
var attrList = await ctx.WineAttributes.OrderBy(a => a.Name).Cast<object>().ToListAsync();
attrList.Insert(0, new NullItem("")); attrList.Insert(0, new NullItem(""));
ControlUtils.RenewItemsSource(AreaCommitmentTypeWineAttributeInput, attrList, a => (a as WineAttr)?.AttrId); ControlUtils.RenewItemsSource(AreaCommitmentTypeWineAttributeInput, attrList, a => (a as WineAttr)?.AttrId);
ControlUtils.RenewItemsSource(AreaCommitmentTypeList, await Context.AreaCommitmentTypes.OrderBy(v => v.VtrgId).ToListAsync(), v => (v as AreaComType)?.VtrgId, null, ControlUtils.RenewSourceDefault.First); ControlUtils.RenewItemsSource(AreaCommitmentTypeList, await ctx.AreaCommitmentTypes
ControlUtils.RenewItemsSource(WineCultivationList, await Context.WineCultivations.OrderBy(c => c.Name).ToListAsync(), c=> (c as WineCult)?.CultId, null, ControlUtils.RenewSourceDefault.First); .OrderBy(t => t.VtrgId)
ControlUtils.RenewItemsSource(SeasonModifierList, await Context.Modifiers.Where(m => m.Year == year).OrderBy(m => m.Ordering).ToListAsync(), m => (m as Modifier)?.ModId, null, ControlUtils.RenewSourceDefault.First); .Include(t => t.WineVar)
.Include(t => t.WineAttr)
.ToListAsync(), t => (t as AreaComType)?.VtrgId, null, ControlUtils.RenewSourceDefault.First);
ControlUtils.RenewItemsSource(WineCultivationList, await ctx.WineCultivations
.OrderBy(c => c.Name)
.ToListAsync(), c=> (c as WineCult)?.CultId, null, ControlUtils.RenewSourceDefault.First);
ControlUtils.RenewItemsSource(SeasonModifierList, await ctx.Modifiers
.Where(m => m.Year == year)
.OrderBy(m => m.Ordering)
.ToListAsync(), m => (m as Modifier)?.ModId, null, ControlUtils.RenewSourceDefault.First);
} }
protected override void UpdateButtons() { protected override void UpdateButtons() {
@ -174,23 +193,52 @@ namespace Elwig.Windows {
App.FocusOriginHierarchy(); App.FocusOriginHierarchy();
} }
private void EditButton_Click(object sender, RoutedEventArgs evt) { private async Task InitEditing() {
using var ctx = new AppDbContext();
await BranchesInitEditing(ctx);
await WineAttributesInitEditing(ctx);
await WineCultivationsInitEditing(ctx);
await AreaCommitmentTypesInitEditing(ctx);
await SeasonsInitEditing(ctx);
await ModifiersInitEditing(ctx);
}
private async Task Save() {
using var ctx = new AppDbContext();
// FIXME
//using var tx = await ctx.Database.BeginTransactionAsync();
await UpdateClientParameters(App.Client);
await BranchesSave(ctx);
await WineAttributesSave(ctx);
await WineCultivationsSave(ctx);
await AreaCommitmentTypesSave(ctx);
await SeasonsSave(ctx);
await ModifiersSave(ctx);
//await tx.CommitAsync();
}
private async Task FinishEditing() {
using var ctx = new AppDbContext();
await BranchesFinishEditing(ctx);
await WineAttributesFinishEditing(ctx);
await WineCultivationsFinishEditing(ctx);
await AreaCommitmentTypesFinishEditing(ctx);
await SeasonsFinishEditing(ctx);
await ModifiersFinishEditing(ctx);
}
private async void EditButton_Click(object sender, RoutedEventArgs evt) {
IsEditing = true; IsEditing = true;
EditButton.Visibility = Visibility.Hidden; EditButton.Visibility = Visibility.Hidden;
ResetButton.Visibility = Visibility.Visible; ResetButton.Visibility = Visibility.Visible;
BranchesInitEditing(); await InitEditing();
WineAttributesInitEditing();
WineCultivationsInitEditing();
AreaCommitmentTypesInitEditing();
SeasonsInitEditing();
ModifiersInitEditing();
UnlockInputs(); UnlockInputs();
UpdateButtons(); UpdateButtons();
} }
private void CancelButton_Click(object sender, RoutedEventArgs evt) { private async void CancelButton_Click(object sender, RoutedEventArgs evt) {
IsEditing = false; IsEditing = false;
IsCreating = false; IsCreating = false;
EditButton.Visibility = Visibility.Visible; EditButton.Visibility = Visibility.Visible;
@ -199,32 +247,20 @@ namespace Elwig.Windows {
SaveButton.IsEnabled = false; SaveButton.IsEnabled = false;
ResetButton.IsEnabled = false; ResetButton.IsEnabled = false;
Context.ChangeTracker.Clear(); await FinishEditing();
BranchesFinishEditing();
WineCultivationsFinishEditing();
WineAttributesFinishEditing();
AreaCommitmentTypesFinishEditing();
SeasonsFinishEditing();
ModifiersFinishEditing();
ClearInputStates(); ClearInputStates();
FillInputs(App.Client); FillInputs(App.Client);
LockInputs(); LockInputs();
} }
private void ResetButton_Click(object sender, RoutedEventArgs evt) { private async void ResetButton_Click(object sender, RoutedEventArgs evt) {
_branchChanged = false; _branchChanged = false;
_attrChanged = false; _attrChanged = false;
_cultChanged = false; _cultChanged = false;
_modChanged = false; _modChanged = false;
Context.ChangeTracker.Clear();
BranchesInitEditing(); await InitEditing();
WineAttributesInitEditing();
WineCultivationsInitEditing();
AreaCommitmentTypesInitEditing();
SeasonsInitEditing();
ModifiersInitEditing();
ClearInputStates(); ClearInputStates();
FillInputs(App.Client); FillInputs(App.Client);
@ -233,13 +269,7 @@ namespace Elwig.Windows {
private async void SaveButton_Click(object sender, RoutedEventArgs evt) { private async void SaveButton_Click(object sender, RoutedEventArgs evt) {
try { try {
await UpdateClientParameters(App.Client); await Save();
await BranchesSave();
await WineAttributesSave();
await WineCultivationsSave();
await AreaCommitmentTypesSave();
await SeasonsSave();
await ModifiersSave();
} catch (Exception exc) { } catch (Exception exc) {
var str = "Der Eintrag konnte nicht in der Datenbank aktualisiert werden!\n\n" + exc.Message; var str = "Der Eintrag konnte nicht in der Datenbank aktualisiert werden!\n\n" + exc.Message;
if (exc.InnerException != null) str += "\n\n" + exc.InnerException.Message; if (exc.InnerException != null) str += "\n\n" + exc.InnerException.Message;
@ -254,12 +284,7 @@ namespace Elwig.Windows {
SaveButton.IsEnabled = false; SaveButton.IsEnabled = false;
ResetButton.IsEnabled = false; ResetButton.IsEnabled = false;
BranchesFinishEditing(); await FinishEditing();
WineAttributesFinishEditing();
WineCultivationsFinishEditing();
AreaCommitmentTypesFinishEditing();
SeasonsFinishEditing();
ModifiersFinishEditing();
ClearInputStates(); ClearInputStates();
FillInputs(App.Client); FillInputs(App.Client);