From 0f65ccace59e09105d4a06e0677e5894ff5a7cc4 Mon Sep 17 00:00:00 2001 From: Vikas Nale Date: Fri, 25 Jul 2025 14:36:32 +0530 Subject: [PATCH] Add API interface to call API --- Marco.Pms.UtilityApplication/ApiService.cs | 57 +++++++++++++ .../DirectoryHelper.cs | 84 ++++++++++--------- Marco.Pms.UtilityApplication/MainWindow.xaml | 17 ++-- .../MainWindow.xaml.cs | 34 +++++++- 4 files changed, 145 insertions(+), 47 deletions(-) create mode 100644 Marco.Pms.UtilityApplication/ApiService.cs diff --git a/Marco.Pms.UtilityApplication/ApiService.cs b/Marco.Pms.UtilityApplication/ApiService.cs new file mode 100644 index 0000000..1d72521 --- /dev/null +++ b/Marco.Pms.UtilityApplication/ApiService.cs @@ -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 SendDataAsync(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; + } + } + } +} diff --git a/Marco.Pms.UtilityApplication/DirectoryHelper.cs b/Marco.Pms.UtilityApplication/DirectoryHelper.cs index 7a02d3b..bf1b511 100644 --- a/Marco.Pms.UtilityApplication/DirectoryHelper.cs +++ b/Marco.Pms.UtilityApplication/DirectoryHelper.cs @@ -13,30 +13,38 @@ namespace Marco.Pms.UtilityApplication public class DirectoryHelper { - public async Task> GenerateCreateContactDto(List contactsData, List categoryMasters, List tagsMaster, List projectMaster) + public async Task<(List, List) > GenerateCreateContactDto(List contactsData, List categoryMasters, List tagsMaster, List projectMaster) { List lstCreateContactDto = new List(); + List failedContacts = new List(); CreateContactDto dto = null; foreach (Contacts contact in contactsData) { - dto = new CreateContactDto(); - dto.Name = contact.Name; - dto.Organization = contact.Organization; - dto.Address = contact.Address; - dto.ContactCategoryId = GetContactCategoryID(contact, categoryMasters); - dto.Description = contact.MoreInformation; - dto.ContactPhones = GetContactPhones(contact); - dto.ContactEmails = GetContactEmails(contact); - dto.ProjectIds = GetContactProjectIDs(contact, projectMaster); - dto.BucketIds = GetContactBucketIDs(contact); - dto.Tags = GetContactTags(contact, tagsMaster); - - lstCreateContactDto.Add(dto); + if (contact.Name != "") + { + dto = new CreateContactDto(); + dto.Name = contact.Name; + dto.Organization = contact.Organization; + dto.Address = contact.Address; + dto.ContactCategoryId = GetContactCategoryID(contact, categoryMasters); + dto.Description = contact.MoreInformation; + dto.ContactPhones = GetContactPhones(contact); + dto.ContactEmails = GetContactEmails(contact); + dto.ProjectIds = GetContactProjectIDs(contact, projectMaster); + dto.BucketIds = GetContactBucketIDs(contact); + dto.Tags = GetContactTags(contact, tagsMaster); + lstCreateContactDto.Add(dto); + } + else + { + // export contact to log + failedContacts.Add(contact); + } } - return lstCreateContactDto; + return (lstCreateContactDto, failedContacts); } private List? GetContactTags(Contacts contact, List tagsMaster) @@ -226,9 +234,9 @@ namespace Marco.Pms.UtilityApplication { categoryMasters.Add(new CategoryMaster { - CategoryID = transactionsSheet.Cells[row, 1].Text, - Categories = transactionsSheet.Cells[row, 2].Text, - Description = transactionsSheet.Cells[row, 3].Text, + CategoryID = transactionsSheet.Cells[row, 1].Text.Trim(), + Categories = transactionsSheet.Cells[row, 2].Text.Trim(), + Description = transactionsSheet.Cells[row, 3].Text.Trim(), }); } catch (FormatException ex) @@ -253,8 +261,8 @@ namespace Marco.Pms.UtilityApplication { tagsMaster.Add(new TagsMaster { - TagID = tagsMasterSheet.Cells[row, 1].Text, - Tag = tagsMasterSheet.Cells[row, 2].Text, + TagID = tagsMasterSheet.Cells[row, 1].Text.Trim(), + Tag = tagsMasterSheet.Cells[row, 2].Text.Trim(), }); } catch (FormatException ex) @@ -279,8 +287,8 @@ namespace Marco.Pms.UtilityApplication { projectMaster.Add(new ProjectMaster { - ProjectID = projectsMasterSheet.Cells[row, 1].Text, - ProjectName = projectsMasterSheet.Cells[row, 2].Text, + ProjectID = projectsMasterSheet.Cells[row, 1].Text.Trim(), + ProjectName = projectsMasterSheet.Cells[row, 2].Text.Trim(), }); } catch (FormatException ex) @@ -306,23 +314,23 @@ namespace Marco.Pms.UtilityApplication { contactsData.Add(new Contacts { - BucketId = masterSheet.Cells[row, 1].Text, - Owner = masterSheet.Cells[row, 2].Text, - Name = masterSheet.Cells[row, 3].Text, - Designation = masterSheet.Cells[row, 4].Text, - Organization = masterSheet.Cells[row, 5].Text, + BucketId = masterSheet.Cells[row, 1].Text.Trim(), + Owner = masterSheet.Cells[row, 2].Text.Trim(), + Name = masterSheet.Cells[row, 3].Text.Trim(), + Designation = masterSheet.Cells[row, 4].Text.Trim(), + Organization = masterSheet.Cells[row, 5].Text.Trim(), - Mobile1 = masterSheet.Cells[row, 6].Text, - Mobile2 = masterSheet.Cells[row, 7].Text, - Mobile3 = masterSheet.Cells[row, 8].Text, - Email1 = masterSheet.Cells[row, 9].Text, - Email2 = masterSheet.Cells[row, 10].Text, - Location = masterSheet.Cells[row, 11].Text, - Address = masterSheet.Cells[row, 12].Text, - MoreInformation = masterSheet.Cells[row, 13].Text, - Category = masterSheet.Cells[row, 14].Text, - Tags = masterSheet.Cells[row, 15].Text, - Project = masterSheet.Cells[row, 16].Text + Mobile1 = masterSheet.Cells[row, 6].Text.Trim(), + Mobile2 = masterSheet.Cells[row, 7].Text.Trim(), + Mobile3 = masterSheet.Cells[row, 8].Text.Trim(), + Email1 = masterSheet.Cells[row, 9].Text.Trim(), + Email2 = masterSheet.Cells[row, 10].Text.Trim(), + Location = masterSheet.Cells[row, 11].Text.Trim(), + Address = masterSheet.Cells[row, 12].Text.Trim(), + MoreInformation = masterSheet.Cells[row, 13].Text.Trim(), + Category = masterSheet.Cells[row, 14].Text.Trim(), + Tags = masterSheet.Cells[row, 15].Text.Trim(), + Project = masterSheet.Cells[row, 16].Text.Trim() }); } catch (FormatException ex) diff --git a/Marco.Pms.UtilityApplication/MainWindow.xaml b/Marco.Pms.UtilityApplication/MainWindow.xaml index 553aecb..045222c 100644 --- a/Marco.Pms.UtilityApplication/MainWindow.xaml +++ b/Marco.Pms.UtilityApplication/MainWindow.xaml @@ -5,12 +5,17 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Marco.Pms.UtilityApplication" mc:Ignorable="d" - Title="MainWindow" Height="450" Width="800"> - - -