DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Responses/Services/EmployeeServiceProvider.cs

52 lines
2.1 KiB
C#
Raw Normal View History

using DamageAssesment.Api.Responses.Interfaces;
2023-10-04 17:45:51 -05:00
using DamageAssesment.Api.Responses.Models;
using Microsoft.AspNetCore.Mvc.Routing;
2023-08-15 22:52:30 -05:00
using Newtonsoft.Json;
2023-10-04 17:45:51 -05:00
namespace DamageAssesment.Api.Responses.Services
2023-08-15 22:52:30 -05:00
{
public class EmployeeServiceProvider : ServiceProviderBase, IEmployeeServiceProvider
2023-08-15 22:52:30 -05:00
{
public EmployeeServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<EmployeeServiceProvider> logger) : base(configuration, httpUtil, logger, configuration.GetValue<string>("RessourceSettings:Employee"), configuration.GetValue<string>("EndPointSettings:EmployeeUrlBase"))
2023-08-15 22:52:30 -05:00
{
}
public async Task<List<Employee>> getEmployeesAsync(string token)
2023-08-15 22:52:30 -05:00
{
try
{
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null,token);
var employees = JsonConvert.DeserializeObject<List<Employee>>(responseJsonString);
2023-08-15 22:52:30 -05:00
if (employees == null || !employees.Any())
return new List<Employee>();
2023-08-15 22:52:30 -05:00
else return employees;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: EmployeeServiceProvider.getEmployeesAsync()");
return new List<Employee>();
2023-08-15 22:52:30 -05:00
}
}
public async Task<Employee> getEmployeeAsync(int employeeId, string token)
2023-08-15 22:52:30 -05:00
{
try
{
url = urlBase + string.Format(configuration.GetValue<string>("RessourceSettings:EmployeeById"), employeeId);
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null, token);
var employee = JsonConvert.DeserializeObject<Employee>(responseJsonString);
2023-08-15 22:52:30 -05:00
if (employee == null)
2023-08-15 22:52:30 -05:00
return null;
else return employee;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: EmployeeServiceProvider.getEmployeeAsync()");
return null;
}
}
}
}