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

93 lines
4.0 KiB
C#
Raw Normal View History

2023-10-04 17:45:51 -05:00
using DamageAssesment.Api.Responses.Interfaces;
using DamageAssesment.Api.Responses.Models;
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 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(string language, string token)
2023-08-15 22:52:30 -05:00
{
try
{
if (!string.IsNullOrEmpty(language))
url = url + "/" + language;
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null, token);
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<QuestionCategory>> GetQuestionCategoriesAsync(string? language, string token)
{
try
{
url = urlBase + configuration.GetValue<string>("RessourceSettings:QuestionCategory");
2023-08-15 22:52:30 -05:00
if (!string.IsNullOrEmpty(language))
url = url + "/" + language;
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null, token);
var questions = JsonConvert.DeserializeObject<List<QuestionCategory>>(responseJsonString);
2023-08-15 22:52:30 -05:00
if (questions == null || !questions.Any())
return new List<QuestionCategory>();
else return questions;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: QuestionServiceProvider.GetQuestionCategoriesAsync()");
return new List<QuestionCategory>();
}
}
public async Task<List<SurveyQuestions>> getSurveyQuestionsAsync(int surveyId, string token)
2023-08-15 22:52:30 -05:00
{
try
{
url = urlBase + string.Format(configuration.GetValue<string>("RessourceSettings:SurveyQuestion"), surveyId);
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null, token);
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, string token)
2023-08-15 22:52:30 -05:00
{
try
{
url = urlBase + string.Format(configuration.GetValue<string>("RessourceSettings:QuestionById"), questionId);
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null, token);
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;
}
}
}
}