using Blazorise; using Blazorise.DataGrid; using Gremlin_BlazorServer.Data.EntityClasses; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; using System.Globalization; using System.Security.Claims; namespace Gremlin_BlazorServer.Pages; public partial class Contacts { [CascadingParameter] private Task? authenticationStateTask { get; set; } private IList? contacts; private Contact? selectedContact; private readonly List? selectedContacts; private Quote? selectedQuote; private readonly CultureInfo cultureInfo = new("de-DE"); private readonly int totalContacts; protected override async Task OnInitializedAsync() { if (authenticationStateTask != null) { ClaimsPrincipal user = (await authenticationStateTask).User; if (user.Identity is { IsAuthenticated: true }) { contacts = await genericController.GetAllAsync(); } } await base.OnInitializedAsync(); } private async Task OnSelectedContactChanged(Contact _selectedContact) { if (_selectedContact != null) { selectedContact = _selectedContact; selectedContact.Quotes = await genericController.GetAllAsync(q => q.ContactId.Equals(selectedContact.ContactId)); } return; } private async Task OnSelectedQuoteChanged(Quote _selectedQuote) { if (_selectedQuote != null) { selectedQuote = _selectedQuote; selectedQuote.LineItems = await genericController.GetAllAsync(lI => lI.QuoteId.Equals(selectedQuote.QuoteId)); } return; } private async Task OnRowInsertedAsync(SavedRowItem> contact) { Contact newContact = await ResolveContactAsync(contact.Item); if (newContact.Account == 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; int count = await genericController.UpdateAsync(contact.Item); Console.WriteLine($"Updated {count} properties in contact {newContact.ContactId}: {newContact.FirstName} {newContact.LastName}."); } private async Task OnRowRemovedAsync(Contact contact) { int count = await genericController.RemoveAsync(contact); Console.WriteLine($"Removed {count} properties and contact {contact.ContactId}: {contact.FirstName} {contact.LastName}."); } private async Task ResolveContactAsync(Contact newContact) { newContact.Account = await genericController.GetAsync(a => a.AccountId.Equals(newContact.AccountId)); if (newContact.Account != null) { newContact.NoPhoneCalls = false; newContact.EmailBounced = false; newContact.NoHardcopyMailing = false; newContact.ValidatedContact = true; newContact.DataModificationByUser = "Gremlin Blazor Server GUI"; newContact.DataVersionNumber++; } else { Console.WriteLine($"No account with AccountId {newContact.AccountId} found!"); } return newContact; } private async Task OnImportContacts(FileChangedEventArgs fileChangedEventArgs) { try { foreach (IFileEntry? file in fileChangedEventArgs.Files) { using MemoryStream stream = new(); await file.WriteToStreamAsync(stream); _ = stream.Seek(0, SeekOrigin.Begin); using StreamReader reader = new(stream); string fileContent = await reader.ReadToEndAsync(); _ = await genericImporter.ImportCsvAsync(fileContent); } } catch (Exception exception) { Console.WriteLine(exception.Message); } StateHasChanged(); return; } }