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 readonly IList accounts = new List(); private Account? selectedAccount; private static int ImportProgress { get; set; } [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 }) { ImportProgress = 0; await ApplicationLoadingIndicatorService.Show(); accounts.AddRange(await GenericController.GetAllAsync()); //selectedAccount = accounts.First(); //await OnSelectedAccountChanged(selectedAccount); await ApplicationLoadingIndicatorService.Hide(); } await base.OnInitializedAsync(); } } 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) { 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(); GenericImporter.ImportCsv(fileContent); } } catch (Exception exception) { Console.WriteLine(exception.Message); } StateHasChanged(); } private static Task OnRowInsertedAsync(SavedRowItem> account) { Account newAccount = ResolveAccountAsync(account.Item); // if (newAccount.AccountType is null || newAccount.SubMarket is null) return; int count = GenericController.Insert(newAccount); Console.WriteLine($"Inserted {count} properties for new account {newAccount.AccountId}: {newAccount.AccountName}."); return Task.CompletedTask; } private static Task OnRowUpdatedAsync(SavedRowItem> account) { Account newAccount = ResolveAccountAsync(account.Item); if (newAccount.AccountType == null || newAccount.SubMarket == null) 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) { int count = await GenericController.RemoveAsync(account); Console.WriteLine($"Removed {count} properties and account {account.AccountId}: {account.AccountName}."); } private static Account 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")); return newAccount; } private static async Task OnRemoveDublicates() { int i = await GenericController.RemoveDublicatesAsync(); Console.WriteLine($"Removed {i} dublicates from Accounts."); } }