94 lines
3.7 KiB
C#
94 lines
3.7 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Elwig.Models;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.IO;
|
|
using System;
|
|
using System.Windows;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Elwig.Helpers {
|
|
public class AppDbContext : DbContext {
|
|
|
|
public DbSet<Country> Countries { get; set; }
|
|
public DbSet<Currency> Currencies { get; set; }
|
|
public DbSet<Member> Members { get; set; }
|
|
public DbSet<BillingAddr> BillingAddresses { get; set; }
|
|
public DbSet<AT_Gem> Gemeinden { get; set; }
|
|
public DbSet<AT_Kg> Katastralgemeinden { get; set; }
|
|
public DbSet<AT_Ort> Orte { get; set; }
|
|
public DbSet<AT_Plz> Postleitzahlen { get; set; }
|
|
public DbSet<AT_PlzDest> PlzDestinations { get; set; }
|
|
public DbSet<PostalDest> PostalDestinations { get; set; }
|
|
public DbSet<Branch> Branches { get; set; }
|
|
public DbSet<WbKg> WbKgs { get; set; }
|
|
public DbSet<WbRd> WbRde { get; set; }
|
|
public DbSet<AreaCom> AreaCommitments { get; set; }
|
|
public DbSet<AreaComParcel> AreaCommitmentParcels { get; set; }
|
|
public DbSet<AreaComAttr> AreaCommitmentAttributes { get; set; }
|
|
public DbSet<Contract> Contracts { get; set; }
|
|
public DbSet<WineOrigin> WineOrigins { get; set; }
|
|
public DbSet<WineAttr> WineAttributes { get; set; }
|
|
public DbSet<WineCult> WineCultivations { get; set; }
|
|
public DbSet<WineQual> WineQualities { get; set; }
|
|
public DbSet<WineVar> WineVarieties { get; set; }
|
|
public DbSet<Season> Seasons { get; set; }
|
|
public DbSet<Delivery> Deliveries { get; set; }
|
|
public DbSet<DeliveryPart> DeliveryParts { get; set; }
|
|
|
|
private readonly StreamWriter? LogFile = null;
|
|
|
|
public AppDbContext() {
|
|
if (App.Config.DatabaseLog != null) {
|
|
try {
|
|
var file = File.Open(App.Config.DatabaseLog, FileMode.Append, FileAccess.Write, FileShare.Write);
|
|
LogFile = new(file) {
|
|
AutoFlush = true
|
|
};
|
|
} catch (Exception e) {
|
|
MessageBox.Show($"Unable to open database log file:\n\n{e.Message}", "Database Log", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
|
|
optionsBuilder.UseSqlite($"Data Source=\"{App.Config.DatabaseFile}\"; Foreign Keys=True; Mode=ReadWrite; Cache=Default");
|
|
optionsBuilder.UseLazyLoadingProxies();
|
|
optionsBuilder.LogTo(Log, LogLevel.Information);
|
|
base.OnConfiguring(optionsBuilder);
|
|
}
|
|
|
|
public override void Dispose() {
|
|
base.Dispose();
|
|
LogFile?.Dispose();
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
protected void Log(string msg) {
|
|
LogFile?.WriteLine(msg);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|