diff --git a/Gremlin_BlazorServer/Data/EntityClasses/Account.cs b/Gremlin_BlazorServer/Data/EntityClasses/Account.cs index c57975f..f9940cd 100644 --- a/Gremlin_BlazorServer/Data/EntityClasses/Account.cs +++ b/Gremlin_BlazorServer/Data/EntityClasses/Account.cs @@ -18,7 +18,7 @@ public class Account : IMetadata public SubMarket? SubMarket { get; set; } public IList? Contacts { get; set; } - public IList SalesReps { get; set; } + public IList? SalesReps { get; set; } // public IList? CustomDescriptions { get; set; } //class properties: diff --git a/Gremlin_BlazorServer/Data/EntityClasses/AccountType.cs b/Gremlin_BlazorServer/Data/EntityClasses/AccountType.cs index 091768a..5138d5d 100644 --- a/Gremlin_BlazorServer/Data/EntityClasses/AccountType.cs +++ b/Gremlin_BlazorServer/Data/EntityClasses/AccountType.cs @@ -6,7 +6,7 @@ public class AccountType : IMetadata { //primary key: //public uint AccountTypeId { get; set; } [Key] //Fluent API .HasKey funktioniert nicht - public string? AccountTypeCode { get; set; } + public string AccountTypeCode { get; set; } = string.Empty; //navigation properties: public IList? Accounts { get; set; } diff --git a/Gremlin_BlazorServer/Data/EntityClasses/LineItem.cs b/Gremlin_BlazorServer/Data/EntityClasses/LineItem.cs index 8776298..7b43de7 100644 --- a/Gremlin_BlazorServer/Data/EntityClasses/LineItem.cs +++ b/Gremlin_BlazorServer/Data/EntityClasses/LineItem.cs @@ -7,7 +7,7 @@ public class LineItem : IMetadata { //foreign keys: public uint QuoteId { get; set; } - public uint ProductId { get; set; } + public uint? ProductId { get; set; } // public uint CustomDescriptionId { get; set; } //navigation properties: diff --git a/Gremlin_BlazorServer/Data/EntityClasses/Product.cs b/Gremlin_BlazorServer/Data/EntityClasses/Product.cs index 1283cd6..7e4b305 100644 --- a/Gremlin_BlazorServer/Data/EntityClasses/Product.cs +++ b/Gremlin_BlazorServer/Data/EntityClasses/Product.cs @@ -10,7 +10,7 @@ public class Product : IMetadata { public CustomDescription? CustomDescription { get; set; } //foreign keys - public string? ProductLineCode { get; set; } + public string ProductLineCode { get; set; } = string.Empty; public uint CustomDescriptionId { get; set; } //Agilent-specific properties: diff --git a/Gremlin_BlazorServer/Data/EntityClasses/ProductLine.cs b/Gremlin_BlazorServer/Data/EntityClasses/ProductLine.cs index c72677f..8ee2d09 100644 --- a/Gremlin_BlazorServer/Data/EntityClasses/ProductLine.cs +++ b/Gremlin_BlazorServer/Data/EntityClasses/ProductLine.cs @@ -5,7 +5,7 @@ namespace Gremlin_BlazorServer.Data.EntityClasses; public class ProductLine : IMetadata { //primary key: //public uint ProductLineId { get; set; } - [Key] public string? ProductLineCode { get; set; } + [Key] public string ProductLineCode { get; set; } = string.Empty; //navigation properties: public IList? Products { get; set; } diff --git a/Gremlin_BlazorServer/Data/EntityClasses/Quote.cs b/Gremlin_BlazorServer/Data/EntityClasses/Quote.cs index 55cda16..1700d84 100644 --- a/Gremlin_BlazorServer/Data/EntityClasses/Quote.cs +++ b/Gremlin_BlazorServer/Data/EntityClasses/Quote.cs @@ -11,8 +11,8 @@ public class Quote : IMetadata { // public uint ProductId { get; set; } //navigation properties: - public Contact? Recipient { get; set; } = new() { Account = new() }; - public SalesRep? SalesRep { get; set; } = new() { Account = new() }; + public Contact Recipient { get; set; } = new() { Account = new() }; + public SalesRep SalesRep { get; set; } = new() { Account = new() }; public IList? LineItems { get; set; } @@ -38,7 +38,7 @@ public class Quote : IMetadata { public bool ShowSinglePrices { get; set; } = true; public bool ShowDiscounts { get; set; } = true; public bool ShowBrutto { get; set; } = true; - public string? QuoteDescription { get; set; } + public string? QuoteDescription { get; set; } = "Gerät"; public string? Tex { get; set; } public string? Description { get; set; } public string? Path { get; set; } diff --git a/Gremlin_BlazorServer/Data/EntityClasses/SalesRep.cs b/Gremlin_BlazorServer/Data/EntityClasses/SalesRep.cs index 6dcb343..c4abb62 100644 --- a/Gremlin_BlazorServer/Data/EntityClasses/SalesRep.cs +++ b/Gremlin_BlazorServer/Data/EntityClasses/SalesRep.cs @@ -9,14 +9,14 @@ public class SalesRep : IMetadata { // public uint QuoteId { get; set; } //navigation properties: - public Account? Account { get; set; } - public IList? ListOfQuotes { get; set; } + public Account Account { get; set; } = new(); + public IList ListOfQuotes { get; set; } = new List(); //class properties: public string? TerritoryId { get; set; } public string? AcademicTitle { get; set; } - public string? FirstName { get; set; } - public string? LastName { get; set; } + public string FirstName { get; set; } = string.Empty; + public string LastName { get; set; } = string.Empty; public byte Gender { get; set; } public string? PhoneNumber { get; set; } public string? MobileNumber { get; set; } diff --git a/Gremlin_BlazorServer/Pages/AccountTypes.razor b/Gremlin_BlazorServer/Pages/AccountTypes.razor index 31066ce..01694f6 100644 --- a/Gremlin_BlazorServer/Pages/AccountTypes.razor +++ b/Gremlin_BlazorServer/Pages/AccountTypes.razor @@ -3,6 +3,7 @@ @using Gremlin_BlazorServer.Data.EntityClasses @inject GenericController GenericController; +@inject ILoadingIndicatorService ApplicationLoadingIndicatorService diff --git a/Gremlin_BlazorServer/Pages/AccountTypes.razor.cs b/Gremlin_BlazorServer/Pages/AccountTypes.razor.cs index d99bb71..ab55027 100644 --- a/Gremlin_BlazorServer/Pages/AccountTypes.razor.cs +++ b/Gremlin_BlazorServer/Pages/AccountTypes.razor.cs @@ -2,25 +2,33 @@ using System.Security.Claims; using Gremlin_BlazorServer.Data.EntityClasses; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; +using NuGet.Packaging; namespace Gremlin_BlazorServer.Pages; public partial class AccountTypes { - private IList? accountTypes; - private AccountType? selectedAccountType; + private readonly IList accountTypes = new List(); + private AccountType selectedAccountType = new(); [CascadingParameter] private Task? AuthenticationStateTask { get; set; } protected override async Task OnInitializedAsync() { if (AuthenticationStateTask != null) { ClaimsPrincipal user = (await AuthenticationStateTask).User; - if (user.Identity is { IsAuthenticated: true }) accountTypes = await GenericController.GetAllAsync("Accounts"); - + if (user.Identity is{ IsAuthenticated: true }) { + await ApplicationLoadingIndicatorService.Show(); + accountTypes.AddRange(await GenericController.GetAllAsync()); + selectedAccountType = accountTypes.First(); + await OnSelectedAccountTypeChanged(selectedAccountType); + await ApplicationLoadingIndicatorService.Hide(); + } await base.OnInitializedAsync(); } } - private async Task OnSelectedAccountTypeChanged(AccountType selectedAccountType) { - if (selectedAccountType != null) this.selectedAccountType = await GenericController.GetAsync(aC => aC.AccountTypeCode == selectedAccountType.AccountTypeCode, "Accounts"); + private async Task OnSelectedAccountTypeChanged(AccountType newSelectedAccountType) { + await ApplicationLoadingIndicatorService.Show(); + selectedAccountType = await GenericController.GetAsync(aC => aC.AccountTypeCode.Equals(newSelectedAccountType.AccountTypeCode), "Accounts"); + await ApplicationLoadingIndicatorService.Hide(); } } \ No newline at end of file diff --git a/Gremlin_BlazorServer/Pages/Accounts.razor.cs b/Gremlin_BlazorServer/Pages/Accounts.razor.cs index 308e683..bf378c1 100644 --- a/Gremlin_BlazorServer/Pages/Accounts.razor.cs +++ b/Gremlin_BlazorServer/Pages/Accounts.razor.cs @@ -2,29 +2,33 @@ using System.Security.Claims; using Blazorise; using Blazorise.DataGrid; using Gremlin_BlazorServer.Data.EntityClasses; +using Gremlin_BlazorServer.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; +using NuGet.Packaging; namespace Gremlin_BlazorServer.Pages; public partial class Accounts { - private IList? accounts; - private Account? selectedAccount; + private readonly IList accounts = new List(); + private Account selectedAccount = new(); [CascadingParameter] private Task? AuthenticationStateTask { get; set; } protected override async Task OnInitializedAsync() { if (AuthenticationStateTask != null) { ClaimsPrincipal user = (await AuthenticationStateTask).User; - if (user.Identity is { IsAuthenticated: true }) accounts = await GenericController.GetAllAsync("AccountType", "SubMarket"); - + if (user.Identity is{ IsAuthenticated: true }) { + accounts.AddRange(await GenericController.GetAllAsync()); + selectedAccount = accounts.First(); + } await base.OnInitializedAsync(); } } - private void OnSelectedAccountChanged(Account selectedAccount) { - this.selectedAccount = selectedAccount; - this.selectedAccount.Contacts = GenericController.GetAll(c => c.AccountId == this.selectedAccount.AccountId); + private async Task OnSelectedAccountChanged(Account newSelectedAccount) { + selectedAccount = newSelectedAccount; + selectedAccount.Contacts = await GenericController.GetAllAsync(c => c.AccountId.Equals(selectedAccount.AccountId)); } private async Task OnImportAccounts(FileChangedEventArgs fileChangedEventArgs) { @@ -47,8 +51,7 @@ public partial class Accounts { private async Task OnRowInsertedAsync(SavedRowItem> account) { Account newAccount = await ResolveAccountAsync(account.Item); - if (newAccount.AccountType == null || newAccount.SubMarket == null) - return; + // if (newAccount.AccountType is null || newAccount.SubMarket is null) return; int count = await GenericController.InsertAsync(newAccount); Console.WriteLine($"Inserted {count} properties for new account {newAccount.AccountId}: {newAccount.AccountName}."); } @@ -69,8 +72,8 @@ public partial class Accounts { private async Task ResolveAccountAsync(Account newAccount) { newAccount.DataModificationByUser = "Gremlin Blazor Server GUI"; newAccount.DataVersionNumber++; - newAccount.AccountType = await GenericController.GetAsync(aT => aT.AccountTypeCode.Equals("SUP")); - newAccount.SubMarket = await GenericController.GetAsync(sM => sM.SubMarketCode.Equals("VEN")); + // newAccount.AccountType = await GenericController.GetAsync(aT => aT.AccountTypeCode.Equals("SUP")); + // newAccount.SubMarket = await GenericController.GetAsync(sM => sM.SubMarketCode.Equals("VEN")); return newAccount; } } \ No newline at end of file diff --git a/Gremlin_BlazorServer/Pages/Contacts.razor.cs b/Gremlin_BlazorServer/Pages/Contacts.razor.cs index a1bd001..7106b47 100644 --- a/Gremlin_BlazorServer/Pages/Contacts.razor.cs +++ b/Gremlin_BlazorServer/Pages/Contacts.razor.cs @@ -3,57 +3,53 @@ using System.Security.Claims; using Blazorise; using Blazorise.DataGrid; using Gremlin_BlazorServer.Data.EntityClasses; +using Gremlin_BlazorServer.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; +using NuGet.Packaging; namespace Gremlin_BlazorServer.Pages; public partial class Contacts { private readonly CultureInfo cultureInfo = new("de-DE"); - private readonly List? selectedContacts; - private readonly int totalContacts; - private IList? contacts; - private Contact? selectedContact; - private Quote? selectedQuote; + private readonly IList contacts = new List(); + private Contact selectedContact = new(); + private Quote selectedQuote = new(); [CascadingParameter] private Task? AuthenticationStateTask { get; set; } protected override async Task OnInitializedAsync() { if (AuthenticationStateTask != null) { ClaimsPrincipal user = (await AuthenticationStateTask).User; - if (user.Identity is { IsAuthenticated: true }) contacts = await GenericController.GetAllAsync(); + if (user.Identity is{ IsAuthenticated: true }) { + contacts.AddRange(await GenericController.GetAllAsync()); + selectedContact = contacts.First(); + } } - await base.OnInitializedAsync(); } - private async Task OnSelectedContactChanged(Contact selectedContact) { - if (selectedContact != null) { - this.selectedContact = selectedContact; - this.selectedContact.Quotes = await GenericController.GetAllAsync(q => q.RecipientId.Equals(this.selectedContact.ContactId)); - } + private async Task OnSelectedContactChanged(Contact newSelectedContact) { + selectedContact = newSelectedContact; + selectedContact.Quotes = await GenericController.GetAllAsync(q => q.RecipientId.Equals(selectedContact.ContactId)); } - private async Task OnSelectedQuoteChanged(Quote selectedQuote) { - if (selectedQuote != null) { - this.selectedQuote = selectedQuote; - this.selectedQuote.LineItems = await GenericController.GetAllAsync(lI => lI.QuoteId.Equals(this.selectedQuote.QuoteId)); - } + private async Task OnSelectedQuoteChanged(Quote newSelectedQuote) { + selectedQuote = newSelectedQuote; + selectedQuote.LineItems = await GenericController.GetAllAsync(lI => lI.QuoteId.Equals(selectedQuote.QuoteId)); } private async Task OnRowInsertedAsync(SavedRowItem> contact) { Contact newContact = await ResolveContactAsync(contact.Item); - if (newContact.Account == null) - return; + // if (newContact.Account is null) return; int count = await GenericController.InsertAsync(newContact); Console.WriteLine($"Inserted {count} properties for new contact {newContact.ContactId}: {newContact.FirstName} {newContact.LastName}."); } private async Task OnRowUpdatedAsync(SavedRowItem> contact) { Contact newContact = await ResolveContactAsync(contact.Item); - if (newContact.Account == null) - return; + // if (newContact.Account is null) return; int count = await GenericController.UpdateAsync(contact.Item); Console.WriteLine($"Updated {count} properties in contact {newContact.ContactId}: {newContact.FirstName} {newContact.LastName}."); } @@ -64,9 +60,9 @@ public partial class Contacts { } private async Task ResolveContactAsync(Contact newContact) { - newContact.Account = await GenericController.ResolveAccountById(newContact.AccountId); + // newContact.Account = await GenericController.ResolveAccountById(newContact.AccountId); - if (newContact.Account != null) { + if (newContact.Account is not null) { newContact.NoPhoneCalls = false; newContact.EmailBounced = false; newContact.NoHardcopyMailing = false; diff --git a/Gremlin_BlazorServer/Pages/CustomDescriptions.razor.cs b/Gremlin_BlazorServer/Pages/CustomDescriptions.razor.cs index f67162e..44fa9dc 100644 --- a/Gremlin_BlazorServer/Pages/CustomDescriptions.razor.cs +++ b/Gremlin_BlazorServer/Pages/CustomDescriptions.razor.cs @@ -2,6 +2,7 @@ using System.Security.Claims; using Blazorise; using Blazorise.DataGrid; using Gremlin_BlazorServer.Data.EntityClasses; +using Gremlin_BlazorServer.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; diff --git a/Gremlin_BlazorServer/Pages/LineItems.razor b/Gremlin_BlazorServer/Pages/LineItems.razor index d9bd75c..76cbc0a 100644 --- a/Gremlin_BlazorServer/Pages/LineItems.razor +++ b/Gremlin_BlazorServer/Pages/LineItems.razor @@ -3,6 +3,7 @@ @using Gremlin_BlazorServer.Data.EntityClasses @inject GenericController GenericController +@inject ILoadingIndicatorService ApplicationLoadingIndicatorService diff --git a/Gremlin_BlazorServer/Pages/LineItems.razor.cs b/Gremlin_BlazorServer/Pages/LineItems.razor.cs index 0d29257..270e79c 100644 --- a/Gremlin_BlazorServer/Pages/LineItems.razor.cs +++ b/Gremlin_BlazorServer/Pages/LineItems.razor.cs @@ -3,23 +3,33 @@ using System.Security.Claims; using Gremlin_BlazorServer.Data.EntityClasses; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; +using NuGet.Packaging; namespace Gremlin_BlazorServer.Pages; public partial class LineItems { private readonly CultureInfo cultureInfo = new("de-DE"); - private IList? lineItems; - private LineItem? selectedLineItem; + private readonly IList lineItems=new List(); + private LineItem selectedLineItem = new(); [CascadingParameter] private Task? AuthenticationStateTask { get; set; } - protected override async Task OnInitializedAsync() { if (AuthenticationStateTask != null) { ClaimsPrincipal user = (await AuthenticationStateTask).User; - if (user.Identity is { IsAuthenticated: true }) lineItems = GenericController.GetAll(); + if (user.Identity is{ IsAuthenticated: true }) { + await ApplicationLoadingIndicatorService.Show(); + lineItems.AddRange(await GenericController.GetAllAsync()); + selectedLineItem = lineItems.First(); + await OnSelectedLineItemChanged(selectedLineItem); + await ApplicationLoadingIndicatorService.Hide(); + } } } - private void OnSelectedLineItemChanged(LineItem newSelectedLineItem) => selectedLineItem = newSelectedLineItem; + private async Task OnSelectedLineItemChanged(LineItem newSelectedLineItem) { + await ApplicationLoadingIndicatorService.Show(); + selectedLineItem = newSelectedLineItem; + await ApplicationLoadingIndicatorService.Hide(); + } } \ No newline at end of file diff --git a/Gremlin_BlazorServer/Pages/ProductLines.razor.cs b/Gremlin_BlazorServer/Pages/ProductLines.razor.cs index 894b86d..f88c85a 100644 --- a/Gremlin_BlazorServer/Pages/ProductLines.razor.cs +++ b/Gremlin_BlazorServer/Pages/ProductLines.razor.cs @@ -1,49 +1,51 @@ using System.Security.Claims; using Blazorise.DataGrid; using Gremlin_BlazorServer.Data.EntityClasses; +using Gremlin_BlazorServer.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; +using NuGet.Packaging; namespace Gremlin_BlazorServer.Pages; public partial class ProductLines { - private IList? productLines; - private ProductLine? selectedProductLine; + private readonly IList productLines = new List(); + private ProductLine selectedProductLine = new(); [CascadingParameter] private Task? AuthenticationStateTask { get; set; } protected override async Task OnParametersSetAsync() { if (AuthenticationStateTask != null) { ClaimsPrincipal user = (await AuthenticationStateTask).User; - if (user.Identity is { IsAuthenticated: true }) productLines = await GenericController.GetAllAsync(); - + if (user.Identity is{ IsAuthenticated: true }) { + await ApplicationLoadingIndicatorService.Show(); + productLines.AddRange(await GenericController.GetAllAsync()); + selectedProductLine = productLines.First(); + await ApplicationLoadingIndicatorService.Hide(); + } await base.OnInitializedAsync(); } } private async Task OnSelectedProductLineChanged(ProductLine newSelectedProductLine) { await ApplicationLoadingIndicatorService.Show(); - selectedProductLine = await GenericController.GetAsync(pL => pL.ProductLineCode == newSelectedProductLine.ProductLineCode, "Products"); + selectedProductLine = await GenericController.GetAsync(pL => pL.ProductLineCode.Equals(newSelectedProductLine.ProductLineCode), "Products"); await ApplicationLoadingIndicatorService.Hide(); } private async Task OnRowInsertedAsync(SavedRowItem> productLine) { ProductLine newProductLine = await ResolveProductAsync(productLine.Item); - if (newProductLine.ProductLineCode == null) - return; int count = await GenericController.InsertAsync(newProductLine); Console.WriteLine($"Inserted {count} properties for new ProductLine {newProductLine.ProductLineCode}."); } private async Task OnRowUpdatedAsync(SavedRowItem> productLine) { ProductLine newProductLine = await ResolveProductAsync(productLine.Item); - if (newProductLine.ProductLineCode == null) - return; int count = await GenericController.UpdateAsync(productLine.Item); Console.WriteLine($"Updated {count} properties in ProductLine {newProductLine.ProductLineCode}."); } - private async Task OnRowRemovedAsync(ProductLine productLine) { + private static async Task OnRowRemovedAsync(ProductLine productLine) { int count = await GenericController.RemoveAsync(productLine); Console.WriteLine($"Removed {count} properties and ProductLine {productLine.ProductLineCode}."); } @@ -51,7 +53,7 @@ public partial class ProductLines { private async Task ResolveProductAsync(ProductLine newProductLine) { newProductLine.DataModificationByUser = "Gremlin Blazor Server GUI"; newProductLine.DataVersionNumber++; - newProductLine.Products = await GenericController.GetAllAsync(p => p.ProductLineCode == newProductLine.ProductLineCode); + newProductLine.Products = await GenericController.GetAllAsync(p => p.ProductLineCode.Equals(newProductLine.ProductLineCode)); return newProductLine; } } \ No newline at end of file diff --git a/Gremlin_BlazorServer/Pages/Products.razor.cs b/Gremlin_BlazorServer/Pages/Products.razor.cs index 3ebf0ab..08073a2 100644 --- a/Gremlin_BlazorServer/Pages/Products.razor.cs +++ b/Gremlin_BlazorServer/Pages/Products.razor.cs @@ -1,5 +1,6 @@ using System.Security.Claims; using Gremlin_BlazorServer.Data.EntityClasses; +using Gremlin_BlazorServer.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; diff --git a/Gremlin_BlazorServer/Pages/Quotes/QuoteAdd.razor.cs b/Gremlin_BlazorServer/Pages/Quotes/QuoteAdd.razor.cs index b3e56c8..37c715f 100644 --- a/Gremlin_BlazorServer/Pages/Quotes/QuoteAdd.razor.cs +++ b/Gremlin_BlazorServer/Pages/Quotes/QuoteAdd.razor.cs @@ -7,32 +7,33 @@ using Gremlin_BlazorServer.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.JSInterop; +using NuGet.Packaging; namespace Gremlin_BlazorServer.Pages.Quotes; public partial class QuoteAdd { - private static CustomDescription newCustomDescription; + private static readonly CustomDescription newCustomDescription = new(); private readonly CultureInfo cultureInfo = new("de-DE"); - private readonly bool debug; + private const bool debug = true; - private IList? contacts; + private IList contacts = new List(); private bool isCreatingPdf; private bool isCreatingTex; private bool lineItemsNotReady = true; private bool pdfNotReady = true; private Quote quote = new(); - private Contact? selectedContact; + private Contact selectedContact = new(); - private LineItem? selectedLineItem; + private LineItem selectedLineItem = new(); // private List? suggestedCustomDescriptions; private bool texNotReady = true; - private string? url; + private string url = string.Empty; [CascadingParameter] private Task? AuthenticationStateTask { get; set; } - [Inject] public static IModalService ModalService { get; set; } + [Inject] public static IModalService? ModalService { get; set; } - public static Task ShowCustomDescriptionModal(List suggestedCustomDescriptions) { - return ModalService.Show(builder => { + public static Task? ShowCustomDescriptionModal(List suggestedCustomDescriptions) { + return ModalService?.Show(builder => { builder.Add(parameter => parameter.CustomDescription, newCustomDescription); builder.Add(parameter => parameter.SuggestedCustomDescriptions, suggestedCustomDescriptions); }); @@ -42,20 +43,10 @@ public partial class QuoteAdd { if (AuthenticationStateTask is not null) { ClaimsPrincipal user = (await AuthenticationStateTask).User; if (user.Identity is { IsAuthenticated: true }) { - contacts = await GenericController.GetAllAsync(); - selectedContact = contacts?.FirstOrDefault(); - if (selectedContact is not null) - await SelectedContactChanged(selectedContact); - - SalesRep newSalesRep = new() { - LastName = "Woitschetzki", - FirstName = "Sascha", - TerritoryId = "83PE89", - PhoneNumber = "+49 176 22285334", - EMail = "sascha.woitschetzki@non.agilent.com", - AccountId = 2262, - Gender = 1 - }; + contacts.AddRange(await GenericController.GetAllAsync()); + selectedContact = contacts.First(); + + SalesRep newSalesRep = await GenericController.GetAsync(sr => sr.FirstName.Equals("Sascha")); quote = await GenerateNewQuote(quote, newSalesRep); } @@ -65,12 +56,12 @@ public partial class QuoteAdd { } private async Task GenerateNewQuote(Quote newQuote, SalesRep newSalesRep) { - newQuote.SalesRep = newSalesRep; //await genericController.GetAsync(sR => sR.LastName.Equals(newSalesRep.LastName)); - newQuote.SalesRep.Account = await GenericController.GetAsync(a => a.AccountId.Equals(newQuote.SalesRep.AccountId)); + newQuote.SalesRep = await GenericController.GetAsync(sR => sR.LastName.Equals(newSalesRep.LastName)); + // newQuote.SalesRep.Account = await GenericController.GetAsync(a => a.AccountId.Equals(newQuote.SalesRep.AccountId)); - Quote? lastQuote = GenericController.GetLast(); - newQuote.QuoteId = lastQuote is not null ? lastQuote.QuoteId + 1 : 1; - newQuote.QuotationNumber = $"DE-{newQuote.SalesRep?.TerritoryId}-{DateTime.Now:My}-{newQuote.QuoteId}"; + Quote lastQuote = await GenericController.GetLastAsync(); + newQuote.QuoteId = lastQuote.QuoteId + 1; + newQuote.QuotationNumber = $"DE-{newQuote.SalesRep.TerritoryId}-{DateTime.Now:My}-{newQuote.QuoteId}"; newQuote.Description = "Gerät"; return newQuote; @@ -92,7 +83,7 @@ public partial class QuoteAdd { Console.WriteLine(exc.Message); } finally { - if (quote.Recipient != null) lineItemsNotReady = false; + lineItemsNotReady = false; StateHasChanged(); } } @@ -105,16 +96,13 @@ public partial class QuoteAdd { Console.WriteLine($"File: {e.File.Name} Progress: {e.Percentage}"); } - private async Task SelectedContactChanged(Contact newSelectedContact) { - quote.Recipient = await GenericController.GetAsync(c => c.ContactId.Equals(newSelectedContact.ContactId)); - - if (quote.Recipient is not null) - //Read account seperatly to avoid new generation - quote.Recipient.Account = await GenericController.GetAsync(a => a.AccountId.Equals(quote.Recipient.AccountId)); - + private Task SelectedContactChanged(Contact newSelectedContact) { + quote.RecipientId = newSelectedContact.ContactId; + // quote.Recipient = await GenericController.GetAsync(c => c.ContactId.Equals(newSelectedContact.ContactId)); + // quote.Recipient.Account = await GenericController.GetAsync(a => a.AccountId.Equals(quote.Recipient.AccountId)); if (quote is { LineItems: not null, Recipient.Account: not null }) lineItemsNotReady = false; - selectedContact = newSelectedContact; + return Task.CompletedTask; } private async Task OnSave() { diff --git a/Gremlin_BlazorServer/Pages/Quotes/QuoteIndex.razor.cs b/Gremlin_BlazorServer/Pages/Quotes/QuoteIndex.razor.cs index dc6199d..ce15305 100644 --- a/Gremlin_BlazorServer/Pages/Quotes/QuoteIndex.razor.cs +++ b/Gremlin_BlazorServer/Pages/Quotes/QuoteIndex.razor.cs @@ -1,6 +1,7 @@ using System.Globalization; using System.Security.Claims; using Gremlin_BlazorServer.Data.EntityClasses; +using Gremlin_BlazorServer.Services; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; diff --git a/Gremlin_BlazorServer/Services/GenericController.cs b/Gremlin_BlazorServer/Services/GenericController.cs index bf9b2ac..fad8b18 100644 --- a/Gremlin_BlazorServer/Services/GenericController.cs +++ b/Gremlin_BlazorServer/Services/GenericController.cs @@ -6,338 +6,326 @@ using Microsoft.EntityFrameworkCore.ChangeTracking; namespace Gremlin_BlazorServer.Services; public class GenericController { - public IList? GetAll() where TResult : class, IMetadata { - try { - using (GremlinDb gremlinDb = new()) return gremlinDb.Set().ToList(); - } - catch (Exception exception) { - Console.WriteLine(exception.InnerException); - return null; - } - } + private static readonly GremlinDb gremlinDb = new(); - public IList? GetAll(string include) where TResult : class, IMetadata { - try { - using (GremlinDb gremlinDb = new()) return gremlinDb.Set().Include(include).ToList(); - } - catch (Exception exception) { - Console.WriteLine(exception.InnerException); - return null; - } - } + public IList? GetAll() where TResult : class, IMetadata { + try { + return gremlinDb.Set().ToList(); + } + catch (Exception exception) { + Console.WriteLine(exception.InnerException); + return null; + } + } - public IList? GetAll(Predicate search) where TResult : class, IMetadata { - ArgumentNullException.ThrowIfNull(search); - try { - using (GremlinDb gremlinDb = new()) return gremlinDb.Set().AsEnumerable().Where(t => search(t)).ToList(); - } - catch (Exception exception) { - Console.WriteLine(exception.InnerException); - return null; - } - } + public IList? GetAll(string include) where TResult : class, IMetadata { + try { + return gremlinDb.Set().Include(include).ToList(); + } + catch (Exception exception) { + Console.WriteLine(exception.InnerException); + return null; + } + } - public async Task?> GetAllAsync() where TResult : class, IMetadata { - try { - await using (GremlinDb gremlinDb = new()) return await gremlinDb.Set().ToListAsync(); - } - catch (DbUpdateConcurrencyException exception) { - await HandleDbUpdateConcurrencyException(exception); - Console.WriteLine(exception.InnerException); - return null; - } - } + public IList? GetAll(Predicate search) where TResult : class, IMetadata { + ArgumentNullException.ThrowIfNull(search); + try { + return gremlinDb.Set().AsEnumerable().Where(t => search(t)).ToList(); + } + catch (Exception exception) { + Console.WriteLine(exception.InnerException); + return null; + } + } - public async Task?> GetAllAsync(string include) where TResult : class, IMetadata { - try { - await using (GremlinDb gremlinDb = new()) return await gremlinDb.Set().Include(include).ToListAsync(); - } - catch (DbUpdateConcurrencyException exception) { - await HandleDbUpdateConcurrencyException(exception); - Console.WriteLine(exception.InnerException); - return null; - } - } + public async Task?> GetAllAsync() where TResult : class, IMetadata { + try { + return await gremlinDb.Set().ToListAsync(); + } + catch (DbUpdateConcurrencyException exception) { + await HandleDbUpdateConcurrencyException(exception); + Console.WriteLine(exception.InnerException); + return null; + } + } - public async Task?> GetAllAsync(string include1, string include2) where TResult : class, IMetadata { - try { - await using (GremlinDb gremlinDb = new()) return await gremlinDb.Set().Include(include1).Include(include2).ToListAsync(); - } - catch (DbUpdateConcurrencyException exception) { - await HandleDbUpdateConcurrencyException(exception); - Console.WriteLine(exception.InnerException); - return null; - } - } + public async Task?> GetAllAsync(string include) where TResult : class, IMetadata { + try { + return await gremlinDb.Set().Include(include).ToListAsync(); + } + catch (DbUpdateConcurrencyException exception) { + await HandleDbUpdateConcurrencyException(exception); + Console.WriteLine(exception.InnerException); + return null; + } + } - public async Task?> GetAllAsync(Predicate search) where TResult : class, IMetadata { - ArgumentNullException.ThrowIfNull(search); - try { - await using (GremlinDb gremlinDb = new()) return await Task.Run(() => gremlinDb.Set().AsEnumerable().Where(t => search(t)).ToList()); - } - catch (DbUpdateConcurrencyException exception) { - await HandleDbUpdateConcurrencyException(exception); - Console.WriteLine(exception.InnerException); - return null; - } - } + public async Task?> GetAllAsync(string include1, string include2) where TResult : class, IMetadata { + try { + return await gremlinDb.Set().Include(include1).Include(include2).ToListAsync(); + } + catch (DbUpdateConcurrencyException exception) { + await HandleDbUpdateConcurrencyException(exception); + Console.WriteLine(exception.InnerException); + return null; + } + } - public async Task?> GetAllAsync(Predicate search, string include) where TResult : class, IMetadata { - ArgumentNullException.ThrowIfNull(search); - try { - await using (GremlinDb gremlinDb = new()) return await Task.Run(() => gremlinDb.Set().Include(include).AsEnumerable().Where(t => search(t)).ToList()); - } - catch (DbUpdateConcurrencyException exception) { - await HandleDbUpdateConcurrencyException(exception); - Console.WriteLine(exception.InnerException); - return null; - } - } + public async Task?> GetAllAsync(Predicate search) where TResult : class, IMetadata { + ArgumentNullException.ThrowIfNull(search); + try { + return await Task.Run(() => gremlinDb.Set().AsEnumerable().Where(t => search(t)).ToList()); + } + catch (DbUpdateConcurrencyException exception) { + await HandleDbUpdateConcurrencyException(exception); + Console.WriteLine(exception.InnerException); + return null; + } + } - public IList? GetAll(Predicate search, string include) where TResult : class, IMetadata { - ArgumentNullException.ThrowIfNull(search); - try { - using (GremlinDb gremlinDb = new()) return gremlinDb.Set().Include(include).AsEnumerable().Where(t => search(t)).ToList(); - } - catch (Exception exception) { - Console.WriteLine(exception.InnerException); - return null; - } - } + public async Task?> GetAllAsync(Predicate search, string include) where TResult : class, IMetadata { + ArgumentNullException.ThrowIfNull(search); + try { + return await Task.Run(() => gremlinDb.Set().Include(include).AsEnumerable().Where(t => search(t)).ToList()); + } + catch (DbUpdateConcurrencyException exception) { + await HandleDbUpdateConcurrencyException(exception); + Console.WriteLine(exception.InnerException); + return null; + } + } - public TResult? Get(Predicate search) where TResult : class, IMetadata { - ArgumentNullException.ThrowIfNull(search); - try { - using (GremlinDb gremlinDb = new()) return gremlinDb.Set().AsEnumerable().FirstOrDefault(t => search(t)); - } - catch (Exception exception) { - Console.WriteLine(exception.InnerException); - return null; - } - } + // public IList? GetAll(Predicate search, string include) where TResult : class, IMetadata { + // ArgumentNullException.ThrowIfNull(search); + // try { + // return gremlinDb.Set().Include(include).AsEnumerable().Where(t => search(t)).ToList(); + // } + // catch (Exception exception) { + // Console.WriteLine(exception.InnerException); + // return null; + // } + // } - public async Task GetAsync(Predicate search) where TResult : class, IMetadata { - ArgumentNullException.ThrowIfNull(search); - try { - await using (GremlinDb gremlinDb = new()) return await Task.Run(() => gremlinDb.Set().AsEnumerable().FirstOrDefault(t => search(t))); - } - catch (DbUpdateConcurrencyException exception) { - await HandleDbUpdateConcurrencyException(exception); - Console.WriteLine(exception.InnerException); - return null; - } - } + // public TResult? Get(Predicate search) where TResult : class, IMetadata { + // ArgumentNullException.ThrowIfNull(search); + // try { + // using GremlinDb gremlinDb = new(); + // return gremlinDb.Set().AsEnumerable().FirstOrDefault(t => search(t)); + // } + // catch (Exception exception) { + // Console.WriteLine(exception.InnerException); + // return null; + // } + // } - public TResult? Get(Predicate search, string include) where TResult : class, IMetadata { - ArgumentNullException.ThrowIfNull(search); - try { - using (GremlinDb gremlinDb = new()) return gremlinDb.Set().AsNoTracking().Include(include).AsEnumerable().FirstOrDefault(t => search(t)); - } - catch (Exception exception) { - Console.WriteLine(exception.InnerException); - return null; - } - } + public static async Task GetAsync(Predicate search) where TResult : class, IMetadata, new() { + try { + return await Task.Run(() => gremlinDb.Set().AsEnumerable().First(t => search(t))); + } + catch (Exception e) { + Console.WriteLine(e.InnerException); + return new(); + } + } - public async Task GetAsync(Predicate search, string include1) where TResult : class, IMetadata { - ArgumentNullException.ThrowIfNull(search); - try { - await using (GremlinDb gremlinDb = new()) return await Task.Run(() => gremlinDb.Set().Include(include1).AsEnumerable().FirstOrDefault(t => search(t))); - } - catch (DbUpdateConcurrencyException exception) { - await HandleDbUpdateConcurrencyException(exception); - Console.WriteLine(exception.InnerException); - return null; - } - } + // public TResult? Get(Predicate search, string include) where TResult : class, IMetadata { + // ArgumentNullException.ThrowIfNull(search); + // try { + // using GremlinDb gremlinDb = new(); + // return gremlinDb.Set().AsNoTracking().Include(include).AsEnumerable().FirstOrDefault(t => search(t)); + // } + // catch (Exception exception) { + // Console.WriteLine(exception.InnerException); + // return null; + // } + // } - public async Task GetAsync(Predicate search, string include1, string include2) where TResult : class, IMetadata { - ArgumentNullException.ThrowIfNull(search); - try { - await using (GremlinDb gremlinDb = new()) return await Task.Run(() => gremlinDb.Set().Include(include1).Include(include2).AsEnumerable().FirstOrDefault(t => search(t))); - } - catch (DbUpdateConcurrencyException exception) { - await HandleDbUpdateConcurrencyException(exception); - Console.WriteLine(exception.InnerException); - return null; - } - } + public async Task GetAsync(Predicate search, string include1) where TResult : class, IMetadata, new() { + ArgumentNullException.ThrowIfNull(search); + try { + return await Task.Run(() => gremlinDb.Set().Include(include1).AsEnumerable().First(t => search(t))); + } + catch (DbUpdateConcurrencyException exception) { + await HandleDbUpdateConcurrencyException(exception); + Console.WriteLine(exception.InnerException); + return new(); + } + } - public TResult? GetLast() where TResult : class, IMetadata { - try { - using (GremlinDb gremlinDb = new()) return gremlinDb.Set().AsEnumerable().Last(); - } - catch (Exception exception) { - Console.WriteLine(exception.InnerException); - return null; - } - } + public async Task GetAsync(Predicate search, string include1, string include2) where TResult : class, IMetadata, new() { + ArgumentNullException.ThrowIfNull(search); + try { + return await Task.Run(() => gremlinDb.Set().Include(include1).Include(include2).AsEnumerable().First(t => search(t))); + } + catch (DbUpdateConcurrencyException exception) { + await HandleDbUpdateConcurrencyException(exception); + Console.WriteLine(exception.InnerException); + return new(); + } + } - public int Insert(T entity) where T : class, IMetadata { - try { - using (GremlinDb gremlinDb = new()) { - gremlinDb.Set().Add(entity); - return gremlinDb.SaveChanges(); - } - } - catch (Exception exception) { - Console.WriteLine(exception.InnerException); - return 0; - } - } + public static async Task GetLastAsync() where TResult : class, IMetadata, new() { + try { + return await Task.Run(() => gremlinDb.Set().AsEnumerable().Last()); + } + catch (Exception exception) { + Console.WriteLine(exception.InnerException); + return new(); + } + } - public int Insert(IEnumerable entities) where T : class, IMetadata { - try { - using (GremlinDb gremlinDb = new()) { - gremlinDb.Set().AddRange(entities); - return gremlinDb.SaveChanges(); - } - } - catch (Exception exception) { - Console.WriteLine(exception.InnerException); - return 0; - } - } + // public int Insert(T entity) where T : class, IMetadata { + // try { + // using GremlinDb gremlinDb = new(); + // gremlinDb.Set().Add(entity); + // return gremlinDb.SaveChanges(); + // } + // catch (Exception exception) { + // Console.WriteLine(exception.InnerException); + // return 0; + // } + // } - public async Task InsertAsync(T entity) where T : class, IMetadata { - try { - await using (GremlinDb gremlinDb = new()) { - gremlinDb.Set().Add(entity); - return await gremlinDb.SaveChangesAsync(); - } - } - catch (DbUpdateConcurrencyException exception) { - await HandleDbUpdateConcurrencyException(exception); - Console.WriteLine(exception.InnerException); - return 0; - } - } + // public int Insert(IEnumerable entities) where T : class, IMetadata { + // try { + // using GremlinDb gremlinDb = new(); + // gremlinDb.Set().AddRange(entities); + // return gremlinDb.SaveChanges(); + // } + // catch (Exception exception) { + // Console.WriteLine(exception.InnerException); + // return 0; + // } + // } - public async Task InsertAsync(IEnumerable entities) where T : class, IMetadata { - try { - await using (GremlinDb gremlinDb = new()) { - gremlinDb.Set().AddRange(entities); - return await gremlinDb.SaveChangesAsync(); - } - } - catch (DbUpdateConcurrencyException exception) { - await HandleDbUpdateConcurrencyException(exception); - Console.WriteLine(exception.InnerException); - return 0; - } - } + public async Task InsertAsync(T entity) where T : class, IMetadata { + try { + gremlinDb.Set().Add(entity); + return await gremlinDb.SaveChangesAsync(); + } + catch (DbUpdateConcurrencyException exception) { + await HandleDbUpdateConcurrencyException(exception); + Console.WriteLine(exception.InnerException); + return 0; + } + } - public static bool IsExisting(Predicate search) where T : class, IMetadata { - ArgumentNullException.ThrowIfNull(search); - try { - using (GremlinDb gremlinDb = new()) return gremlinDb.Set().AsEnumerable().Any(t => search(t)); - } - catch (Exception exception) { - Console.WriteLine(exception.InnerException); - return false; - } - } + public async Task InsertAsync(IEnumerable entities) where T : class, IMetadata { + try { + gremlinDb.Set().AddRange(entities); + return await gremlinDb.SaveChangesAsync(); + } + catch (DbUpdateConcurrencyException exception) { + await HandleDbUpdateConcurrencyException(exception); + Console.WriteLine(exception.InnerException); + return 0; + } + } - public int Update(T entity) where T : class, IMetadata { - try { - using (GremlinDb gremlinDb = new()) { - gremlinDb.Set().Update(entity); - return gremlinDb.SaveChanges(); - } - } - catch (Exception exception) { - Console.WriteLine(exception.InnerException); - return 0; - } - } + public static async Task IsExistingAsync(Predicate search) where T : class, IMetadata { + try { + return await Task.Run(() => gremlinDb.Set().AsEnumerable().Any(t => search(t))); + } + catch (Exception exception) { + Console.WriteLine(exception.InnerException); + return false; + } + } - public async Task UpdateAsync(T entity) where T : class, IMetadata { - await using (GremlinDb gremlinDb = new()) { - try { - gremlinDb.Set().Update(entity); - return await gremlinDb.SaveChangesAsync(false); - } - catch (DbUpdateConcurrencyException exception) { - await HandleDbUpdateConcurrencyException(exception); - Console.WriteLine(exception.InnerException); - return 0; - } - } - } + // public int Update(T entity) where T : class, IMetadata { + // try { + // using GremlinDb gremlinDb = new(); + // gremlinDb.Set().Update(entity); + // return gremlinDb.SaveChanges(); + // } + // catch (Exception exception) { + // Console.WriteLine(exception.InnerException); + // return 0; + // } + // } - private async Task HandleDbUpdateConcurrencyException(DbUpdateConcurrencyException exception) where T : class, IMetadata { - // Loop through the entities that caused the concurrency conflict - foreach (EntityEntry? entry in exception.Entries) { - if (entry.Entity is T) { - T? clientValues = (T)entry.Entity; - PropertyValues? databaseEntry = await entry.GetDatabaseValuesAsync(); - if (databaseEntry is null) - // The record has been deleted from the database - // Notify the user or handle the error in some other way - throw new("The record has been deleted from the database."); - T? databaseValues = (T)databaseEntry.ToObject(); - // Compare the database values with the client values - if (databaseValues.DataVersionNumber != clientValues.DataVersionNumber) - // The name has been changed by another user - // Notify the user or handle the error in some other way - throw new("The record has been modified by another user."); - // The conflict is caused by a property other than the name - // Notify the user or handle the error in some other way - throw new("A concurrency conflict occurred."); - } + public async Task UpdateAsync(T entity) where T : class, IMetadata { + try { + gremlinDb.Set().Update(entity); + return await gremlinDb.SaveChangesAsync(false); + } + catch (DbUpdateConcurrencyException exception) { + await HandleDbUpdateConcurrencyException(exception); + Console.WriteLine(exception.InnerException); + return 0; + } + } - // Handle concurrency conflicts for other entity types, if necessary - throw new NotSupportedException($"Concurrency conflicts for entities of type {entry.Entity.GetType().Name} are not supported."); - } - } + private static async Task HandleDbUpdateConcurrencyException(DbUpdateConcurrencyException exception) where T : class, IMetadata { + // Loop through the entities that caused the concurrency conflict + foreach (EntityEntry? entry in exception.Entries) { + if (entry.Entity is not T clientValues) throw new NotSupportedException($"Concurrency conflicts for entities of type {entry.Entity.GetType().Name} are not supported."); + PropertyValues? databaseEntry = await entry.GetDatabaseValuesAsync(); + if (databaseEntry is null) + // The record has been deleted from the database + // Notify the user or handle the error in some other way + throw new("The record has been deleted from the database."); + T databaseValues = (T)databaseEntry.ToObject(); + // Compare the database values with the client values + if (databaseValues.DataVersionNumber != clientValues.DataVersionNumber) + // The name has been changed by another user + // Notify the user or handle the error in some other way + throw new("The record has been modified by another user."); + // The conflict is caused by a property other than the name + // Notify the user or handle the error in some other way + throw new("A concurrency conflict occurred."); - public int Update(IEnumerable entities) where T : class, IMetadata { - try { - using (GremlinDb gremlinDb = new()) { - gremlinDb.Set().UpdateRange(entities); - return gremlinDb.SaveChanges(); - } - } - catch (Exception exception) { - Console.WriteLine(exception.InnerException); - return 0; - } - } + // Handle concurrency conflicts for other entity types, if necessary + } + } - public async Task UpdateAsync(IEnumerable entities) where T : class, IMetadata { - try { - await using (GremlinDb gremlinDb = new()) { - gremlinDb.Set().UpdateRange(entities); - return await gremlinDb.SaveChangesAsync(); - } - } - catch (Exception exception) { - Console.WriteLine(exception.InnerException); - return 0; - } - } + // public int Update(IEnumerable entities) where T : class, IMetadata { + // try { + // using GremlinDb gremlinDb = new(); + // gremlinDb.Set().UpdateRange(entities); + // return gremlinDb.SaveChanges(); + // } + // catch (Exception exception) { + // Console.WriteLine(exception.InnerException); + // return 0; + // } + // } - public async Task RemoveAsync(T entity) where T : class, IMetadata { - try { - await using (GremlinDb gremlinDb = new()) { - gremlinDb.Set().Remove(entity); - return await gremlinDb.SaveChangesAsync(); - } - } - catch (Exception exception) { - Console.WriteLine(exception.InnerException); - return 0; - } - } - - public async Task ResolveAccountById(uint accountId){ - try { - await using (GremlinDb gremlinDb = new()) { - return gremlinDb.Accounts.First(a => a.AccountId.Equals(accountId)); - } - } - catch { - return new Account(); - } - } + public static async Task UpdateAsync(IEnumerable entities) where T : class, IMetadata { + try { + await Task.Run(() => gremlinDb.Set().UpdateRange(entities)); + return await gremlinDb.SaveChangesAsync(); + } + catch (Exception exception) { + Console.WriteLine(exception.InnerException); + return 0; + } + } + + public static async Task RemoveAsync(T entity) where T : class, IMetadata { + try { + await Task.Run(() => gremlinDb.Set().Remove(entity)); + return await gremlinDb.SaveChangesAsync(); + } + catch (Exception exception) { + Console.WriteLine(exception.InnerException); + return 0; + } + } + + // public async Task ResolveAccountById(uint accountId){ + // try { + // + // if (gremlinDb.Accounts != null) + // return gremlinDb.Accounts.First(a => a.AccountId.Equals(accountId)); + // else { + // return null; + // } + // } + // catch (Exception exception){ + // Console.WriteLine(exception.InnerException); + // return null; + // } + // } } \ No newline at end of file diff --git a/Gremlin_BlazorServer/Services/GenericImporter.cs b/Gremlin_BlazorServer/Services/GenericImporter.cs index 24bbb79..fb65a3e 100644 --- a/Gremlin_BlazorServer/Services/GenericImporter.cs +++ b/Gremlin_BlazorServer/Services/GenericImporter.cs @@ -11,15 +11,13 @@ public class GenericImporter { Console.WriteLine("Importing accounts from csv..."); IList splitLines = await Task.Run(() => SplitLines(fileContent)); - if (typeof(T) == typeof(Account)) { - ParseListToAccounts(splitLines); - int countNewAccounts = genericController.Insert(newAccounts); - int countUpdatedAccounts = genericController.Update(updatedAccounts); - Console.WriteLine($"Added {countNewAccounts} new Accounts to database and updated {updatedAccounts.Count} Accounts."); - return countNewAccounts > 0 || updatedAccounts.Count > 0; - } + if (typeof(T) != typeof(Account)) return false; + await ParseListToAccounts(splitLines); + int countNewAccounts = await genericController.InsertAsync(newAccounts); + int countUpdatedAccounts = await GenericController.UpdateAsync(updatedAccounts); + Console.WriteLine($"Added {countNewAccounts} new Accounts to database and updated {countUpdatedAccounts} Accounts."); + return countNewAccounts > 0 || updatedAccounts.Count > 0; - return false; } private static IList SplitLines(string fileContent) { @@ -30,69 +28,58 @@ public class GenericImporter { return fileList; } - private void ParseListToAccounts(IList lineList) // ID;Acct Name 1 and 2;Street;City;BP Role;Postal Code;Customer Type;Market Indicator; + private async Task ParseListToAccounts(IList lineList) // ID;Acct Name 1 and 2;Street;City;BP Role;Postal Code;Customer Type;Market Indicator; { - List existingAccounts = new(); - foreach (string[] line in lineList) { - if (!line[0].Contains("ID")) //HACK: skip first row if header - if (uint.TryParse(line[0], out uint sapAccountNumber) && uint.TryParse(line[5], out uint zip)) //HACK: skip lines with wrong uint - { - Account readAccount = new() { - SapAccountNumber = sapAccountNumber, - AccountName = line[1], - Street = line[2], - City = line[3].ToUpper().First() + line[3].Substring(1).ToLower(), - Zip = zip, - AccountType = new(), - SubMarket = new(), - PhoneNumber = line[8], - EMail = line[9], - // ParentAccountId = 0, - DataModificationByUser = "Gremlin Generic Importer" - }; - - AccountType? accountType = genericController.Get(aT => aT.AccountTypeCode == line[6]); - if (accountType == null) continue; - - readAccount.AccountType.AccountTypeCode = accountType.AccountTypeCode; - readAccount.AccountType = genericController.Get(aT => aT.AccountTypeCode == readAccount.AccountType.AccountTypeCode); - - SubMarket? subMarket = genericController.Get(sM => sM.SubMarketCode == line[7]); - if (subMarket == null) continue; - readAccount.SubMarket.SubMarketCode = subMarket.SubMarketCode; - readAccount.SubMarket = genericController.Get(sM => sM.SubMarketCode == readAccount.SubMarket.SubMarketCode); - - if (readAccount?.SubMarket?.SubMarketCode == null || readAccount?.AccountType?.AccountTypeCode == null) continue; //HACK: skip Accounts with no SubMarket or AccountType - - if (AccountExists(sapAccountNumber)) { - Account? existingAccount = genericController.Get(a => a.SapAccountNumber == readAccount.SapAccountNumber); - if (existingAccount == null) continue; //Account does not exist - if (!IsEqualAccount(readAccount, existingAccount)) { - existingAccount.DataModificationDate = DateTime.Now; - existingAccount.DataVersionNumber++; - existingAccount.DataModificationByUser = "Updated by Gremlin Generic Importer"; - existingAccount.AccountName = readAccount.AccountName; - existingAccount.City = readAccount.City; - existingAccount.EMail = readAccount.EMail; - existingAccount.PhoneNumber = readAccount.PhoneNumber; - existingAccount.Street = readAccount.Street; - existingAccount.Zip = readAccount.Zip; - //genericController.Update(existingAccount); - Console.WriteLine($"Update in Account {existingAccount.SapAccountNumber}:{existingAccount.AccountName}"); - updatedAccounts.Add(existingAccount); - } - } - else { - newAccounts.Add(readAccount); - } - } + if (line[0].Contains("ID")) continue; //HACK: skip first row if header + if (!uint.TryParse(line[0], out uint sapAccountNumber) || !uint.TryParse(line[5], out uint zip)) continue; //HACK: skip lines with wrong uint + Account readAccount = new() { + SapAccountNumber = sapAccountNumber, + AccountName = line[1], + Street = line[2], + City = line[3].ToUpper().First() + line[3].Substring(1).ToLower(), + Zip = zip, + AccountType = new(), + SubMarket = new(), + PhoneNumber = line[8], + EMail = line[9], + // ParentAccountId = 0, + DataModificationByUser = "Gremlin Generic Importer" + }; + + AccountType accountType = await GenericController.GetAsync(aT => aT.AccountTypeCode == line[6]); + readAccount.AccountType.AccountTypeCode = accountType.AccountTypeCode; + readAccount.AccountType = await GenericController.GetAsync(aT => aT.AccountTypeCode == readAccount.AccountType.AccountTypeCode); + + SubMarket subMarket = await GenericController.GetAsync(sM => sM.SubMarketCode == line[7]); + readAccount.SubMarket.SubMarketCode = subMarket.SubMarketCode; + readAccount.SubMarket = await GenericController.GetAsync(sM => sM.SubMarketCode == readAccount.SubMarket.SubMarketCode); + + if (readAccount.SubMarket?.SubMarketCode is null || readAccount.AccountType?.AccountTypeCode is null) continue; //HACK: skip Accounts with no SubMarket or AccountType + + if (await AccountExists(sapAccountNumber)) { + Account existingAccount = await GenericController.GetAsync(a => a.SapAccountNumber.Equals(readAccount.SapAccountNumber)); + if (IsEqualAccount(readAccount, existingAccount)) continue; + existingAccount.DataModificationDate = DateTime.Now; + existingAccount.DataVersionNumber++; + existingAccount.DataModificationByUser = "Updated by Gremlin Generic Importer"; + existingAccount.AccountName = readAccount.AccountName; + existingAccount.City = readAccount.City; + existingAccount.EMail = readAccount.EMail; + existingAccount.PhoneNumber = readAccount.PhoneNumber; + existingAccount.Street = readAccount.Street; + existingAccount.Zip = readAccount.Zip; + //genericController.Update(existingAccount); + Console.WriteLine($"Update in Account {existingAccount.SapAccountNumber}:{existingAccount.AccountName}"); + updatedAccounts.Add(existingAccount); + } + else { + newAccounts.Add(readAccount); + } } } private static bool IsEqualAccount(Account account, Account existingAccount) => account.AccountName == existingAccount.AccountName && account.City == existingAccount.City && account.EMail == existingAccount.EMail && account.PhoneNumber == existingAccount.PhoneNumber && account.Street == existingAccount.Street && account.Zip == existingAccount.Zip; - private bool AccountExists(uint sapAccountNumber) { - return GenericController.IsExisting(a => a.SapAccountNumber == sapAccountNumber); - } + private async Task AccountExists(uint sapAccountNumber) => await GenericController.IsExistingAsync(a => a.SapAccountNumber.Equals(sapAccountNumber)); } \ No newline at end of file diff --git a/Gremlin_BlazorServer/Services/QuoteHandling.cs b/Gremlin_BlazorServer/Services/QuoteHandling.cs index 5299ad8..34dee1e 100644 --- a/Gremlin_BlazorServer/Services/QuoteHandling.cs +++ b/Gremlin_BlazorServer/Services/QuoteHandling.cs @@ -185,7 +185,7 @@ public class QuoteHandling { //TODO Load all relevant CustomDescriptions upfront foreach (LineItem lineItem in quote.LineItems) { - CustomDescription newCustomDescription = await genericController.GetAsync(newCustomDescription => newCustomDescription.ProductNumber.Equals(lineItem.ProductNumber, StringComparison.Ordinal) && newCustomDescription.OptionNumber.Equals(lineItem.OptionNumber, StringComparison.Ordinal)); + CustomDescription newCustomDescription = await GenericController.GetAsync(newCustomDescription => newCustomDescription.ProductNumber.Equals(lineItem.ProductNumber, StringComparison.Ordinal) && newCustomDescription.OptionNumber.Equals(lineItem.OptionNumber, StringComparison.Ordinal)); if (newCustomDescription is null) { Console.WriteLine($"Keine CustomDescription für {lineItem.ProductNumber}#{lineItem.OptionNumber} verfügbar!"); @@ -201,10 +201,10 @@ public class QuoteHandling { _ = await genericController.InsertAsync(newCustomDescription); } - lineItem.Product = await genericController.GetAsync(p => p.ProductNumber.Equals(lineItem.ProductNumber) && p.OptionNumber.Equals(lineItem.OptionNumber)); + lineItem.Product = await GenericController.GetAsync(p => p.ProductNumber.Equals(lineItem.ProductNumber) && p.OptionNumber.Equals(lineItem.OptionNumber)); if (lineItem.Product is null) return quote; lineItem.ProductId = lineItem.Product.ProductId; - lineItem.Product.CustomDescription = await genericController.GetAsync(cD => cD.ProductNumber.Equals(lineItem.ProductNumber) && cD.OptionNumber.Equals(lineItem.OptionNumber)); + lineItem.Product.CustomDescription = await GenericController.GetAsync(cD => cD.ProductNumber.Equals(lineItem.ProductNumber) && cD.OptionNumber.Equals(lineItem.OptionNumber)); lineItem.Quote = quote; lineItem.QuoteId = lineItem.Quote.QuoteId; }