Make methods in Utils async

This commit is contained in:
2023-03-22 23:30:04 +01:00
parent 51b9dcbf41
commit 3318db42ba
2 changed files with 10 additions and 7 deletions

View File

@ -7,6 +7,7 @@ using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Diagnostics; using System.Diagnostics;
using System.Windows.Controls.Primitives; using System.Windows.Controls.Primitives;
using Microsoft.EntityFrameworkCore;
namespace Elwig.Helpers { namespace Elwig.Helpers {
public static class Utils { public static class Utils {
@ -86,13 +87,14 @@ namespace Elwig.Helpers {
}); });
} }
public static bool MgNrExists(AppDbContext ctx, int mgnr) { public static async Task<bool> MgNrExists(AppDbContext ctx, int mgnr) {
return ctx.Members.Find(mgnr) != null; return await ctx.Members.FindAsync(mgnr) != null;
} }
public static int NextMgNr(AppDbContext ctx) { public static async Task<int> NextMgNr(AppDbContext ctx) {
int c = ctx.Members.Select(m => m.MgNr).Min(); int c = await ctx.Members.Select(m => m.MgNr).MinAsync();
ctx.Members.OrderBy(m => m.MgNr).Select(m => m.MgNr).ToList().ForEach(a => { if (a <= c + 100) c = a; }); (await ctx.Members.OrderBy(m => m.MgNr).Select(m => m.MgNr).ToListAsync())
.ForEach(a => { if (a <= c + 100) c = a; });
return c + 1; return c + 1;
} }
} }

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using System.Windows.Controls; using System.Windows.Controls;
using Elwig.Models; using Elwig.Models;
@ -265,7 +266,7 @@ namespace Elwig.Helpers {
} }
int nr = int.Parse(input.Text); int nr = int.Parse(input.Text);
if (nr != m?.MgNr && Utils.MgNrExists(ctx, nr)) { if (nr != m?.MgNr && Utils.MgNrExists(ctx, nr).GetAwaiter().GetResult()) {
return new(false, "Mitgliedsnummer wird bereits verwendet"); return new(false, "Mitgliedsnummer wird bereits verwendet");
} }
@ -278,7 +279,7 @@ namespace Elwig.Helpers {
return res; return res;
} else if (!required && input.Text.Length == 0) { } else if (!required && input.Text.Length == 0) {
return new(true, null); return new(true, null);
} else if (!Utils.MgNrExists(ctx, int.Parse(input.Text))) { } else if (!Utils.MgNrExists(ctx, int.Parse(input.Text)).GetAwaiter().GetResult()) {
return new(false, "Ein Mitglied mit dieser Mitgliedsnummer existiert nicht"); return new(false, "Ein Mitglied mit dieser Mitgliedsnummer existiert nicht");
} }