[#82] BaseDataWindow: Fixed error handling after first error to allow saving changes
Test / Run tests (push) Successful in 2m8s
Test / Run tests (push) Successful in 2m8s
This commit is contained in:
@@ -47,26 +47,26 @@ namespace Elwig.Windows {
|
|||||||
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)) {
|
// delete
|
||||||
ctx.Remove(ctx.AreaCommitmentTypes.Find(vtrgid)!);
|
var delete = _acts.Where(a => a.Value == null).Select(a => a.Key).ToList();
|
||||||
}
|
await ctx.AreaCommitmentTypes.Where(v => delete.Contains(v.VtrgId)).ExecuteDeleteAsync();
|
||||||
foreach (var (attr, old) in _actIds) {
|
|
||||||
attr.VtrgId = old;
|
|
||||||
}
|
|
||||||
foreach (var (old, vtrgid) in _acts.Where(a => a.Value != null)) {
|
|
||||||
ctx.Update(ctx.AreaCommitmentTypes.Find(old)!);
|
|
||||||
}
|
|
||||||
await ctx.SaveChangesAsync();
|
|
||||||
|
|
||||||
foreach (var (old, vtrgid) in _acts.Where(a => a.Value != null)) {
|
// update
|
||||||
|
ctx.UpdateRange(_actList
|
||||||
|
.Where(_actIds.ContainsKey)
|
||||||
|
.Select(t => {
|
||||||
|
t.VtrgId = _actIds[t];
|
||||||
|
ctx.Entry(t).State = EntityState.Added;
|
||||||
|
return t;
|
||||||
|
}));
|
||||||
|
await ctx.SaveChangesAsync();
|
||||||
|
foreach (var (old, vtrgid) in _acts.Where(a => a.Value != null && a.Key != a.Value)) {
|
||||||
await ctx.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 ctx.SaveChangesAsync();
|
|
||||||
|
|
||||||
foreach (var type in _actList.Where(a => !_actIds.ContainsKey(a))) {
|
// insert
|
||||||
if (type.VtrgId == null) continue;
|
ctx.AddRange(_actList
|
||||||
ctx.Add(type);
|
.Where(t => !_actIds.ContainsKey(t) && t.VtrgId != null));
|
||||||
}
|
|
||||||
await ctx.SaveChangesAsync();
|
await ctx.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,7 +125,7 @@ namespace Elwig.Windows {
|
|||||||
var id = AreaCommitmentTypeIdInput.Text;
|
var id = AreaCommitmentTypeIdInput.Text;
|
||||||
if (old != null) _acts[old] = id;
|
if (old != null) _acts[old] = id;
|
||||||
attr.VtrgId = id;
|
attr.VtrgId = id;
|
||||||
attr.SortId = v?.SortId;
|
attr.SortId = v?.SortId ?? "";
|
||||||
attr.AttrId = a?.AttrId;
|
attr.AttrId = a?.AttrId;
|
||||||
attr.MinKgPerHa = AreaCommitmentTypeMinKgPerHaInput.Text.Length > 0 ? int.Parse(AreaCommitmentTypeMinKgPerHaInput.Text) : null;
|
attr.MinKgPerHa = AreaCommitmentTypeMinKgPerHaInput.Text.Length > 0 ? int.Parse(AreaCommitmentTypeMinKgPerHaInput.Text) : null;
|
||||||
attr.PenaltyPerKg = AreaCommitmentTypePenaltyPerKgInput.Text.Length > 0 ? decimal.Parse(AreaCommitmentTypePenaltyPerKgInput.Text) : null;
|
attr.PenaltyPerKg = AreaCommitmentTypePenaltyPerKgInput.Text.Length > 0 ? decimal.Parse(AreaCommitmentTypePenaltyPerKgInput.Text) : null;
|
||||||
|
|||||||
@@ -41,25 +41,26 @@ namespace Elwig.Windows {
|
|||||||
if (!_branchChanged || _branchList == null || _branches == null || _branchIds == null)
|
if (!_branchChanged || _branchList == null || _branches == null || _branchIds == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
// delete
|
||||||
var deleteZwstIds = _branches.Where(b => b.Value == null).Select(b => b.Key).ToList();
|
var deleteZwstIds = _branches.Where(b => b.Value == null).Select(b => b.Key).ToList();
|
||||||
await ctx.Branches.Where(b => deleteZwstIds.Contains(b.ZwstId)).ExecuteDeleteAsync();
|
await ctx.Branches.Where(b => deleteZwstIds.Contains(b.ZwstId)).ExecuteDeleteAsync();
|
||||||
|
|
||||||
foreach (var (branch, old) in _branchIds) {
|
// update
|
||||||
branch.ZwstId = old;
|
ctx.UpdateRange(_branchList
|
||||||
}
|
.Where(_branchIds.ContainsKey)
|
||||||
foreach (var (old, zwstid) in _branches.Where(b => b.Value != null)) {
|
.Select(b => {
|
||||||
ctx.Update(ctx.Branches.Find(old)!);
|
b.ZwstId = _branchIds[b];
|
||||||
}
|
ctx.Entry(b).State = EntityState.Added;
|
||||||
|
return b;
|
||||||
|
}));
|
||||||
await ctx.SaveChangesAsync();
|
await ctx.SaveChangesAsync();
|
||||||
|
foreach (var (old, zwstid) in _branches.Where(b => b.Value != null && b.Key != b.Value)) {
|
||||||
foreach (var (old, zwstid) in _branches.Where(b => b.Value != null)) {
|
|
||||||
await ctx.Database.ExecuteSqlAsync($"UPDATE branch SET zwstid = {zwstid} WHERE zwstid = {old}");
|
await ctx.Database.ExecuteSqlAsync($"UPDATE branch SET zwstid = {zwstid} WHERE zwstid = {old}");
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var branch in _branchList.Where(b => !_branchIds.ContainsKey(b))) {
|
// insert
|
||||||
if (branch.ZwstId == null) continue;
|
ctx.AddRange(_branchList
|
||||||
ctx.Add(branch);
|
.Where(b => !_branchIds.ContainsKey(b) && b.ZwstId != null));
|
||||||
}
|
|
||||||
await ctx.SaveChangesAsync();
|
await ctx.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,30 +48,30 @@ namespace Elwig.Windows {
|
|||||||
if (!_modChanged || _modList == null || _mods == null || _modIds == null)
|
if (!_modChanged || _modList == null || _mods == null || _modIds == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
var year = (SeasonList.SelectedItem as Season)?.Year;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
foreach (var mod in _modList) mod.Ordering = ++i;
|
foreach (var mod in _modList) mod.Ordering = ++i;
|
||||||
|
|
||||||
var year = (SeasonList.SelectedItem as Season)?.Year;
|
// delete
|
||||||
foreach (var (modid, _) in _mods.Where(m => m.Value == null)) {
|
var deleteModIds = _mods.Where(b => b.Value == null).Select(b => b.Key).ToList();
|
||||||
ctx.Remove(ctx.Modifiers.Find(year, modid)!);
|
await ctx.Modifiers.Where(b => b.Year == year && deleteModIds.Contains(b.ModId)).ExecuteDeleteAsync();
|
||||||
}
|
|
||||||
foreach (var (mod, old) in _modIds) {
|
|
||||||
mod.ModId = old;
|
|
||||||
}
|
|
||||||
foreach (var (old, modid) in _mods.Where(m => m.Value != null)) {
|
|
||||||
ctx.Update(ctx.Modifiers.Find(year, old)!);
|
|
||||||
}
|
|
||||||
await ctx.SaveChangesAsync();
|
|
||||||
|
|
||||||
|
// update
|
||||||
|
ctx.UpdateRange(_modList
|
||||||
|
.Where(_modIds.ContainsKey)
|
||||||
|
.Select(m => {
|
||||||
|
m.ModId = _modIds[m];
|
||||||
|
ctx.Entry(m).State = EntityState.Added;
|
||||||
|
return m;
|
||||||
|
}));
|
||||||
|
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 ctx.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 ctx.SaveChangesAsync();
|
|
||||||
|
|
||||||
foreach (var mod in _modList.Where(m => !_modIds.ContainsKey(m))) {
|
// insert
|
||||||
if (mod.ModId == null) continue;
|
ctx.AddRange(_modList
|
||||||
await ctx.AddAsync(mod);
|
.Where(m => !_modIds.ContainsKey(m) && m.ModId != null));
|
||||||
}
|
|
||||||
await ctx.SaveChangesAsync();
|
await ctx.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,26 +41,28 @@ namespace Elwig.Windows {
|
|||||||
if (!_attrChanged || _attrList == null || _attrs == null || _attrIds == null)
|
if (!_attrChanged || _attrList == null || _attrs == null || _attrIds == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
// delete
|
||||||
var deleteAttrIds = _attrs.Where(a => a.Value == null).Select(a => a.Key).ToList();
|
var deleteAttrIds = _attrs.Where(a => a.Value == null).Select(a => a.Key).ToList();
|
||||||
await ctx.WineAttributes.Where(a => deleteAttrIds.Contains(a.AttrId)).ExecuteDeleteAsync();
|
await ctx.WineAttributes.Where(a => deleteAttrIds.Contains(a.AttrId)).ExecuteDeleteAsync();
|
||||||
foreach (var (attr, old) in _attrIds) {
|
|
||||||
attr.AttrId = old;
|
|
||||||
}
|
|
||||||
foreach (var (old, attrid) in _attrs.Where(a => a.Value != null)) {
|
|
||||||
ctx.Update(ctx.WineAttributes.Find(old)!);
|
|
||||||
}
|
|
||||||
await ctx.SaveChangesAsync();
|
|
||||||
|
|
||||||
|
// update
|
||||||
|
ctx.UpdateRange(_attrList
|
||||||
|
.Where(_attrIds.ContainsKey)
|
||||||
|
.Select(a => {
|
||||||
|
a.AttrId = _attrIds[a];
|
||||||
|
ctx.Entry(a).State = EntityState.Added;
|
||||||
|
return a;
|
||||||
|
}));
|
||||||
|
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 ctx.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 ctx.Database.ExecuteSqlAsync($"UPDATE area_commitment_type SET vtrgid = (sortid || COALESCE(attrid, '') || COALESCE(disc, '')) WHERE attrid = {attrid}");
|
await ctx.Database.ExecuteSqlAsync($"UPDATE area_commitment_type SET vtrgid = (sortid || COALESCE(attrid, '') || COALESCE(disc, '')) WHERE attrid = {attrid}");
|
||||||
await ctx.Database.ExecuteSqlRawAsync($"UPDATE payment_variant SET data = REPLACE(REPLACE(data, '/{old}\"', '/{attrid}\"'), '/{old}-', '/{attrid}-')");
|
await ctx.Database.ExecuteSqlAsync($"UPDATE payment_variant SET data = REPLACE(REPLACE(data, {$"/{old}\""}, {$"/{attrid}\""}), {$"/{old}-"}, {$"/{attrid}-"})");
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var attr in _attrList.Where(a => !_attrIds.ContainsKey(a))) {
|
// insert
|
||||||
if (attr.AttrId == null) continue;
|
ctx.AddRange(_attrList
|
||||||
ctx.Add(attr);
|
.Where(a => !_attrIds.ContainsKey(a) && a.AttrId != null));
|
||||||
}
|
|
||||||
await ctx.SaveChangesAsync();
|
await ctx.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,25 +41,27 @@ namespace Elwig.Windows {
|
|||||||
if (!_cultChanged || _cultList == null || _cults == null || _cultIds == null)
|
if (!_cultChanged || _cultList == null || _cults == null || _cultIds == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
// delete
|
||||||
var deleteCultIds = _cults.Where(c => c.Value == null).Select(c => c.Key).ToList();
|
var deleteCultIds = _cults.Where(c => c.Value == null).Select(c => c.Key).ToList();
|
||||||
await ctx.WineCultivations.Where(c => deleteCultIds.Contains(c.CultId)).ExecuteDeleteAsync();
|
await ctx.WineCultivations.Where(c => deleteCultIds.Contains(c.CultId)).ExecuteDeleteAsync();
|
||||||
foreach (var (cult, old) in _cultIds) {
|
|
||||||
cult.CultId = old;
|
|
||||||
}
|
|
||||||
foreach (var (old, cultid) in _cults.Where(c => c.Value != null)) {
|
|
||||||
ctx.Update(ctx.WineCultivations.Find(old)!);
|
|
||||||
}
|
|
||||||
await ctx.SaveChangesAsync();
|
|
||||||
|
|
||||||
|
// update
|
||||||
|
ctx.UpdateRange(_cultList
|
||||||
|
.Where(_cultIds.ContainsKey)
|
||||||
|
.Select(c => {
|
||||||
|
c.CultId = _cultIds[c];
|
||||||
|
ctx.Entry(c).State = EntityState.Added;
|
||||||
|
return c;
|
||||||
|
}));
|
||||||
|
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 ctx.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 ctx.Database.ExecuteSqlRawAsync($"UPDATE payment_variant SET data = REPLACE(data, '-{old}\"', '-{cultid}\"')");
|
await ctx.Database.ExecuteSqlAsync($"UPDATE payment_variant SET data = REPLACE(data, {$"-{old}\""}, {$"-{cultid}\""})");
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var cult in _cultList.Where(c => !_cultIds.ContainsKey(c))) {
|
// insert
|
||||||
if (cult.CultId == null) continue;
|
ctx.AddRange(_cultList
|
||||||
ctx.Add(cult);
|
.Where(c => !_cultIds.ContainsKey(c) && c.CultId != null));
|
||||||
}
|
|
||||||
await ctx.SaveChangesAsync();
|
await ctx.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ using System.Windows.Input;
|
|||||||
namespace Elwig.Windows {
|
namespace Elwig.Windows {
|
||||||
public partial class BaseDataWindow : AdministrationWindow {
|
public partial class BaseDataWindow : AdministrationWindow {
|
||||||
|
|
||||||
protected AppDbContext? EditContext;
|
|
||||||
|
|
||||||
public BaseDataWindow() {
|
public BaseDataWindow() {
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
RequiredInputs = [
|
RequiredInputs = [
|
||||||
@@ -189,31 +187,30 @@ namespace Elwig.Windows {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async Task InitEditing() {
|
private async Task InitEditing() {
|
||||||
EditContext = new AppDbContext();
|
using var ctx = new AppDbContext();
|
||||||
await BranchesInitEditing(EditContext);
|
await BranchesInitEditing(ctx);
|
||||||
await WineAttributesInitEditing(EditContext);
|
await WineAttributesInitEditing(ctx);
|
||||||
await WineCultivationsInitEditing(EditContext);
|
await WineCultivationsInitEditing(ctx);
|
||||||
await AreaCommitmentTypesInitEditing(EditContext);
|
await AreaCommitmentTypesInitEditing(ctx);
|
||||||
await SeasonsInitEditing(EditContext);
|
await SeasonsInitEditing(ctx);
|
||||||
await ModifiersInitEditing(EditContext);
|
await ModifiersInitEditing(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task Save() {
|
private async Task Save() {
|
||||||
await UpdateClientParameters(App.Client);
|
await UpdateClientParameters(App.Client);
|
||||||
await UpdateParameters(Utils.CurrentLastSeason);
|
await UpdateParameters(Utils.CurrentLastSeason);
|
||||||
using var tx = await EditContext!.Database.BeginTransactionAsync();
|
using var ctx = new AppDbContext();
|
||||||
await BranchesSave(EditContext!);
|
using var tx = await ctx.Database.BeginTransactionAsync();
|
||||||
await WineAttributesSave(EditContext!);
|
await BranchesSave(ctx);
|
||||||
await WineCultivationsSave(EditContext!);
|
await WineAttributesSave(ctx);
|
||||||
await AreaCommitmentTypesSave(EditContext!);
|
await WineCultivationsSave(ctx);
|
||||||
await SeasonsSave(EditContext!);
|
await AreaCommitmentTypesSave(ctx);
|
||||||
await ModifiersSave(EditContext!);
|
await SeasonsSave(ctx);
|
||||||
|
await ModifiersSave(ctx);
|
||||||
await tx.CommitAsync();
|
await tx.CommitAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task FinishEditing() {
|
private async Task FinishEditing() {
|
||||||
EditContext?.Dispose();
|
|
||||||
EditContext = null;
|
|
||||||
using var ctx = new AppDbContext();
|
using var ctx = new AppDbContext();
|
||||||
await BranchesFinishEditing(ctx);
|
await BranchesFinishEditing(ctx);
|
||||||
await WineAttributesFinishEditing(ctx);
|
await WineAttributesFinishEditing(ctx);
|
||||||
|
|||||||
Reference in New Issue
Block a user