Move some Utils functions to AppDbContext
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Elwig.Models;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Elwig.Helpers {
|
||||
public class AppDbContext : DbContext {
|
||||
@ -27,5 +29,27 @@ namespace Elwig.Helpers {
|
||||
optionsBuilder.UseLazyLoadingProxies();
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
|
||||
public async Task<bool> MgNrExists(int mgnr) {
|
||||
return await Members.FindAsync(mgnr) != null;
|
||||
}
|
||||
|
||||
public async Task<bool> VNrExists(int vnr) {
|
||||
return await Contracts.FindAsync(vnr) != null;
|
||||
}
|
||||
|
||||
public async Task<int> NextMgNr() {
|
||||
int c = await Members.Select(m => m.MgNr).MinAsync();
|
||||
(await Members.OrderBy(m => m.MgNr).Select(m => m.MgNr).ToListAsync())
|
||||
.ForEach(a => { if (a <= c + 100) c = a; });
|
||||
return c + 1;
|
||||
}
|
||||
|
||||
public async Task<int> NextVNr() {
|
||||
int c = await Contracts.Select(co => co.VNr).MinAsync();
|
||||
(await Contracts.OrderBy(co => co.VNr).Select(co => co.VNr).ToListAsync())
|
||||
.ForEach(a => { if (a <= c + 100) c = a; });
|
||||
return c + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,27 +86,5 @@ namespace Elwig.Helpers {
|
||||
UseShellExecute = true,
|
||||
});
|
||||
}
|
||||
|
||||
public static async Task<bool> MgNrExists(AppDbContext ctx, int mgnr) {
|
||||
return await ctx.Members.FindAsync(mgnr) != null;
|
||||
}
|
||||
|
||||
public static async Task<bool> VNrExists(AppDbContext ctx, int vnr) {
|
||||
return await ctx.Contracts.FindAsync(vnr) != null;
|
||||
}
|
||||
|
||||
public static async Task<int> NextMgNr(AppDbContext ctx) {
|
||||
int c = await ctx.Members.Select(m => m.MgNr).MinAsync();
|
||||
(await ctx.Members.OrderBy(m => m.MgNr).Select(m => m.MgNr).ToListAsync())
|
||||
.ForEach(a => { if (a <= c + 100) c = a; });
|
||||
return c + 1;
|
||||
}
|
||||
|
||||
public static async Task<int> NextVNr(AppDbContext ctx) {
|
||||
int c = await ctx.Contracts.Select(co => co.VNr).MinAsync();
|
||||
(await ctx.Contracts.OrderBy(co => co.VNr).Select(co => co.VNr).ToListAsync())
|
||||
.ForEach(a => { if (a <= c + 100) c = a; });
|
||||
return c + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -265,7 +265,7 @@ namespace Elwig.Helpers {
|
||||
}
|
||||
|
||||
int nr = int.Parse(input.Text);
|
||||
if (nr != m?.MgNr && Utils.MgNrExists(ctx, nr).GetAwaiter().GetResult()) {
|
||||
if (nr != m?.MgNr && ctx.MgNrExists(nr).GetAwaiter().GetResult()) {
|
||||
return new(false, "Mitgliedsnummer wird bereits verwendet");
|
||||
}
|
||||
|
||||
@ -278,7 +278,7 @@ namespace Elwig.Helpers {
|
||||
return res;
|
||||
} else if (!required && input.Text.Length == 0) {
|
||||
return new(true, null);
|
||||
} else if (!Utils.MgNrExists(ctx, int.Parse(input.Text)).GetAwaiter().GetResult()) {
|
||||
} else if (!ctx.MgNrExists(int.Parse(input.Text)).GetAwaiter().GetResult()) {
|
||||
return new(false, "Ein Mitglied mit dieser Mitgliedsnummer existiert nicht");
|
||||
}
|
||||
|
||||
@ -304,7 +304,7 @@ namespace Elwig.Helpers {
|
||||
}
|
||||
|
||||
int nr = int.Parse(input.Text);
|
||||
if (nr != c?.VNr && Utils.VNrExists(ctx, nr).GetAwaiter().GetResult()) {
|
||||
if (nr != c?.VNr && ctx.VNrExists(nr).GetAwaiter().GetResult()) {
|
||||
return new(false, "Vertragsnummer wird bereits verwendet");
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user