Services: Add Utils.RunForeground() to factor Task.Run and try-catch-Blocks out
Test / Run tests (push) Successful in 2m2s

This commit is contained in:
2026-06-30 02:27:57 +02:00
parent 69efca1cc3
commit beacba6bd9
6 changed files with 188 additions and 291 deletions
+58 -124
View File
@@ -6,7 +6,6 @@ using Elwig.Models.Entities;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows;
using System;
using Elwig.ViewModels;
@@ -726,16 +725,10 @@ namespace Elwig.Services {
}
public static async Task GenerateDeliveryNote(int year, int did, ExportMode mode) {
Mouse.OverrideCursor = Cursors.Wait;
await Task.Run(async () => {
try {
using var doc = await DeliveryNote.Initialize(year, did);
await Utils.ExportDocument(doc, mode, doc.Delivery.LsNr, (doc.Member, $"{DeliveryNote.Name} Nr. {doc.Delivery.LsNr}", $"Im Anhang finden Sie den {DeliveryNote.Name} Nr. {doc.Delivery.LsNr}"));
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
await Utils.RunForeground(async () => {
using var doc = await DeliveryNote.Initialize(year, did);
await Utils.ExportDocument(doc, mode, doc.Delivery.LsNr, (doc.Member, $"{DeliveryNote.Name} Nr. {doc.Delivery.LsNr}", $"Im Anhang finden Sie den {DeliveryNote.Name} Nr. {doc.Delivery.LsNr}"));
});
Mouse.OverrideCursor = null;
}
public static async Task GenerateDeliveryJournal(this DeliveryAdminViewModel vm, ExportSubject subject, ExportMode mode) {
@@ -774,62 +767,42 @@ namespace Elwig.Services {
if (mode == ExportMode.SaveList) {
var filename = InteractionService.SaveFile(DeliveryJournal.Name, DeliveryJournal.Name, "ods");
if (filename != null) {
Mouse.OverrideCursor = Cursors.Wait;
await Task.Run(async () => {
try {
var data = await DeliveryJournalData.FromQuery(query, filterNames);
using var ods = new OdsFile(filename);
await ods.AddTable(data);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
await Utils.RunForeground(async () => {
var data = await DeliveryJournalData.FromQuery(query, filterNames);
using var ods = new OdsFile(filename);
await ods.AddTable(data);
});
Mouse.OverrideCursor = null;
}
} else if (mode == ExportMode.Export) {
var filename = InteractionService.SaveFile(DeliveryJournal.Name, subject == ExportSubject.Selected ? $"Lieferung_{vm.SelectedDelivery?.LsNr}" : $"Lieferungen_{DateTime.Now:yyyy-MM-dd_HH-mm-ss}_{App.ZwstId}", "elwig.zip");
if (filename != null) {
if (!filename.EndsWith(".elwig.zip")) filename += ".elwig.zip";
Mouse.OverrideCursor = Cursors.Wait;
await Task.Run(async () => {
try {
var list = await query
.Select(p => p.Delivery)
.Distinct()
.Include(d => d.Parts).ThenInclude(p => p.PartModifiers)
.ToListAsync();
var wbKgs = list
.SelectMany(d => d.Parts)
.Where(p => p.Kg != null)
.Select(p => p.Kg!)
.DistinctBy(k => k.KgNr)
.OrderBy(k => k.KgNr)
.ToList();
await ElwigData.Export(filename, list, wbKgs, filterNames);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
await Utils.RunForeground(async () => {
var list = await query
.Select(p => p.Delivery)
.Distinct()
.Include(d => d.Parts).ThenInclude(p => p.PartModifiers)
.ToListAsync();
var wbKgs = list
.SelectMany(d => d.Parts)
.Where(p => p.Kg != null)
.Select(p => p.Kg!)
.DistinctBy(k => k.KgNr)
.OrderBy(k => k.KgNr)
.ToList();
await ElwigData.Export(filename, list, wbKgs, filterNames);
});
Mouse.OverrideCursor = null;
}
} else if (mode == ExportMode.Upload && App.Config.SyncUrl != null) {
Mouse.OverrideCursor = Cursors.Wait;
await Task.Run(async () => {
await Utils.RunForeground(async () => {
await SyncService.Upload(App.Config.SyncUrl, App.Config.SyncUrl, App.Config.SyncPassword, query, filterNames);
});
Mouse.OverrideCursor = null;
} else {
Mouse.OverrideCursor = Cursors.Wait;
await Task.Run(async () => {
try {
var data = await DeliveryJournalData.FromQuery(query, filterNames);
using var doc = new DeliveryJournal(string.Join(" / ", filterNames), data);
await Utils.ExportDocument(doc, mode);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
await Utils.RunForeground(async () => {
var data = await DeliveryJournalData.FromQuery(query, filterNames);
using var doc = new DeliveryJournal(string.Join(" / ", filterNames), data);
await Utils.ExportDocument(doc, mode);
});
Mouse.OverrideCursor = null;
}
}
@@ -850,17 +823,11 @@ namespace Elwig.Services {
throw new ArgumentException("Invalid value for ExportSubject");
}
Mouse.OverrideCursor = Cursors.Wait;
await Task.Run(async () => {
try {
var data = await WineQualityStatisticsData.FromQuery(query, App.Client.OrderingMemberList);
using var doc = new WineQualityStatistics(string.Join(" / ", filterNames), data);
await Utils.ExportDocument(doc, mode);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
await Utils.RunForeground(async () => {
var data = await WineQualityStatisticsData.FromQuery(query, App.Client.OrderingMemberList);
using var doc = new WineQualityStatistics(string.Join(" / ", filterNames), data);
await Utils.ExportDocument(doc, mode);
});
Mouse.OverrideCursor = null;
}
public static async Task GenerateLocalityStatistics(this DeliveryAdminViewModel vm, ExportSubject subject) {
@@ -878,17 +845,11 @@ namespace Elwig.Services {
var filename = InteractionService.SaveFile("Lieferstatistik pro Ort", $"Lieferstatistik-{vm.FilterSeason ?? Utils.CurrentLastSeason}", "ods");
if (filename != null) {
Mouse.OverrideCursor = Cursors.Wait;
await Task.Run(async () => {
try {
using var ods = new OdsFile(filename);
var tbl = await WineLocalityStatisticsData.FromQuery(query, filterNames);
await ods.AddTable(tbl);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
await Utils.RunForeground(async () => {
using var ods = new OdsFile(filename);
var tbl = await WineLocalityStatisticsData.FromQuery(query, filterNames);
await ods.AddTable(tbl);
});
Mouse.OverrideCursor = null;
}
}
@@ -921,38 +882,26 @@ namespace Elwig.Services {
if (mode == ExportMode.SaveList) {
var filename = InteractionService.SaveFile(DeliveryDepreciationList.Name, $"{DeliveryDepreciationList.Name}-{vm.FilterSeason ?? Utils.CurrentLastSeason}", "ods");
if (filename != null) {
Mouse.OverrideCursor = Cursors.Wait;
await Task.Run(async () => {
try {
using var ods = new OdsFile(filename);
var tblTotal = await DeliveryJournalData.FromQuery(query, filterNames);
tblTotal.FullName = DeliveryDepreciationList.Name;
tblTotal.Name = "Gesamt";
await ods.AddTable(tblTotal);
foreach (var branch in await ctx.FetchBranches().ToListAsync()) {
var tbl = await DeliveryJournalData.FromQuery(query.Where(p => p.Delivery.ZwstId == branch.ZwstId), filterNames);
tbl.FullName = DeliveryDepreciationList.Name;
tbl.Name = branch.Name;
await ods.AddTable(tbl);
}
} catch (Exception exc) {
InteractionService.ShowException(exc);
await Utils.RunForeground(async () => {
using var ods = new OdsFile(filename);
var tblTotal = await DeliveryJournalData.FromQuery(query, filterNames);
tblTotal.FullName = DeliveryDepreciationList.Name;
tblTotal.Name = "Gesamt";
await ods.AddTable(tblTotal);
foreach (var branch in await ctx.FetchBranches().ToListAsync()) {
var tbl = await DeliveryJournalData.FromQuery(query.Where(p => p.Delivery.ZwstId == branch.ZwstId), filterNames);
tbl.FullName = DeliveryDepreciationList.Name;
tbl.Name = branch.Name;
await ods.AddTable(tbl);
}
});
Mouse.OverrideCursor = null;
}
} else {
Mouse.OverrideCursor = Cursors.Wait;
await Task.Run(async () => {
try {
var data = await DeliveryJournalData.FromQuery(query, filterNames);
using var doc = new DeliveryDepreciationList(string.Join(" / ", filterNames), data);
await Utils.ExportDocument(doc, mode);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
await Utils.RunForeground(async () => {
var data = await DeliveryJournalData.FromQuery(query, filterNames);
using var doc = new DeliveryDepreciationList(string.Join(" / ", filterNames), data);
await Utils.ExportDocument(doc, mode);
});
Mouse.OverrideCursor = null;
}
}
@@ -981,19 +930,13 @@ namespace Elwig.Services {
var filename = InteractionService.SaveFile("Liefermengen", "Liefermengen", "ods");
if (filename != null) {
Mouse.OverrideCursor = Cursors.Wait;
await Task.Run(async () => {
try {
using var ods = new OdsFile(filename);
var tblTotal = await MemberDeliveryData.FromQuery(query, filterNames);
var tbl = await MemberDeliveryPerVarietyData.FromQuery(query, filterNames);
await ods.AddTable(tblTotal);
await ods.AddTable(tbl);
} catch (Exception exc) {
InteractionService.ShowException(exc);
}
await Utils.RunForeground(async () => {
using var ods = new OdsFile(filename);
var tblTotal = await MemberDeliveryData.FromQuery(query, filterNames);
var tbl = await MemberDeliveryPerVarietyData.FromQuery(query, filterNames);
await ods.AddTable(tblTotal);
await ods.AddTable(tbl);
});
Mouse.OverrideCursor = null;
}
}
@@ -1196,8 +1139,7 @@ namespace Elwig.Services {
$"Soll wirklich für {dids.Length:N0} Teillieferung(en) das Attribut\n'{attributeName}' gesetz werden?"))
return;
Mouse.OverrideCursor = Cursors.Wait;
await Task.Run(async () => {
await Utils.RunForeground(async () => {
using (var cnx = await AppDbContext.ConnectAsync()) {
await cnx.ExecuteBatch($"""
UPDATE delivery_part SET attrid = {attrid}
@@ -1208,8 +1150,6 @@ namespace Elwig.Services {
});
} catch (Exception exc) {
InteractionService.ShowException(exc);
} finally {
Mouse.OverrideCursor = null;
}
}
@@ -1226,8 +1166,7 @@ namespace Elwig.Services {
$"Soll wirklich für {dids.Length:N0} Teillieferung(en) der Zu-/Abschlag\n'{modifierName}' hinzugefügt werden?"))
return;
Mouse.OverrideCursor = Cursors.Wait;
await Task.Run(async () => {
await Utils.RunForeground(async () => {
using (var cnx = await AppDbContext.ConnectAsync()) {
await cnx.ExecuteBatch($"""
INSERT INTO delivery_part_modifier (year, did, dpnr, modid)
@@ -1239,8 +1178,6 @@ namespace Elwig.Services {
});
} catch (Exception exc) {
InteractionService.ShowException(exc);
} finally {
Mouse.OverrideCursor = null;
}
}
@@ -1257,8 +1194,7 @@ namespace Elwig.Services {
$"Soll wirklich für {dids.Length:N0} Teillieferung(en) der Zu-/Abschlag\n'{modifierName}' entfernt werden?"))
return;
Mouse.OverrideCursor = Cursors.Wait;
await Task.Run(async () => {
await Utils.RunForeground(async () => {
using (var cnx = await AppDbContext.ConnectAsync()) {
await cnx.ExecuteBatch($"""
DELETE FROM delivery_part_modifier
@@ -1269,8 +1205,6 @@ namespace Elwig.Services {
});
} catch (Exception exc) {
InteractionService.ShowException(exc);
} finally {
Mouse.OverrideCursor = null;
}
}
}