Gremlin/Gremlin_BlazorServer/Pages/Accounts.razor.cs

79 lines
3.1 KiB
C#

using Blazorise;
using Blazorise.DataGrid;
using Gremlin_BlazorServer.Data.EntityClasses;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using System.Security.Claims;
namespace Gremlin_BlazorServer.Pages;
public partial class Accounts {
[CascadingParameter]
private Task<AuthenticationState>? authenticationStateTask { get; set; }
private IList<Account>? accounts;
private Account? selectedAccount;
protected override async Task OnInitializedAsync() {
if (authenticationStateTask != null) {
ClaimsPrincipal user = (await authenticationStateTask).User;
if (user.Identity is { IsAuthenticated: true }) {
accounts = await genericController.GetAllAsync<Account>("AccountType", "SubMarket");
}
await base.OnInitializedAsync();
}
}
private void OnSelectedAccountChanged(Account _selectedAccount) {
selectedAccount = _selectedAccount;
selectedAccount.Contacts = genericController.GetAll<Contact>(c => c.AccountId == 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();
_ = await genericImporter.ImportCsvAsync<Account>(fileContent);
}
}
catch (Exception exception) {
Console.WriteLine(exception.Message);
}
StateHasChanged();
return;
}
private async Task OnRowInsertedAsync(SavedRowItem<Account, Dictionary<string, object>> account) {
Account newAccount = await ResolveAccountAsync(account.Item);
if (newAccount.AccountType == null || newAccount.SubMarket == null)
return;
int count = await genericController.InsertAsync(newAccount);
Console.WriteLine($"Inserted {count} properties for new account {newAccount.AccountId}: {newAccount.AccountName}.");
}
private async Task OnRowUpdatedAsync(SavedRowItem<Account, Dictionary<string, object>> account) {
Account newAccount = await ResolveAccountAsync(account.Item);
if (newAccount.AccountType == null || newAccount.SubMarket == null)
return;
int count = await genericController.UpdateAsync(account.Item);
Console.WriteLine($"Updated {count} properties in account {newAccount.AccountId}: {newAccount.AccountName}.");
}
private async Task OnRowRemovedAsync(Account account) {
int count = await genericController.RemoveAsync(account);
Console.WriteLine($"Removed {count} properties and account {account.AccountId}: {account.AccountName}.");
}
private async Task<Account> ResolveAccountAsync(Account newAccount) {
newAccount.DataModificationByUser = "Gremlin Blazor Server GUI";
newAccount.DataVersionNumber++;
newAccount.AccountType = await genericController.GetAsync<AccountType>(aT => aT.AccountTypeCode.Equals("SUP"));
newAccount.SubMarket = await genericController.GetAsync<SubMarket>(sM => sM.SubMarketCode.Equals("VEN"));
return newAccount;
}
}