Gremlin/Gremlin_BlazorServer/Pages/Contacts.razor.cs

101 lines
3.7 KiB
C#

using System.Globalization;
using System.Security.Claims;
using Blazorise;
using Blazorise.DataGrid;
using Gremlin_BlazorServer.Data.EntityClasses;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
namespace Gremlin_BlazorServer.Pages;
public partial class Contacts {
private readonly CultureInfo cultureInfo = new("de-DE");
private readonly List<Contact>? selectedContacts;
private readonly int totalContacts;
private IList<Contact>? contacts;
private Contact? selectedContact;
private Quote? selectedQuote;
[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 = await GenericController.GetAllAsync<Contact>();
}
await base.OnInitializedAsync();
}
private async Task OnSelectedContactChanged(Contact selectedContact) {
if (selectedContact != null) {
this.selectedContact = selectedContact;
this.selectedContact.Quotes = await GenericController.GetAllAsync<Quote>(q => q.RecipientId.Equals(this.selectedContact.ContactId));
}
}
private async Task OnSelectedQuoteChanged(Quote selectedQuote) {
if (selectedQuote != null) {
this.selectedQuote = selectedQuote;
this.selectedQuote.LineItems = await GenericController.GetAllAsync<LineItem>(lI => lI.QuoteId.Equals(this.selectedQuote.QuoteId));
}
}
private async Task OnRowInsertedAsync(SavedRowItem<Contact, Dictionary<string, object>> contact) {
Contact newContact = await ResolveContactAsync(contact.Item);
if (newContact.Account == null)
return;
int count = await GenericController.InsertAsync(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 == null)
return;
int count = await GenericController.UpdateAsync(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 async Task<Contact> ResolveContactAsync(Contact newContact) {
newContact.Account = await GenericController.ResolveAccountById(newContact.AccountId);
if (newContact.Account != null) {
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 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();
_ = await GenericImporter.ImportCsvAsync<Account>(fileContent);
}
}
catch (Exception exception) {
Console.WriteLine(exception.Message);
}
StateHasChanged();
}
}