DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Providers/SurveyServiceProvider.cs
2023-08-15 23:52:30 -04:00

60 lines
2.2 KiB
C#

using DamageAssesment.Api.SurveyResponses.Bases;
using DamageAssesment.Api.SurveyResponses.Interfaces;
using DamageAssesment.Api.SurveyResponses.Models;
using Newtonsoft.Json;
using System.Reflection;
namespace DamageAssesment.Api.SurveyResponses.Providers
{
public class SurveyServiceProvider :ServiceProviderBase, ISurveyServiceProvider
{
public SurveyServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger<EmployeeServiceProvider> logger) : base(configuration, httpClient, logger, "/api/Surveys", configuration.GetValue<string>("EndPointSettings:SurveyUrlBase"))
{
}
public async Task<List<Survey>> getSurveysAsync()
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var response = await httpClient.GetAsync(ressource);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var surveys = JsonConvert.DeserializeObject<List<Survey>>(responseString);
if (surveys == null || !surveys.Any())
return null;
else return surveys;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyServiceProvider.getSurveysAsync()");
return null;
}
}
public async Task<Survey> getSurveyAsync(int surveyId)
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var response = await httpClient.GetAsync(ressource+"/"+ surveyId);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var survey = JsonConvert.DeserializeObject<Survey>(responseString);
if (survey == null )
return null;
else return survey;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyServiceProvider.getSurveyAsync()");
return null;
}
}
}
}