DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Responses/Services/SurveyServiceProvider.cs
2023-10-04 18:45:51 -04:00

53 lines
2.1 KiB
C#

using DamageAssesment.Api.Responses.Interfaces;
using DamageAssesment.Api.Responses.Models;
using Newtonsoft.Json;
namespace DamageAssesment.Api.Responses.Services
{
public class SurveyServiceProvider : ServiceProviderBase, ISurveyServiceProvider
{
public SurveyServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<EmployeeServiceProvider> logger) : base(configuration, httpUtil, logger, configuration.GetValue<string>("RessourceSettings:Survey"), configuration.GetValue<string>("EndPointSettings:SurveyUrlBase"))
{
}
public async Task<List<Survey>> getSurveysAsync(string language)
{
try
{
if (!string.IsNullOrEmpty(language))
url = url + "/" + language;
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
var surveys = JsonConvert.DeserializeObject<List<Survey>>(responseJsonString);
if (surveys == null || !surveys.Any())
return new List<Survey>();
else return surveys;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyServiceProvider.getSurveysAsync()");
return new List<Survey>();
}
}
public async Task<Survey> getSurveyAsync(int surveyId)
{
try
{
url = urlBase + string.Format(configuration.GetValue<string>("RessourceSettings:SurveyById"), surveyId);
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
var survey = JsonConvert.DeserializeObject<Survey>(responseJsonString);
if (survey == null)
return null;
else return survey;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyServiceProvider.getSurveyAsync()");
return null;
}
}
}
}