DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Services/QuestionServiceProvider.cs

71 lines
2.9 KiB
C#
Raw Normal View History

using DamageAssesment.Api.SurveyResponses.Interfaces;
2023-08-15 22:52:30 -05:00
using DamageAssesment.Api.SurveyResponses.Models;
using Newtonsoft.Json;
namespace DamageAssesment.Api.SurveyResponses.Services
2023-08-15 22:52:30 -05:00
{
public class QuestionServiceProvider : ServiceProviderBase, IQuestionServiceProvider
{
public QuestionServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<QuestionServiceProvider> logger) : base(configuration, httpUtil, logger, configuration.GetValue<string>("RessourceSettings:Question"), configuration.GetValue<string>("EndPointSettings:QuestionUrlBase"))
2023-08-15 22:52:30 -05:00
{
}
public async Task<List<Question>> getQuestionsAsync()
{
try
{
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
var questions = JsonConvert.DeserializeObject<List<Question>>(responseJsonString);
2023-08-15 22:52:30 -05:00
if (questions == null || !questions.Any())
return new List<Question>();
2023-08-15 22:52:30 -05:00
else return questions;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: QuestionServiceProvider.getQuestionsAsync()");
return new List<Question>();
2023-08-15 22:52:30 -05:00
}
}
public async Task<List<SurveyQuestions>> getSurveyQuestionsAsync(int surveyId)
{
try
{
url = urlBase + string.Format(configuration.GetValue<string>("RessourceSettings:SurveyQuestion"), surveyId);
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
var questions = JsonConvert.DeserializeObject<List<SurveyQuestions>>(responseJsonString);
2023-08-15 22:52:30 -05:00
if (questions == null || !questions.Any())
return new List<SurveyQuestions>() ;
2023-08-15 22:52:30 -05:00
else return questions;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: QuestionServiceProvider.getSurveyQuestionsAsync()");
return new List<SurveyQuestions>();
2023-08-15 22:52:30 -05:00
}
}
public async Task<Question> getQuestionsAsync(int questionId)
{
try
{
url = urlBase + string.Format(configuration.GetValue<string>("RessourceSettings:QuestionById"), questionId);
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
var question = JsonConvert.DeserializeObject<Question>(responseJsonString);
2023-08-15 22:52:30 -05:00
if (question == null)
return null;
else return question;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: QuestionServiceProvider.getQuestionsAsync(questionId)");
return null;
}
}
}
}