97 lines
3.6 KiB
C#
97 lines
3.6 KiB
C#
using System.Globalization;
|
|
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();
|
|
private Quote selectedQuote = 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 async Task OnSelectedContactChanged(Contact newSelectedContact) {
|
|
selectedContact = newSelectedContact;
|
|
selectedContact.Quotes = await GenericController.GetAllAsync<Quote>(q => q.RecipientId.Equals(selectedContact.ContactId));
|
|
}
|
|
|
|
private async Task OnSelectedQuoteChanged(Quote newSelectedQuote) {
|
|
selectedQuote = newSelectedQuote;
|
|
selectedQuote.LineItems = await GenericController.GetAllAsync<LineItem>(lI => lI.QuoteId.Equals(selectedQuote.QuoteId));
|
|
}
|
|
|
|
private async Task OnRowInsertedAsync(SavedRowItem<Contact, Dictionary<string, object>> contact) {
|
|
Contact newContact = await ResolveContactAsync(contact.Item);
|
|
// if (newContact.Account is 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 is 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 is not 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();
|
|
}
|
|
} |