Gremlin/Gremlin_BlazorServer/Pages/Contacts.razor.cs

106 lines
3.9 KiB
C#

using Blazorise;
using Blazorise.DataGrid;
using Gremlin_BlazorServer.Data.EntityClasses;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using System.Globalization;
using System.Security.Claims;
namespace Gremlin_BlazorServer.Pages;
public partial class Contacts {
[CascadingParameter]
private Task<AuthenticationState>? authenticationStateTask { get; set; }
private IList<Contact>? contacts;
private Contact? selectedContact;
private readonly List<Contact>? selectedContacts;
private Quote? selectedQuote;
private readonly CultureInfo cultureInfo = new("de-DE");
private readonly int totalContacts;
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) {
selectedContact = _selectedContact;
selectedContact.Quotes = await genericController.GetAllAsync<Quote>(q => q.ContactId.Equals(selectedContact.ContactId));
}
return;
}
private async Task OnSelectedQuoteChanged(Quote _selectedQuote) {
if (_selectedQuote != null) {
selectedQuote = _selectedQuote;
selectedQuote.LineItems = await genericController.GetAllAsync<LineItem>(lI => lI.QuoteId.Equals(selectedQuote.QuoteId));
}
return;
}
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.GetAsync<Account>(a => a.AccountId.Equals(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();
return;
}
}