Gremlin/Gremlin_BlazorServer/Pages/CustomDescriptions.razor.cs

73 lines
3.4 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;
namespace Gremlin_BlazorServer.Pages;
public partial class CustomDescriptions {
private IList<CustomDescription>? customDescriptions;
private CustomDescription? selectedCustomDescription;
[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 }) customDescriptions = GenericController.GetAll<CustomDescription>("Supplier");
await base.OnInitializedAsync();
}
}
private void OnSelectedCustomDescriptionChanged(CustomDescription selectedCustomDescription) => this.selectedCustomDescription = selectedCustomDescription;
private async Task OnImportCustomDescriptions(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<CustomDescription>(fileContent);
}
}
catch (Exception exception) {
Console.WriteLine(exception.Message);
}
StateHasChanged();
}
private async Task OnRowInsertedAsync(SavedRowItem<CustomDescription, Dictionary<string, object>> customDescription) {
CustomDescription newCustomDescription = await ResolveCustomDescriptionAsync(customDescription.Item);
if (newCustomDescription.ProductNumber == null || newCustomDescription.OptionNumber == null)
return;
int count = await GenericController.InsertAsync(newCustomDescription);
Console.WriteLine($"Inserted {count} properties for new custom description {newCustomDescription.ProductNumber}#{newCustomDescription.OptionNumber}: {newCustomDescription.Heading}");
}
private async Task OnRowUpdatedAsync(SavedRowItem<CustomDescription, Dictionary<string, object>> customDescription) {
CustomDescription newCustomDescription = await ResolveCustomDescriptionAsync(customDescription.Item);
if (newCustomDescription.ProductNumber == null || newCustomDescription.OptionNumber == null)
return;
int count = await GenericController.UpdateAsync(customDescription.Item);
Console.WriteLine($"Updated {count} properties for custom description {newCustomDescription.ProductNumber}#{newCustomDescription.OptionNumber}: {newCustomDescription.Heading}");
}
private async Task OnRowRemovedAsync(CustomDescription customDescription) {
int count = await GenericController.RemoveAsync(customDescription);
Console.WriteLine($"Removed {count} properties and custom description {customDescription.ProductNumber}#{customDescription.OptionNumber}: {customDescription.Heading}");
}
private async Task<CustomDescription> ResolveCustomDescriptionAsync(CustomDescription newCustomDescription) {
newCustomDescription.Supplier = await GenericController.GetAsync<Account>(a => a.AccountId.Equals(newCustomDescription.AccountId));
newCustomDescription.DataModificationByUser = "Gremlin Blazor Server GUI";
newCustomDescription.DataVersionNumber++;
return newCustomDescription;
}
}