diff --git a/TIAMMobileApp/Services/UserDataServiceMobile.cs b/TIAMMobileApp/Services/UserDataServiceMobile.cs index 4ee8cce4..3cf7a49f 100644 --- a/TIAMMobileApp/Services/UserDataServiceMobile.cs +++ b/TIAMMobileApp/Services/UserDataServiceMobile.cs @@ -1,5 +1,6 @@ using System.Net.Http.Json; using System.Text; +using System.Text.Json; using AyCode.Interfaces.StorageHandlers; using AyCode.Services.Loggers; using Newtonsoft.Json; @@ -20,9 +21,8 @@ namespace TIAMMobileApp.Services private readonly HttpClient http; private readonly ISecureStorageHandler secureStorageHandler; private readonly IServiceProviderDataService serviceProviderDataService; - private readonly ILogger _logger; + private readonly LoggerClient _logger; - public Dictionary userRoleTypes { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } public UserDataServiceMobile(HttpClient http, ISecureStorageHandler secureStorageHandler, IServiceProviderDataService serviceProviderDataService, IEnumerable logWriters) { @@ -183,16 +183,46 @@ namespace TIAMMobileApp.Services public async Task GetUserByEmailAsync(string email) { - var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetUserByEmail}"; - - _logger.Info("GetUserByEmailAsync url: " + url + ", " + email); - //GlobalLogger.Info("GetUserByEmailAsync url: " + url + ", " + email, "GLOBAL_LOGGER"); - var response = await http.PostAsJsonAsync(url, email); - var result = await response.Content.ReadAsStringAsync(); - var user = JsonConvert.DeserializeObject(result); - return user; + try + { + var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetUserByEmail}/{email}"; + _logger.Info("GetUserByEmailAsync url: " + url + ", " + email); + var response = await http.GetAsync(url); + + response.EnsureSuccessStatusCode(); + if (response.Content != null) + { + var jsonResponse = await response.Content.ReadAsStringAsync(); + var user = System.Text.Json.JsonSerializer.Deserialize(jsonResponse, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + return user; + } + + else + { + return null; + } + + } + catch (HttpRequestException httpRequestException) + { + // Handle specific HTTP request exceptions + _logger.DebugConditional($"Request error: {httpRequestException.Message}"); + throw; + } + catch (Exception ex) + { + // Handle other possible exceptions + _logger.DebugConditional($"An error occurred: {ex.Message}"); + throw; + } + + + } public async Task GetUserByIdAsync(Guid id) diff --git a/TIAMWebApp/Client/Services/UserDataServiceWeb.cs b/TIAMWebApp/Client/Services/UserDataServiceWeb.cs index 4ce09625..3bccb2fb 100644 --- a/TIAMWebApp/Client/Services/UserDataServiceWeb.cs +++ b/TIAMWebApp/Client/Services/UserDataServiceWeb.cs @@ -9,6 +9,9 @@ using TIAMWebApp.Shared.Application.Utility; using AyCode.Interfaces.StorageHandlers; using TIAM.Models.Dtos.Users; using AyCode.Services.Loggers; +using System.Net.Http; +using AyCode.Core.Extensions; +using System.Text.Json; namespace TIAMWebApp.Client.Services @@ -21,8 +24,7 @@ namespace TIAMWebApp.Client.Services private readonly IServiceProviderDataService serviceProviderDataService; private readonly LoggerClient _logger; - - public Dictionary userRoleTypes { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + public UserDataServiceWeb(HttpClient http, ISecureStorageHandler secureStorageHandler, IServiceProviderDataService serviceProviderDataService, IEnumerable logWriters) //public UserDataServiceWeb(HttpClient http, ISecureStorageHandler secureStorageHandler, IJSRuntime jSRuntime, IServiceProviderDataService serviceProviderDataService, HttpClientLogItemWriter logWriter) @@ -186,16 +188,46 @@ namespace TIAMWebApp.Client.Services public async Task GetUserByEmailAsync(string email) { - var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetUserByEmail}/{email}"; - _logger.Info("GetUserByEmailAsync url: " + url + ", " + email); - //GlobalLogger.Info("GetUserByEmailAsync url: " + url + ", " + email, "GLOBAL_LOGGER"); + + try + { + var url = $"{Setting.ApiBaseUrl}/{APIUrls.GetUserByEmail}/{email}"; + _logger.Info("GetUserByEmailAsync url: " + url + ", " + email); + var response = await http.GetAsync(url); + + response.EnsureSuccessStatusCode(); + if (response.Content != null) + { + var jsonResponse = await response.Content.ReadAsStringAsync(); + var user = System.Text.Json.JsonSerializer.Deserialize(jsonResponse, new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }); + return user; + } + + else + { + return null; + } + + } + catch (HttpRequestException httpRequestException) + { + // Handle specific HTTP request exceptions + _logger.DebugConditional($"Request error: {httpRequestException.Message}"); + throw; + } + catch (Exception ex) + { + // Handle other possible exceptions + _logger.DebugConditional($"An error occurred: {ex.Message}"); + throw; + } + - var response = (UserModelDto)(await http.GetFromJsonAsync(url,typeof (UserModelDto))); - - //var user = JsonConvert.DeserializeObject(result); - return response; } public async Task GetUserByIdAsync(Guid id) diff --git a/TIAMWebApp/Server/Controllers/UserAPIController.cs b/TIAMWebApp/Server/Controllers/UserAPIController.cs index 7e7746a5..efab911b 100644 --- a/TIAMWebApp/Server/Controllers/UserAPIController.cs +++ b/TIAMWebApp/Server/Controllers/UserAPIController.cs @@ -358,10 +358,20 @@ namespace TIAMWebApp.Server.Controllers [AllowAnonymous] [HttpGet] [Route(APIUrls.GetUserByEmailRouteName + "/{email}")] - public Task GetUserByEmail(string email) + public async Task? GetUserByEmail(string email) { - _logger.Info($"GetUserByEmail called with email: {email}"); - return _userDal.GetUserModelDtoByEmailAsync(email, false); + _logger.Info($"GetUserByEmail called with email: {email}"); + var result = _userDal.GetUserModelDtoByEmailAsync(email, false); + if (result.Result == null) + { + UserModelDto resultDto = new UserModelDto(); + return resultDto; + } + else + { + return result.Result; + } + } [AllowAnonymous] diff --git a/TIAMWebApp/Shared/Interfaces/IUserDataService.cs b/TIAMWebApp/Shared/Interfaces/IUserDataService.cs index cf918b60..99e29458 100644 --- a/TIAMWebApp/Shared/Interfaces/IUserDataService.cs +++ b/TIAMWebApp/Shared/Interfaces/IUserDataService.cs @@ -5,8 +5,7 @@ using TIAMWebApp.Shared.Application.Models.PageModels; namespace TIAMWebApp.Shared.Application.Interfaces { public interface IUserDataService - { - public Dictionary userRoleTypes { get; set; } + { public Task IsLoggedInAsync(Guid id);