From d70383bbc1f44b2d8518e1c222d6495c277a9e11 Mon Sep 17 00:00:00 2001 From: Sascha Woitschetzki Date: Mon, 14 Aug 2023 13:28:15 +0200 Subject: [PATCH] Import of Contacts --- Gremlin_BlazorServer/Pages/Accounts.razor.cs | 14 ++-- Gremlin_BlazorServer/Pages/Contacts.razor.cs | 6 +- .../Pages/CustomDescriptions.razor.cs | 12 +-- .../Pages/ProductLines.razor.cs | 4 +- .../Pages/Quotes/QuoteAdd.razor.cs | 27 +++--- .../Services/GenericController.cs | 21 ++--- .../Services/GenericImporter.cs | 84 ++++++++++++------- .../Services/QuoteHandling.cs | 2 +- 8 files changed, 98 insertions(+), 72 deletions(-) diff --git a/Gremlin_BlazorServer/Pages/Accounts.razor.cs b/Gremlin_BlazorServer/Pages/Accounts.razor.cs index 4de9c74..fad242c 100644 --- a/Gremlin_BlazorServer/Pages/Accounts.razor.cs +++ b/Gremlin_BlazorServer/Pages/Accounts.razor.cs @@ -45,7 +45,7 @@ public partial class Accounts { _ = stream.Seek(0, SeekOrigin.Begin); using StreamReader reader = new(stream); string fileContent = await reader.ReadToEndAsync(); - await GenericImporter.ImportCsvAsync(fileContent); + GenericImporter.ImportCsv(fileContent); } } catch (Exception exception) { @@ -55,19 +55,21 @@ public partial class Accounts { StateHasChanged(); } - private static async Task OnRowInsertedAsync(SavedRowItem> account) { + private static Task OnRowInsertedAsync(SavedRowItem> account) { Account newAccount = ResolveAccountAsync(account.Item); // if (newAccount.AccountType is null || newAccount.SubMarket is null) return; - int count = await GenericController.InsertAsync(newAccount); + int count = GenericController.Insert(newAccount); Console.WriteLine($"Inserted {count} properties for new account {newAccount.AccountId}: {newAccount.AccountName}."); + return Task.CompletedTask; } - private static async Task OnRowUpdatedAsync(SavedRowItem> account) { + private static Task OnRowUpdatedAsync(SavedRowItem> account) { Account newAccount = ResolveAccountAsync(account.Item); if (newAccount.AccountType == null || newAccount.SubMarket == null) - return; - int count = await GenericController.UpdateAsync(account.Item); + return Task.CompletedTask; + int count = GenericController.Update(account.Item); Console.WriteLine($"Updated {count} properties in account {newAccount.AccountId}: {newAccount.AccountName}."); + return Task.CompletedTask; } private static async Task OnRowRemovedAsync(Account account) { diff --git a/Gremlin_BlazorServer/Pages/Contacts.razor.cs b/Gremlin_BlazorServer/Pages/Contacts.razor.cs index 1779156..da4ef9d 100644 --- a/Gremlin_BlazorServer/Pages/Contacts.razor.cs +++ b/Gremlin_BlazorServer/Pages/Contacts.razor.cs @@ -38,14 +38,14 @@ public partial class Contacts { private async Task OnRowInsertedAsync(SavedRowItem> contact) { Contact newContact = await ResolveContactAsync(contact.Item); // if (newContact.Account is null) return; - int count = await GenericController.InsertAsync(newContact); + int count = GenericController.Insert(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 is null) return; - int count = await GenericController.UpdateAsync(contact.Item); + int count = GenericController.Update(contact.Item); Console.WriteLine($"Updated {count} properties in contact {newContact.ContactId}: {newContact.FirstName} {newContact.LastName}."); } @@ -80,7 +80,7 @@ public partial class Contacts { _ = stream.Seek(0, SeekOrigin.Begin); using StreamReader reader = new(stream); string fileContent = await reader.ReadToEndAsync(); - await GenericImporter.ImportCsvAsync(fileContent); + GenericImporter.ImportCsv(fileContent); } } catch (Exception exception) { diff --git a/Gremlin_BlazorServer/Pages/CustomDescriptions.razor.cs b/Gremlin_BlazorServer/Pages/CustomDescriptions.razor.cs index 94714d4..a8f269b 100644 --- a/Gremlin_BlazorServer/Pages/CustomDescriptions.razor.cs +++ b/Gremlin_BlazorServer/Pages/CustomDescriptions.razor.cs @@ -34,7 +34,7 @@ public partial class CustomDescriptions { _ = stream.Seek(0, SeekOrigin.Begin); using StreamReader reader = new(stream); string fileContent = await reader.ReadToEndAsync(); - await GenericImporter.ImportCsvAsync(fileContent); + GenericImporter.ImportCsv(fileContent); } } catch (Exception exception) { @@ -43,16 +43,18 @@ public partial class CustomDescriptions { StateHasChanged(); } - private static async Task OnRowInsertedAsync(SavedRowItem> customDescription) { + private static Task OnRowInsertedAsync(SavedRowItem> customDescription) { CustomDescription newCustomDescription = ResolveCustomDescriptionAsync(customDescription.Item); - int count = await GenericController.InsertAsync(newCustomDescription); + int count = GenericController.Insert(newCustomDescription); Console.WriteLine($"Inserted {count} properties for new custom description {newCustomDescription.ProductNumber}#{newCustomDescription.OptionNumber}: {newCustomDescription.Heading}"); + return Task.CompletedTask; } - private static async Task OnRowUpdatedAsync(SavedRowItem> customDescription) { + private static Task OnRowUpdatedAsync(SavedRowItem> customDescription) { CustomDescription newCustomDescription = ResolveCustomDescriptionAsync(customDescription.Item); - int count = await GenericController.UpdateAsync(customDescription.Item); + int count = GenericController.Update(customDescription.Item); Console.WriteLine($"Updated {count} properties for custom description {newCustomDescription.ProductNumber}#{newCustomDescription.OptionNumber}: {newCustomDescription.Heading}"); + return Task.CompletedTask; } private static async Task OnRowRemovedAsync(CustomDescription customDescription) { diff --git a/Gremlin_BlazorServer/Pages/ProductLines.razor.cs b/Gremlin_BlazorServer/Pages/ProductLines.razor.cs index 4191d0b..73f18f6 100644 --- a/Gremlin_BlazorServer/Pages/ProductLines.razor.cs +++ b/Gremlin_BlazorServer/Pages/ProductLines.razor.cs @@ -36,13 +36,13 @@ public partial class ProductLines { private async Task OnRowInsertedAsync(SavedRowItem> productLine) { ProductLine newProductLine = await ResolveProductAsync(productLine.Item); - int count = await GenericController.InsertAsync(newProductLine); + int count = GenericController.Insert(newProductLine); Console.WriteLine($"Inserted {count} properties for new ProductLine {newProductLine.ProductLineCode}."); } private async Task OnRowUpdatedAsync(SavedRowItem> productLine) { ProductLine newProductLine = await ResolveProductAsync(productLine.Item); - int count = await GenericController.UpdateAsync(productLine.Item); + int count = GenericController.Update(productLine.Item); Console.WriteLine($"Updated {count} properties in ProductLine {newProductLine.ProductLineCode}."); } diff --git a/Gremlin_BlazorServer/Pages/Quotes/QuoteAdd.razor.cs b/Gremlin_BlazorServer/Pages/Quotes/QuoteAdd.razor.cs index 25c84a4..952077e 100644 --- a/Gremlin_BlazorServer/Pages/Quotes/QuoteAdd.razor.cs +++ b/Gremlin_BlazorServer/Pages/Quotes/QuoteAdd.razor.cs @@ -221,7 +221,7 @@ public partial class QuoteAdd { //TODO need to wait for modal! //Insert new CustomDescription to db - int count = await GenericController.InsertAsync(newCustomDescription); + int count = GenericController.Insert(newCustomDescription); Console.WriteLine($"Inserted {count} new CustomDescriptions"); // Product product = await GenericController.GetAsync(p => p.ProductNumber.Equals(lineItem.ProductNumber) && p.OptionNumber.Equals(lineItem.OptionNumber)); @@ -308,13 +308,14 @@ public partial class QuoteAdd { if (quote is { LineItems: not null, Recipient.Account: not null }) lineItemsNotReady = false; } - private async Task OnSave() { - if (await GenericController.InsertAsync(quote) > 0) { - // if (await GenericController.InsertAsync(quote.LineItems) > 0) { - Console.WriteLine($"Succesfully added quote {quote.QuotationNumber} to db with {quote.LineItems.Count} lineitems and a total net of {quote.TotalNet} euros."); - NavigationManager.NavigateTo("Quotes/QuoteIndex"); - // } - } + private Task OnSave() { + if (GenericController.Insert(quote) <= 0) return Task.CompletedTask; + // if (await GenericController.InsertAsync(quote.LineItems) > 0) { + Console.WriteLine($"Succesfully added quote {quote.QuotationNumber} to db with {quote.LineItems.Count} lineitems and a total net of {quote.TotalNet} euros."); + NavigationManager.NavigateTo("Quotes/QuoteIndex"); + // } + + return Task.CompletedTask; } private async Task OnCreateTex() { @@ -383,16 +384,18 @@ public partial class QuoteAdd { isReloadingLineItems = false; } - private static async Task OnRowInsertedAsync(SavedRowItem> customDescription) { + private static Task OnRowInsertedAsync(SavedRowItem> customDescription) { CustomDescription nCd = ResolveCustomDescription(customDescription.Item); - int count = await GenericController.InsertAsync(nCd); + int count = GenericController.Insert(nCd); Console.WriteLine($"Inserted {count} properties for new custom description {nCd.ProductNumber}#{nCd.OptionNumber}: {nCd.Heading}"); + return Task.CompletedTask; } - private static async Task OnRowUpdatedAsync(SavedRowItem> customDescription) { + private static Task OnRowUpdatedAsync(SavedRowItem> customDescription) { CustomDescription nCd = ResolveCustomDescription(customDescription.Item); - int count = await GenericController.UpdateAsync(customDescription.Item); + int count = GenericController.Update(customDescription.Item); Console.WriteLine($"Updated {count} properties for custom description {nCd.ProductNumber}#{nCd.OptionNumber}: {nCd.Heading}"); + return Task.CompletedTask; } private static async Task OnRowRemovedAsync(CustomDescription customDescription) { diff --git a/Gremlin_BlazorServer/Services/GenericController.cs b/Gremlin_BlazorServer/Services/GenericController.cs index a8ef5bf..150111d 100644 --- a/Gremlin_BlazorServer/Services/GenericController.cs +++ b/Gremlin_BlazorServer/Services/GenericController.cs @@ -125,10 +125,10 @@ public class GenericController { } } - public static async Task InsertAsync(T entity) where T : class, IMetadata { + public static int Insert(T entity) where T : class, IMetadata { try { gremlinDb.Set().Add(entity); - return await gremlinDb.SaveChangesAsync(); + return gremlinDb.SaveChanges(); } catch (DbUpdateException ex) { Console.WriteLine($"{ex.InnerException}"); @@ -141,11 +141,11 @@ public class GenericController { } } - public static async Task InsertAsync(IEnumerable entities) where T : class, IMetadata { + public static int Insert(IEnumerable entities) where T : class, IMetadata { // gremlinDb.Set().ToList(); //Get newest versions fresh from db try { gremlinDb.Set().AddRange(entities); - return await gremlinDb.SaveChangesAsync(); + return gremlinDb.SaveChanges(); } catch (DbUpdateException ex) { HandleConcurrencyExceptions(ex); @@ -167,11 +167,10 @@ public class GenericController { } } - public static async Task UpdateAsync(T entity) where T : class, IMetadata { - Task task = gremlinDb.Set().FirstOrDefaultAsync(t => t.Equals(entity)); //Get newest version fresh from db + public static int Update(T entity) where T : class, IMetadata { try { gremlinDb.Set().Update(entity); - return await gremlinDb.SaveChangesAsync(false); + return gremlinDb.SaveChanges(false); } catch (DbUpdateException ex) { HandleConcurrencyExceptions(ex); @@ -183,12 +182,10 @@ public class GenericController { } } - public static async Task UpdateAsync(IEnumerable entities) where T : class, IMetadata { - // IQueryable list = gremlinDb.Set().Where(t => t.Equals(entities)); //Get newest version fresh from db - + public static int Update(IEnumerable entities) where T : class, IMetadata { try { - await Task.Run(() => gremlinDb.Set().UpdateRange(entities)); - return await gremlinDb.SaveChangesAsync(); + gremlinDb.Set().UpdateRange(entities); + return gremlinDb.SaveChanges(); } catch (DbUpdateException ex) { HandleConcurrencyExceptions(ex); diff --git a/Gremlin_BlazorServer/Services/GenericImporter.cs b/Gremlin_BlazorServer/Services/GenericImporter.cs index 0b9e7d1..e308661 100644 --- a/Gremlin_BlazorServer/Services/GenericImporter.cs +++ b/Gremlin_BlazorServer/Services/GenericImporter.cs @@ -7,12 +7,11 @@ namespace Gremlin_BlazorServer.Services; public class GenericImporter { - public static async Task ImportCsvAsync(string fileContent) where T : class, IMetadata, new() { + public static void ImportCsv(string fileContent) where T : class, IMetadata, new() { Console.WriteLine($"GENERIC IMPORTER: Importing {typeof(T)} from csv..."); - List splitLines = await Task.Run(() => SplitLines(fileContent)); + List splitLines = SplitLines(fileContent); Console.WriteLine($"Found {splitLines.Count} potential {typeof(T)} in csv."); - ParseLinesToResultSet(splitLines); - Console.WriteLine($"GENERIC IMPORTER: Ready!"); + Console.WriteLine(ParseLinesToResultSet(splitLines) ? $"GENERIC IMPORTER: Ready." : $"GENERIC IMPORTER: Error by writing to db!"); } private static List SplitLines(string fileContent) { @@ -21,7 +20,7 @@ public class GenericImporter { return fileList; } - private static async void ParseLinesToResultSet(IEnumerable lineList) where T : class, IMetadata, new() { + private static bool ParseLinesToResultSet(IEnumerable lineList) where T : class, IMetadata, new() { Tuple? resultSet = new(new(), new()); List updatedItems = new(); List newItems = new(); @@ -35,9 +34,12 @@ public class GenericImporter { if (resultSet.Item2 is not null) newItems.Add(resultSet.Item2); } + int success = 0; Console.WriteLine($"Found {updatedItems.Count} updates and {newItems.Count} new items."); - if (updatedItems.Count > 0) await GenericController.UpdateAsync(updatedItems); - if (newItems.Count > 0) await GenericController.InsertAsync(newItems); + if (updatedItems.Count > 0) success += GenericController.Update(updatedItems); + if (newItems.Count > 0) success += GenericController.Insert(newItems); + + return success > 0; } private static Tuple? ParseToAccount(IReadOnlyList line) { // "ID" "Name" "Street" "City" "BP Role" "Postal Code" "Customer Type" "Market Indicator" "Phone" "E-Mail" "Market code" @@ -81,7 +83,6 @@ public class GenericImporter { existingAccount.PhoneNumber = readAccount.PhoneNumber; existingAccount.Street = readAccount.Street; existingAccount.Zip = readAccount.Zip; - Console.WriteLine($"Update in Account {existingAccount.SapAccountNumber}:{existingAccount.AccountName}"); updatedAccount = existingAccount; } else { @@ -119,12 +120,11 @@ public class GenericImporter { Gender = 0 }; - Contact? existingContact = GenericController.Get(a => a.SapContactNumber == sapContactNumber - || (a.LastName == readContact.LastName && a.FirstName == readContact.FirstName)); + Contact? existingContact = GenericController.Get(a => a.SapContactNumber == sapContactNumber || (a.LastName == readContact.LastName && a.FirstName == readContact.FirstName)); if (existingContact is not null) { if (IsEqualContact(readContact, existingContact)) { - Console.WriteLine($"---> The contact {readContact.SapContactNumber}:{readContact.FirstName} {readContact.LastName} ist aktuell."); + // Console.WriteLine($"---> The contact {readContact.SapContactNumber}:{readContact.FirstName} {readContact.LastName} ist aktuell."); return null; } @@ -148,24 +148,46 @@ public class GenericImporter { return new(updatedContact, newContact); } - 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 static bool IsEqualContact(Contact contact, Contact existingContact) - => contact.LastName == existingContact.LastName - && contact.FirstName == existingContact.FirstName - && contact.EMail == existingContact.EMail - && contact.PhoneNumber == existingContact.PhoneNumber - && contact.EmailBounced == existingContact.EmailBounced - && contact.OptInStatus == existingContact.OptInStatus; -} - -internal class Result { - public T? NewItem { get; set; } - public T? UpdatedItem { get; set; } + private static bool IsEqualAccount(Account readAccount, Account existingAccount) { + bool equalAccount = true; + if (readAccount.AccountName != existingAccount.AccountName) { + equalAccount = false; + Console.WriteLine($"--- UPDATE in Account {readAccount.SapAccountNumber} ---"); + Console.WriteLine($"{readAccount.AccountName} | {existingAccount.AccountName}"); + } + if (readAccount.City != existingAccount.City) { + equalAccount = false; + Console.WriteLine($"--- UPDATE in Account {readAccount.SapAccountNumber} ---"); + Console.WriteLine($"{readAccount.City} | {existingAccount.City}"); + } + if (readAccount.EMail != existingAccount.EMail) { + equalAccount = false; + Console.WriteLine($"--- UPDATE in Account {readAccount.SapAccountNumber} ---"); + Console.WriteLine($"{readAccount.EMail} | {existingAccount.EMail}"); + } + if (readAccount.PhoneNumber != existingAccount.PhoneNumber) { + equalAccount = false; + Console.WriteLine($"--- UPDATE in Account {readAccount.SapAccountNumber} ---"); + Console.WriteLine($"{readAccount.PhoneNumber} | {existingAccount.PhoneNumber}"); + } + if (readAccount.Street != existingAccount.Street) { + equalAccount = false; + Console.WriteLine($"--- UPDATE in Account {readAccount.SapAccountNumber} ---"); + Console.WriteLine($"{readAccount.Street} | {existingAccount.Street}"); + } + if (readAccount.Zip != existingAccount.Zip) { + equalAccount = false; + Console.WriteLine($"--- UPDATE in Account {readAccount.SapAccountNumber} ---"); + Console.WriteLine($"{readAccount.Zip} | {existingAccount.Zip}"); + } + return equalAccount; + } + + private static bool IsEqualContact(Contact readContact, Contact existingContact) + => readContact.LastName == existingContact.LastName + && readContact.FirstName == existingContact.FirstName + && readContact.EMail == existingContact.EMail + && readContact.PhoneNumber == existingContact.PhoneNumber + && readContact.EmailBounced == existingContact.EmailBounced + && readContact.OptInStatus == existingContact.OptInStatus; } \ No newline at end of file diff --git a/Gremlin_BlazorServer/Services/QuoteHandling.cs b/Gremlin_BlazorServer/Services/QuoteHandling.cs index 15c311a..99f0c6f 100644 --- a/Gremlin_BlazorServer/Services/QuoteHandling.cs +++ b/Gremlin_BlazorServer/Services/QuoteHandling.cs @@ -206,7 +206,7 @@ public class QuoteHandling { AccountId = 1 }; - int count = await GenericController.InsertAsync(newCustomDescription); + int count = GenericController.Insert(newCustomDescription); Console.WriteLine($"Inserted {count} new CustomDescriptions"); // Product product = await GenericController.GetAsync(p => p.ProductNumber.Equals(lineItem.ProductNumber) && p.OptionNumber.Equals(lineItem.OptionNumber));