75 lines
2.9 KiB
C#

using Marco.Pms.Model.Dtos.Directory;
using Microsoft.Win32;
using System.Windows;
namespace Marco.Pms.UtilityApplication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
//DirectoryHelper helper = new DirectoryHelper();
private readonly DirectoryHelper _directoryHelper;
private readonly ApiService _apiService;
// Replace with your actual API endpoint
private const string ApiEndpoint = "https://your-api-url.com/api/data";
public MainWindow()
{
InitializeComponent();
_apiService = new ApiService(ApiEndpoint);
_directoryHelper = new DirectoryHelper();
}
private void btnFileUpload_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Excel files (*.xlsx)|*.xlsx|CSV files (*.csv)|*.csv"; // Optional: filter file types
if (openFileDialog.ShowDialog() == true)
{
string filePath = openFileDialog.FileName;
// Now you can use filePath to read or process the selected file
txtFilePath.Text = filePath;
//MessageBox.Show($"Selected file: {filePath}");
}
}
private async void btnSubmit_Click(object sender, RoutedEventArgs e)
{
List<Contacts> contactsData = null;
List<TagsMaster> tagsMaster = null;
List<ProjectMaster> projectMaster = null;
List<CategoryMaster> categoryMasters = null;
List<CreateContactDto> createContactDto = null;
List<Contacts> failedContacts = null;
(contactsData, categoryMasters, tagsMaster, projectMaster) = await _directoryHelper.ReadExcelData(txtFilePath.Text);
( createContactDto, failedContacts) = await _directoryHelper.GenerateCreateContactDto(contactsData, categoryMasters, tagsMaster, projectMaster);
// Check if there's data to display
if (failedContacts != null && failedContacts.Any())
{
// Assign the list to the DataGrid's ItemsSource
ContactsDataGrid.ItemsSource = failedContacts;
// You can optionally update a status text block
// StatusTextBlock.Text = $"Displayed {importedContacts.Count} contacts.";
}
else
{
// Clear the DataGrid if no data
ContactsDataGrid.ItemsSource = null;
// StatusTextBlock.Text = "No contacts to display.";
}
foreach (Contacts contact in contactsData)
{
//.Text = $"Sending {apiDataList.Count} records to API...";
bool success = await _apiService.SendDataAsync(contact);
}
}
}
}