Add API interface to call API

This commit is contained in:
Vikas Nale 2025-07-25 14:36:32 +05:30
parent 9acb9974a0
commit 0f65ccace5
4 changed files with 145 additions and 47 deletions

View File

@ -0,0 +1,57 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace Marco.Pms.UtilityApplication
{
internal class ApiService
{
private readonly HttpClient _httpClient;
private readonly string _apiUrl;
public ApiService(string apiUrl)
{
_httpClient = new HttpClient();
_apiUrl = apiUrl;
}
public async Task<bool> SendDataAsync<T>(T data)
{
try
{
var jsonContent = JsonConvert.SerializeObject(data);
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
HttpResponseMessage response = await _httpClient.PostAsync(_apiUrl, content);
response.EnsureSuccessStatusCode(); // Throws an exception if the HTTP response status is an error code.
string responseBody = await response.Content.ReadAsStringAsync();
// You can parse the responseBody if your API returns something useful
Console.WriteLine($"API Response: {responseBody}");
return true;
}
catch (HttpRequestException ex)
{
Console.WriteLine($"API request error: {ex.Message}");
// Log or handle the error appropriately in your UI
return false;
}
catch (JsonException ex)
{
Console.WriteLine($"JSON serialization error: {ex.Message}");
return false;
}
catch (Exception ex)
{
Console.WriteLine($"An unexpected error occurred: {ex.Message}");
return false;
}
}
}
}

View File

@ -13,30 +13,38 @@ namespace Marco.Pms.UtilityApplication
public class DirectoryHelper public class DirectoryHelper
{ {
public async Task<List<CreateContactDto>> GenerateCreateContactDto(List<Contacts> contactsData, List<CategoryMaster> categoryMasters, List<TagsMaster> tagsMaster, List<ProjectMaster> projectMaster) public async Task<(List<CreateContactDto>, List<Contacts>) > GenerateCreateContactDto(List<Contacts> contactsData, List<CategoryMaster> categoryMasters, List<TagsMaster> tagsMaster, List<ProjectMaster> projectMaster)
{ {
List<CreateContactDto> lstCreateContactDto = new List<CreateContactDto>(); List<CreateContactDto> lstCreateContactDto = new List<CreateContactDto>();
List<Contacts> failedContacts = new List<Contacts>();
CreateContactDto dto = null; CreateContactDto dto = null;
foreach (Contacts contact in contactsData) foreach (Contacts contact in contactsData)
{ {
dto = new CreateContactDto(); if (contact.Name != "")
dto.Name = contact.Name; {
dto.Organization = contact.Organization; dto = new CreateContactDto();
dto.Address = contact.Address; dto.Name = contact.Name;
dto.ContactCategoryId = GetContactCategoryID(contact, categoryMasters); dto.Organization = contact.Organization;
dto.Description = contact.MoreInformation; dto.Address = contact.Address;
dto.ContactPhones = GetContactPhones(contact); dto.ContactCategoryId = GetContactCategoryID(contact, categoryMasters);
dto.ContactEmails = GetContactEmails(contact); dto.Description = contact.MoreInformation;
dto.ProjectIds = GetContactProjectIDs(contact, projectMaster); dto.ContactPhones = GetContactPhones(contact);
dto.BucketIds = GetContactBucketIDs(contact); dto.ContactEmails = GetContactEmails(contact);
dto.Tags = GetContactTags(contact, tagsMaster); dto.ProjectIds = GetContactProjectIDs(contact, projectMaster);
dto.BucketIds = GetContactBucketIDs(contact);
lstCreateContactDto.Add(dto); dto.Tags = GetContactTags(contact, tagsMaster);
lstCreateContactDto.Add(dto);
}
else
{
// export contact to log
failedContacts.Add(contact);
}
} }
return lstCreateContactDto; return (lstCreateContactDto, failedContacts);
} }
private List<ContactTagDto>? GetContactTags(Contacts contact, List<TagsMaster> tagsMaster) private List<ContactTagDto>? GetContactTags(Contacts contact, List<TagsMaster> tagsMaster)
@ -226,9 +234,9 @@ namespace Marco.Pms.UtilityApplication
{ {
categoryMasters.Add(new CategoryMaster categoryMasters.Add(new CategoryMaster
{ {
CategoryID = transactionsSheet.Cells[row, 1].Text, CategoryID = transactionsSheet.Cells[row, 1].Text.Trim(),
Categories = transactionsSheet.Cells[row, 2].Text, Categories = transactionsSheet.Cells[row, 2].Text.Trim(),
Description = transactionsSheet.Cells[row, 3].Text, Description = transactionsSheet.Cells[row, 3].Text.Trim(),
}); });
} }
catch (FormatException ex) catch (FormatException ex)
@ -253,8 +261,8 @@ namespace Marco.Pms.UtilityApplication
{ {
tagsMaster.Add(new TagsMaster tagsMaster.Add(new TagsMaster
{ {
TagID = tagsMasterSheet.Cells[row, 1].Text, TagID = tagsMasterSheet.Cells[row, 1].Text.Trim(),
Tag = tagsMasterSheet.Cells[row, 2].Text, Tag = tagsMasterSheet.Cells[row, 2].Text.Trim(),
}); });
} }
catch (FormatException ex) catch (FormatException ex)
@ -279,8 +287,8 @@ namespace Marco.Pms.UtilityApplication
{ {
projectMaster.Add(new ProjectMaster projectMaster.Add(new ProjectMaster
{ {
ProjectID = projectsMasterSheet.Cells[row, 1].Text, ProjectID = projectsMasterSheet.Cells[row, 1].Text.Trim(),
ProjectName = projectsMasterSheet.Cells[row, 2].Text, ProjectName = projectsMasterSheet.Cells[row, 2].Text.Trim(),
}); });
} }
catch (FormatException ex) catch (FormatException ex)
@ -306,23 +314,23 @@ namespace Marco.Pms.UtilityApplication
{ {
contactsData.Add(new Contacts contactsData.Add(new Contacts
{ {
BucketId = masterSheet.Cells[row, 1].Text, BucketId = masterSheet.Cells[row, 1].Text.Trim(),
Owner = masterSheet.Cells[row, 2].Text, Owner = masterSheet.Cells[row, 2].Text.Trim(),
Name = masterSheet.Cells[row, 3].Text, Name = masterSheet.Cells[row, 3].Text.Trim(),
Designation = masterSheet.Cells[row, 4].Text, Designation = masterSheet.Cells[row, 4].Text.Trim(),
Organization = masterSheet.Cells[row, 5].Text, Organization = masterSheet.Cells[row, 5].Text.Trim(),
Mobile1 = masterSheet.Cells[row, 6].Text, Mobile1 = masterSheet.Cells[row, 6].Text.Trim(),
Mobile2 = masterSheet.Cells[row, 7].Text, Mobile2 = masterSheet.Cells[row, 7].Text.Trim(),
Mobile3 = masterSheet.Cells[row, 8].Text, Mobile3 = masterSheet.Cells[row, 8].Text.Trim(),
Email1 = masterSheet.Cells[row, 9].Text, Email1 = masterSheet.Cells[row, 9].Text.Trim(),
Email2 = masterSheet.Cells[row, 10].Text, Email2 = masterSheet.Cells[row, 10].Text.Trim(),
Location = masterSheet.Cells[row, 11].Text, Location = masterSheet.Cells[row, 11].Text.Trim(),
Address = masterSheet.Cells[row, 12].Text, Address = masterSheet.Cells[row, 12].Text.Trim(),
MoreInformation = masterSheet.Cells[row, 13].Text, MoreInformation = masterSheet.Cells[row, 13].Text.Trim(),
Category = masterSheet.Cells[row, 14].Text, Category = masterSheet.Cells[row, 14].Text.Trim(),
Tags = masterSheet.Cells[row, 15].Text, Tags = masterSheet.Cells[row, 15].Text.Trim(),
Project = masterSheet.Cells[row, 16].Text Project = masterSheet.Cells[row, 16].Text.Trim()
}); });
} }
catch (FormatException ex) catch (FormatException ex)

View File

@ -5,12 +5,17 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Marco.Pms.UtilityApplication" xmlns:local="clr-namespace:Marco.Pms.UtilityApplication"
mc:Ignorable="d" mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"> Title="MainWindow" Height="450" Width="800" WindowState="Maximized">
<Grid> <Grid Margin="0,0,0,-113" UseLayoutRounding="False">
<TextBox HorizontalAlignment="Left" Margin="207,72,0,0" TextWrapping="Wrap" Text="E:\Downloads\Directory Contacts - Marketing Team Yatin.xlsx" VerticalAlignment="Top" Width="348" Name="txtFilePath" FontSize="16"/> <Grid.ColumnDefinitions>
<Label Content="Select File" HorizontalAlignment="Left" Margin="86,66,0,0" VerticalAlignment="Top" Name="lblFileName" Height="29" Width="99" FontSize="16"/> <ColumnDefinition Width="7*"/>
<Button Content="Upload File" HorizontalAlignment="Left" Margin="560,72,0,0" VerticalAlignment="Top" Name="btnFileUpload" FontSize="16" Click="btnFileUpload_Click"/> <ColumnDefinition Width="93*"/>
<Button Content="Submit" HorizontalAlignment="Left" Margin="446,124,0,0" VerticalAlignment="Top" x:Name="btnSubmit" FontSize="16" RenderTransformOrigin="0.147,2.815" Click="btnSubmit_Click"/> </Grid.ColumnDefinitions>
<TextBox HorizontalAlignment="Left" Margin="80,36,0,0" TextWrapping="Wrap" Text="E:\Downloads\Directory Contacts - Marketing Team Yatin.xlsx" VerticalAlignment="Top" Width="508" Name="txtFilePath" FontSize="16" Grid.Column="1"/>
<Label Content="Select File" HorizontalAlignment="Left" Margin="36,30,0,0" VerticalAlignment="Top" Name="lblFileName" Height="29" Width="99" FontSize="16" Grid.ColumnSpan="2"/>
<Button Content="Upload File" HorizontalAlignment="Left" Margin="598,37,0,0" VerticalAlignment="Top" Name="btnFileUpload" FontSize="16" Click="btnFileUpload_Click" Width="118" Grid.Column="1"/>
<Button Content="Submit" HorizontalAlignment="Left" Margin="309,70,0,0" VerticalAlignment="Top" x:Name="btnSubmit" FontSize="16" RenderTransformOrigin="0.147,2.815" Click="btnSubmit_Click" Width="117" Grid.Column="1"/>
<DataGrid d:ItemsSource="{d:SampleData ItemCount=5}" Margin="0,118,0,0" Name="ContactsDataGrid" Grid.ColumnSpan="2"/>
</Grid> </Grid>
</Window> </Window>

View File

@ -9,10 +9,16 @@ namespace Marco.Pms.UtilityApplication
/// </summary> /// </summary>
public partial class MainWindow : Window public partial class MainWindow : Window
{ {
DirectoryHelper helper = new DirectoryHelper(); //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() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
_apiService = new ApiService(ApiEndpoint);
_directoryHelper = new DirectoryHelper();
} }
private void btnFileUpload_Click(object sender, RoutedEventArgs e) private void btnFileUpload_Click(object sender, RoutedEventArgs e)
@ -37,11 +43,33 @@ namespace Marco.Pms.UtilityApplication
List<TagsMaster> tagsMaster = null; List<TagsMaster> tagsMaster = null;
List<ProjectMaster> projectMaster = null; List<ProjectMaster> projectMaster = null;
List<CategoryMaster> categoryMasters = null; List<CategoryMaster> categoryMasters = null;
List<CreateContactDto> createContactDto = null;
List<Contacts> failedContacts = null;
(contactsData, categoryMasters, tagsMaster, projectMaster) = await _directoryHelper.ReadExcelData(txtFilePath.Text);
(contactsData, categoryMasters, tagsMaster, projectMaster) = await helper.ReadExcelData(txtFilePath.Text); ( createContactDto, failedContacts) = await _directoryHelper.GenerateCreateContactDto(contactsData, categoryMasters, tagsMaster, projectMaster);
List<CreateContactDto> createContactDto = await helper.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);
}
} }
} }
} }