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

81 lines
3.1 KiB
C#

using DamageAssesment.Api.SurveyResponses.Bases;
using DamageAssesment.Api.SurveyResponses.Interfaces;
using DamageAssesment.Api.SurveyResponses.Models;
using Newtonsoft.Json;
namespace DamageAssesment.Api.SurveyResponses.Providers
{
public class QuestionServiceProvider : ServiceProviderBase, IQuestionServiceProvider
{
public QuestionServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger<QuestionServiceProvider> logger) : base(configuration, httpClient, logger, "/api/Questions", configuration.GetValue<string>("EndPointSettings:QuestionUrlBase"))
{
}
public async Task<List<Question>> getQuestionsAsync()
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var response = await httpClient.GetAsync(ressource);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var questions = JsonConvert.DeserializeObject<List<Question>>(responseString);
if (questions == null || !questions.Any())
return null;
else return questions;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: QuestionServiceProvider.getQuestionsAsync()");
return null;
}
}
public async Task<List<SurveyQuestions>> getSurveyQuestionsAsync(int surveyId)
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var response = await httpClient.GetAsync("/api/GetSurveyQuestions/" + surveyId);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var questions = JsonConvert.DeserializeObject<List<SurveyQuestions>>(responseString);
if (questions == null || !questions.Any())
return null;
else return questions;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: QuestionServiceProvider.getSurveyQuestionsAsync()");
return null;
}
}
public async Task<Question> getQuestionsAsync(int questionId)
{
try
{
httpClient.BaseAddress = new Uri(urlBase);
var response = await httpClient.GetAsync("/api/Questions/" + questionId);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var question = JsonConvert.DeserializeObject<Question>(responseString);
if (question == null)
return null;
else return question;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: QuestionServiceProvider.getQuestionsAsync(questionId)");
return null;
}
}
}
}