Gremlin/Gremlin_BlazorServer/Pages/Opportunities.razor.cs

86 lines
4.0 KiB
C#

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<Opportunity> opportunities = new List<Opportunity>();
private Opportunity? selectedOpportunity;
private static int ImportProgress { get; set; }
[CascadingParameter] private Task<AuthenticationState>? 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<Opportunity>());
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<Opportunity>(fileContent);
}
}
catch (Exception exception) {
Console.WriteLine(exception.Message);
}
StateHasChanged();
}
private static Task OnRowInsertedAsync(SavedRowItem<Opportunity, Dictionary<string, object>> 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, Dictionary<string, object>> 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<OpportunityType>(aT => aT.OpportunityTypeCode.Equals("SUP"));
// newOpportunity.SubMarket = await GenericController.GetAsync<SubMarket>(sM => sM.SubMarketCode.Equals("VEN"));
return newOpportunity;
}
private static async Task OnRemoveDublicates() {
int i = await GenericController.RemoveDublicatesAsync<Opportunity>();
Console.WriteLine($"Removed {i} dublicates from Opportunities.");
}
}