Gremlin/Gremlin_BlazorServer/Pages/Contacts.razor.cs

97 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 Contacts {
// private readonly CultureInfo cultureInfo = new("de-DE");
private readonly IList<Contact> contacts = new List<Contact>();
private Contact selectedContact = new();
[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 }) {
contacts.AddRange(await GenericController.GetAllAsync<Contact>());
selectedContact = contacts.First();
}
}
await base.OnInitializedAsync();
}
private Task OnSelectedContactChanged(Contact newSelectedContact) {
selectedContact = newSelectedContact;
return Task.CompletedTask;
// selectedContact.Quotes = await GenericController.GetAllAsync<Quote>(q => q.RecipientId.Equals(selectedContact.ContactId));
}
private async Task OnRowInsertedAsync(SavedRowItem<Contact, Dictionary<string, object>> contact) {
Contact newContact = await ResolveContactAsync(contact.Item);
// if (newContact.Account is null) return;
int count = GenericController.Insert(newContact);
Console.WriteLine($"Inserted {count} properties for new contact {newContact.ContactId}: {newContact.FirstName} {newContact.LastName}.");
}
private async Task OnRowUpdatedAsync(SavedRowItem<Contact, Dictionary<string, object>> contact) {
Contact newContact = await ResolveContactAsync(contact.Item);
// if (newContact.Account is null) return;
int count = GenericController.Update(contact.Item);
Console.WriteLine($"Updated {count} properties in contact {newContact.ContactId}: {newContact.FirstName} {newContact.LastName}.");
}
private async Task OnRowRemovedAsync(Contact contact) {
int count = await GenericController.RemoveAsync(contact);
Console.WriteLine($"Removed {count} properties and contact {contact.ContactId}: {contact.FirstName} {contact.LastName}.");
}
private static Task<Contact> ResolveContactAsync(Contact newContact) {
// newContact.Account = await GenericController.ResolveAccountById(newContact.AccountId);
if (newContact.AccountId > 0) {
newContact.NoPhoneCalls = false;
newContact.EmailBounced = false;
newContact.NoHardcopyMailing = false;
newContact.ValidatedContact = true;
newContact.DataModificationByUser = "Gremlin Blazor Server GUI";
newContact.DataVersionNumber++;
}
else {
Console.WriteLine($"No account with AccountId {newContact.AccountId} found!");
}
return Task.FromResult(newContact);
}
private async Task OnImportContacts(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<Contact>(fileContent);
}
}
catch (Exception exception) {
Console.WriteLine(exception.Message);
}
StateHasChanged();
}
private static async Task OnRemoveDublicates() {
int i = await GenericController.RemoveDublicatesAsync<Contact>();
Console.WriteLine($"Removed {i} dublicates from Contacts.");
}
}