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 Opportunities { private readonly IList opportunities = new List(); private Opportunity? selectedOpportunity; 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(); opportunities.AddRange(await GenericController.GetAllAsync()); selectedOpportunity = opportunities.FirstOrDefault(); await OnSelectedOpportunityChanged(selectedOpportunity); await ApplicationLoadingIndicatorService.Hide(); } await base.OnInitializedAsync(); } } public Task OnSelectedOpportunityChanged(Opportunity? newSelectedOpportunity) => Task.FromResult(selectedOpportunity = newSelectedOpportunity); private async Task OnImportOpportunities(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> opportunity) { Opportunity newOpportunity = ResolveOpportunityAsync(opportunity.Item); // if (newOpportunity.OpportunityType is null || newOpportunity.SubMarket is null) return; int count = GenericController.Insert(newOpportunity); Console.WriteLine($"Inserted {count} properties for new Opportunity {newOpportunity.OpportunityId}: {newOpportunity.Description}."); return Task.CompletedTask; } private static Task OnRowUpdatedAsync(SavedRowItem> opportunity) { Opportunity newOpportunity = ResolveOpportunityAsync(opportunity.Item); int count = GenericController.Update(opportunity.Item); Console.WriteLine($"Updated {count} properties in Opportunity {newOpportunity.OpportunityId}: {newOpportunity.Description}."); return Task.CompletedTask; } private static async Task OnRowRemovedAsync(Opportunity opportunity) { int count = await GenericController.RemoveAsync(opportunity); Console.WriteLine($"Removed {count} properties and Opportunity {opportunity.OpportunityId}: {opportunity.Description}."); } private static Opportunity ResolveOpportunityAsync(Opportunity newOpportunity) { newOpportunity.DataModificationByUser = "Gremlin Blazor Server GUI"; newOpportunity.DataVersionNumber++; // newOpportunity.OpportunityType = await GenericController.GetAsync(aT => aT.OpportunityTypeCode.Equals("SUP")); // newOpportunity.SubMarket = await GenericController.GetAsync(sM => sM.SubMarketCode.Equals("VEN")); return newOpportunity; } private static async Task OnRemoveDublicates() { int i = await GenericController.RemoveDublicatesAsync(); Console.WriteLine($"Removed {i} dublicates from Opportunities."); } }