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