Gremlin/Gremlin_BlazorServer/Pages/CustomDescriptions.razor.cs

77 lines
3.8 KiB
C#

using System.Diagnostics;
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 = await GenericController.GetAllAsync<CustomDescription>();
await base.OnInitializedAsync();
}
}
private void OnSelectedCustomDescriptionChanged(CustomDescription newSelectedCustomDescription) => selectedCustomDescription = newSelectedCustomDescription;
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();
GenericImporter.ImportCsv<CustomDescription>(fileContent);
}
}
catch (Exception exception) {
Console.WriteLine(exception.Message);
}
StateHasChanged();
}
private static Task OnRowInsertedAsync(SavedRowItem<CustomDescription, Dictionary<string, object>> customDescription) {
CustomDescription newCustomDescription = ResolveCustomDescriptionAsync(customDescription.Item);
int count = GenericController.Insert(newCustomDescription);
Console.WriteLine($"Inserted {count} properties for new custom description {newCustomDescription.ProductNumber}#{newCustomDescription.OptionNumber}: {newCustomDescription.Heading}");
return Task.CompletedTask;
}
private static Task OnRowUpdatedAsync(SavedRowItem<CustomDescription, Dictionary<string, object>> customDescription) {
CustomDescription newCustomDescription = ResolveCustomDescriptionAsync(customDescription.Item);
int count = GenericController.Update(customDescription.Item);
Console.WriteLine($"Updated {count} properties for custom description {newCustomDescription.ProductNumber}#{newCustomDescription.OptionNumber}: {newCustomDescription.Heading}");
return Task.CompletedTask;
}
private static 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 static CustomDescription ResolveCustomDescriptionAsync(CustomDescription newCustomDescription) {
// IList<Product> products = await GenericController.GetAllAsync<Product>(p => p.ProductNumber == newCustomDescription.ProductNumber);
// if (products is not null && products.Count > 0)
// newCustomDescription.ProductId = products[0].ProductId;
// else
// Debug.WriteLine($"Found no product with ProductId {newCustomDescription.ProductNumber}");
newCustomDescription.DataModificationByUser = "Gremlin Blazor Server GUI";
newCustomDescription.DataVersionNumber++;
return newCustomDescription;
}
}