Move some Utils functions to AppDbContext

This commit is contained in:
2023-04-16 11:21:30 +02:00
parent d5b9af541f
commit 0333e8a5c5
5 changed files with 29 additions and 27 deletions

View File

@ -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;
}
}
}