Generic Controller

pull/1/head
Sascha 2023-01-12 09:30:51 +07:00
parent 332891d5ba
commit 23405f8ded
15 changed files with 78 additions and 87 deletions

@ -504,7 +504,7 @@ namespace Gremlin_BlazorServer.Data.DBClasses
};
MetaDataSetter.ForImport(newAccount, "Tester", "Seeding data for testing.");
AccountType accountType = gremlinDb.AccountTypes.FirstOrDefault(a => a.AccountTypeCode == "FPC")!;
AccountType? accountType = gremlinDb.AccountTypes.FirstOrDefault(a => a.AccountTypeCode == "FPC")!;
newAccount.AccountType = accountType;
SubMarket subMarket = gremlinDb.SubMarkets.First(a => a.SubMarketCode == "CEN");
@ -771,7 +771,7 @@ namespace Gremlin_BlazorServer.Data.DBClasses
// AccountType aus DB laden, damit der Datensatz vom Context verfolgt wird und EF Core nicht versucht, diesen standardmäßig neu anzulegen.
if (account.AccountType.AccountTypeCode != "")
{
AccountType accountType = gremlinDb.AccountTypes.First(a => a.AccountTypeCode == account.AccountType.AccountTypeCode);
AccountType? accountType = gremlinDb.AccountTypes.First(a => a.AccountTypeCode == account.AccountType.AccountTypeCode);
account.AccountType = accountType;
account.AccountType = ResolveAccountType(gremlinDb, account.AccountType.AccountTypeCode);
@ -1049,7 +1049,7 @@ namespace Gremlin_BlazorServer.Data.DBClasses
foreach (Account account in accountsReadFromFile)
{
// AccountType aus DB laden, damit der Datensatz vom Context verfolgt wird und EF Core nicht versucht, diesen standardmäßig neu anzulegen.
AccountType accountType = gremlinDb.AccountTypes.First(a => a.AccountTypeCode == account.AccountType.AccountTypeCode);
AccountType? accountType = gremlinDb.AccountTypes.First(a => a.AccountTypeCode == account.AccountType.AccountTypeCode);
account.AccountType = accountType;
SubMarket subMarket = gremlinDb.SubMarkets.First(a => a.SubMarketCode == account.SubMarket.SubMarketCode);
@ -1858,7 +1858,7 @@ namespace Gremlin_BlazorServer.Data.DBClasses
catch { return new(); }
}
public static AccountType ResolveAccountType(GremlinDb db, string accountTypeCode)
public static AccountType? ResolveAccountType(GremlinDb db, string accountTypeCode)
{
try { return db.AccountTypes.First(account => account.AccountTypeCode == accountTypeCode); }
catch { return new(); }

@ -725,7 +725,7 @@ namespace Gremlin_BlazorServer.Data.DBClasses
foreach (Account account in accountsReadFromFile)
{
// AccountType aus DB laden, damit der Datensatz vom Context verfolgt wird und EF Core nicht versucht, diesen standardmäßig neu anzulegen.
AccountType accountType = gremlinDb.AccountTypes.First(a => a.AccountTypeCode == account.AccountType.AccountTypeCode);
AccountType? accountType = gremlinDb.AccountTypes.First(a => a.AccountTypeCode == account.AccountType.AccountTypeCode);
account.AccountType = accountType;
account.AccountType = DbHelper.ResolveAccountType(gremlinDb, account.AccountType.AccountTypeCode);

@ -6,66 +6,49 @@ namespace Gremlin_BlazorServer.Data.DBClasses
{
public class GremlinDb : DbContext
{
public DbSet<Contact> Contacts { get; set; }
public DbSet<Account> Accounts { get; set; }
public DbSet<Quote> Quotes { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<LineItem> LineItems { get; set; }
public DbSet<CustomDescription> CustomDescriptions { get; set; }
public DbSet<ProductLine> ProductLines { get; set; }
public DbSet<AccountType> AccountTypes { get; set; }
public DbSet<SubMarket> SubMarkets { get; set; }
public DbSet<RuSettings> RuSettings { get; set; }
public DbSet<RegisteredUser> RegisteredUser { get; set; }
public DbSet<Contact>? Contacts { get; set; }
public DbSet<Account>? Accounts { get; set; }
public DbSet<Quote>? Quotes { get; set; }
public DbSet<Product>? Products { get; set; }
public DbSet<LineItem>? LineItems { get; set; }
public DbSet<CustomDescription>? CustomDescriptions { get; set; }
public DbSet<ProductLine>? ProductLines { get; set; }
public DbSet<AccountType>? AccountTypes { get; set; }
public DbSet<SubMarket>? SubMarkets { get; set; }
public DbSet<RuSettings>? RuSettings { get; set; }
public DbSet<RegisteredUser>? RegisteredUser { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//string connectionString = $"server={Properties.Settings.Default.server};" +
// $"port={Properties.Settings.Default.port};" +
// $"database={Properties.Settings.Default.database};" +
// $"user={Properties.Settings.Default.user};" +
// $"password={Encryption.ToInsecureString(Encryption.DecryptString(Properties.Settings.Default.password))};" +
// $"SslMode={Properties.Settings.Default.SslMode};" +
// $"SslCa={Properties.Settings.Default.SslCA}";
string connectionString = $"server=woitschetzki.de;" +
$"port=3306;" +
$"database=regulus;" +
$"user=root;" +
$"password=lungretter1;" +
$"SslMode=;" +
$"SslCa=";
const string connectionString = "server=woitschetzki.de;port=3306;database=regulus;user=root;password=lungretter1;SslMode=;SslCa=";
try
{
optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)).EnableSensitiveDataLogging().EnableDetailedErrors();
}
catch (Exception ex)
catch (Exception e)
{
Debug.WriteLine(ex);
//ChooseDB chooseDB = new();
//_ = chooseDB.ShowDialog();
Debug.WriteLine(e);
OnConfiguring(optionsBuilder);
throw;
}
}
void ConfigureAutoIncludeFor<T>(ModelBuilder modelBuilder) where T: class
{
var type = typeof(T);
foreach (var property in type.GetProperties())
{
if (typeof(IMetadata).IsAssignableFrom(property.PropertyType)
||
property.PropertyType.IsGenericType
&& typeof(IList<>).IsAssignableFrom(property.PropertyType.GetGenericTypeDefinition()))
{
modelBuilder.Entity<T>().Navigation(e => property.GetValue(e)).AutoInclude();
}
}
}
// void ConfigureAutoIncludeFor<T>(ModelBuilder modelBuilder) where T: class
// {
// var type = typeof(T);
//
// foreach (var property in type.GetProperties())
// {
// if (typeof(IMetadata).IsAssignableFrom(property.PropertyType)
// ||
// property.PropertyType.IsGenericType
// && typeof(IList<>).IsAssignableFrom(property.PropertyType.GetGenericTypeDefinition()))
// {
// modelBuilder.Entity<T>().Navigation(e => property.GetValue(e)).AutoInclude();
// }
// }
// }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
@ -76,6 +59,7 @@ namespace Gremlin_BlazorServer.Data.DBClasses
//TO BE TESTED!
modelBuilder.ApplyConfigurationsFromAssembly(typeof(GremlinDb).Assembly);
//AutoInclude all NavigationParameters in Entities
modelBuilder.Entity<Account>().Navigation(db => db.Contacts).AutoInclude();
modelBuilder.Entity<Account>().Navigation(db => db.AccountType).AutoInclude();
modelBuilder.Entity<Account>().Navigation(db => db.SubMarket).AutoInclude();
@ -87,11 +71,13 @@ namespace Gremlin_BlazorServer.Data.DBClasses
modelBuilder.Entity<Quote>().Navigation(db => db.Recipient).AutoInclude();
modelBuilder.Entity<Quote>().Navigation(db => db.LineItems).AutoInclude();
//Takes too much time
//modelBuilder.Entity<ProductLine>().Navigation(db => db.Products).AutoInclude();
modelBuilder.Entity<Product>().Navigation(db => db.CustomDescription).AutoInclude();
modelBuilder.Entity<Product>().Navigation(db => db.ProductLine).AutoInclude();
//Generic AutoInclude method not yet working
//ConfigureAutoIncludeFor<Account>(modelBuilder);
////Fluent-Konfiguration einzeln für eine Entity aufrufen:

@ -9,7 +9,7 @@
//foreign keys:
public IList<Contact> Contacts { get; set; }
public uint ParentAccountId { get; set; }
public AccountType AccountType { get; set; }
public AccountType? AccountType { get; set; }
public SubMarket SubMarket { get; set; }
public IList<CustomDescription> CustomDescriptions { get; set; }

@ -3,7 +3,7 @@
@using Gremlin_BlazorServer.Data.EntityClasses;
@using Gremlin_BlazorServer.Services;
@inject GenericController<AccountType> AccountTypeService
@inject GenericTypeController<AccountType> AccountTypeService
<h1>AccountTypes</h1>
<DataGrid TItem="AccountType" Data="@accountTypes" SelectedRow="@selectedAccountType" SelectedRowChanged="@OnSelectedAccountTypeChanged" Editable ShowPager Bordered Hoverable Sortable Filterable Striped Responsive>
@ -31,7 +31,7 @@
protected override async Task OnInitializedAsync()
{
accountTypes = await Task.Run(() => AccountTypeService.GetAsync());
accountTypes = await Task.Run(() => AccountTypeService.GetAllAsync());
selectedAccountType = accountTypes.First();
}

@ -3,7 +3,7 @@
@using Gremlin_BlazorServer.Data.EntityClasses;
@using Gremlin_BlazorServer.Services;
@inject GenericController<Account> AccountService
@inject GenericTypeController<Account> AccountService
<h1>Accounts</h1>
<DataGrid TItem="Account" Data="@accounts" SelectedRow="@selectedAccount" SelectedRowChanged="@OnSelectedAccountChanged" Editable ShowPager Bordered Hoverable Sortable Filterable Striped Responsive>
@ -35,7 +35,7 @@
protected override async Task OnInitializedAsync()
{
accounts = await Task.Run(() => AccountService.GetAsync());
accounts = await Task.Run(() => AccountService.GetAllAsync());
selectedAccount = accounts.First();
}

@ -3,7 +3,7 @@
@using Gremlin_BlazorServer.Data.EntityClasses;
@using Gremlin_BlazorServer.Services;
@inject GenericController<Contact> ContactService
@inject GenericTypeController<Contact> ContactService
<h1>Contacts</h1>
@ -36,7 +36,7 @@
protected override async Task OnInitializedAsync()
{
contacts = await Task.Run(() => ContactService.GetAsync());
contacts = await Task.Run(() => ContactService.GetAllAsync());
selectedContact = contacts.First();
}

@ -3,8 +3,6 @@
@using Gremlin_BlazorServer.Data.EntityClasses;
@using Gremlin_BlazorServer.Services;
@inject GenericController<ProductLine> ProductLineService
@inject GenericController<Product> ProductService
@inject GenericController GenericController
<h1>ProductLines</h1>
@ -17,7 +15,7 @@
<h2>@selectedProductLine.ProductLineCode: @selectedProductLine.ProductLineDescription</h2>
<DataGrid TItem="Product" Data="@selectedProductLine.Products" Editable ShowPager Bordered Hoverable Sortable Filterable Striped Responsive>
<DataGrid TItem="Product" Data="@productsOfSelectedProductLine" Editable ShowPager Bordered Hoverable Sortable Filterable Striped Responsive>
<DataGridCommandColumn />
<DataGridColumn Field="@nameof(Product.ProductNumber)" Caption="ProductNumber" Filterable Sortable Editable/>
<DataGridColumn Field="@nameof(Product.OptionNumber)" Caption="OptionNumber" Filterable Sortable Editable/>
@ -33,14 +31,15 @@
protected override async Task OnParametersSetAsync()
{
productLines = await Task.Run(() => ProductLineService.GetAsync());
productLines = GenericController.GetAll<ProductLine>();
//selectedProductLine = productLines.First();
//await OnSelectedProductLineChanged(selectedProductLine);
}
private async Task OnSelectedProductLineChanged(ProductLine pL)
private Task OnSelectedProductLineChanged(ProductLine pL)
{
selectedProductLine = pL;
productsOfSelectedProductLine = await GenericController.GetAllAsync<Product>((product) => product.ProductLine == selectedProductLine);
productsOfSelectedProductLine = GenericController.GetAll<Product>((product) => product.ProductLine == selectedProductLine);
return Task.CompletedTask;
}
}

@ -4,7 +4,7 @@
@using Gremlin_BlazorServer.Services;
@using System.Globalization
@inject GenericController<Product> ProductService
@inject GenericTypeController<Product> ProductService
<h1>Products</h1>
<DataGrid TItem="Product" Data="@products" SelectedRow="@selectedProduct" SelectedRowChanged="@OnSelectedProductChanged" Editable ShowPager Bordered Hoverable Sortable Filterable Striped Responsive>
@ -36,7 +36,7 @@
protected override async Task OnInitializedAsync()
{
products = await Task.Run(() => ProductService.GetAsync());
products = await Task.Run(() => ProductService.GetAllAsync());
selectedProduct = products.First();
OnSelectedProductChanged(selectedProduct);
}

@ -5,10 +5,10 @@
@using System.Globalization;
@using System.Diagnostics
@inject GenericController<Quote> QuoteService
@inject GenericController<Contact> ContactService
@inject GenericController<Account> AccountService
@inject GenericController<CustomDescription> CustomDescriptionService
@inject GenericTypeController<Quote> QuoteService
@inject GenericTypeController<Contact> ContactService
@inject GenericTypeController<Account> AccountService
@inject GenericTypeController<CustomDescription> CustomDescriptionService
@inject QuoteHandling QuoteHandling
@inject NavigationManager NavigationManager
@ -106,9 +106,9 @@
CustomDescription customDescriptionOfSelectedLineItem = new();
private readonly CultureInfo cultureInfo = new("de-DE");
protected override async Task OnInitializedAsync()
protected override async Task OnParametersSetAsync()
{
contacts = await ContactService.GetAsync();
contacts = await ContactService.GetAllAsync();
selectedContact = contacts.First();
//Resolve account if not present

@ -4,7 +4,7 @@
@using Gremlin_BlazorServer.Services;
@using System.Globalization;
@inject GenericController<Quote> QuoteService
@inject GenericTypeController<Quote> QuoteService
<Divider DividerType="DividerType.TextContent" Text="Quotes"/>
<NavLink class="nav-link" href="Quotes/QuoteAdd">
@ -90,7 +90,7 @@
protected override async Task OnInitializedAsync()
{
quotes = await Task.Run(() => QuoteService.GetAsync());
quotes = await Task.Run(() => QuoteService.GetAllAsync());
selectedQuote = quotes.First();
await OnSelectedQuoteChanged(selectedQuote);
}

@ -12,15 +12,16 @@ builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddScoped<GremlinDb>();
builder.Services.AddScoped<GenericController<Contact>>();
builder.Services.AddScoped<GenericController<Account>>();
builder.Services.AddScoped<GenericController<AccountType>>();
builder.Services.AddScoped<GenericController<ProductLine>>();
builder.Services.AddScoped<GenericController<Product>>();
builder.Services.AddScoped<GenericController<Quote>>();
builder.Services.AddScoped<GenericController<CustomDescription>>();
builder.Services.AddScoped<GenericController>();
builder.Services.AddScoped<GenericTypeController<Contact>>();
builder.Services.AddScoped<GenericTypeController<Account>>();
builder.Services.AddScoped<GenericTypeController<AccountType>>();
builder.Services.AddScoped<GenericTypeController<ProductLine>>();
builder.Services.AddScoped<GenericTypeController<Product>>();
builder.Services.AddScoped<GenericTypeController<Quote>>();
builder.Services.AddScoped<GenericTypeController<CustomDescription>>();
builder.Services.AddScoped<QuoteHandling>();
builder.Services.AddScoped<LineItemService>();
builder.Services.AddScoped<LoginService>();

@ -7,9 +7,13 @@ namespace Gremlin_BlazorServer.Services
{
private readonly GremlinDb gremlinDb = new();
public async Task<List<TResult>> GetAllAsync<TResult>(Predicate<TResult> search) where TResult : class, IMetadata
public List<T> GetAll<T>() where T : class, IMetadata
{
return await Task.Run(() => gremlinDb.Set<TResult>().AsEnumerable<TResult>().Where(t => search(t)).ToList());
return gremlinDb.Set<T>().ToList();
}
public List<TResult> GetAll<TResult>(Predicate<TResult> search) where TResult : class, IMetadata
{
return gremlinDb.Set<TResult>().AsEnumerable<TResult>().Where(t => search(t)).ToList();
}
}
}

@ -1,13 +1,14 @@
using Gremlin_BlazorServer.Data.DBClasses;
using Gremlin_BlazorServer.Data.EntityClasses;
using Microsoft.EntityFrameworkCore;
namespace Gremlin_BlazorServer.Services;
public class GenericController<T> where T : class
public class GenericTypeController<T> where T : class
{
private readonly GremlinDb gremlinDb = new();
public async Task<List<T>> GetAsync()
public async Task<List<T>> GetAllAsync()
{
try
{

@ -6,8 +6,8 @@ namespace Gremlin_BlazorServer.Services
{
public class TexService
{
private readonly GenericController<Account> accountService = new();
private readonly GenericController<CustomDescription> customDescriptionService = new();
private readonly GenericTypeController<Account> accountService = new();
private readonly GenericTypeController<CustomDescription> customDescriptionService = new();
public async Task<StringBuilder> CreateTexAsync(Quote quote)
{