DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Providers/EmployeeServiceProvider.cs
2023-08-31 11:10:38 -04:00

59 lines
2.3 KiB
C#

using DamageAssesment.Api.UsersAccess.Bases;
using DamageAssesment.Api.UsersAccess.Interfaces;
using DamageAssesment.Api.UsersAccess.Models;
using Newtonsoft.Json;
using System.Reflection;
namespace DamageAssesment.Api.SurveyResponses.Providers
{
public class EmployeeServiceProvider :ServiceProviderBase, IEmployeeServiceProvider
{
public EmployeeServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger<EmployeeServiceProvider> logger) : base(configuration, httpClient, logger, "/api/Employees", configuration.GetValue<string>("EndPointSettings:EmployeeUrlBase"))
{
}
public async Task<List<Employee>> getEmployeesAsync()
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var response = await httpClient.GetAsync(ressource);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var employees = JsonConvert.DeserializeObject<List<Employee>>(responseString);
if (employees == null || !employees.Any())
return null;
else return employees;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: EmployeeServiceProvider.getEmployeesAsync()");
return null;
}
}
public async Task<Employee> getEmployeeAsync(string employeeID)
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
//ressource = ressource + "/" + employeeID;
var response = await httpClient.GetAsync("/api/Employees/"+ employeeID);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var employee = JsonConvert.DeserializeObject<Employee>(responseString);
if (employee == null )
return null;
else return employee;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: EmployeeServiceProvider.getEmployeeAsync()");
return null;
}
}
}
}