using DamageAssesment.Api.SurveyResponses.Interfaces; using DamageAssesment.Api.SurveyResponses.Models; using Newtonsoft.Json; namespace DamageAssesment.Api.SurveyResponses.Services { public class AnswerServiceProvider : ServiceProviderBase, IAnswerServiceProvider { public AnswerServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger logger) : base(configuration, httpUtil, logger, configuration.GetValue("RessourceSettings:Answer"), configuration.GetValue("EndPointSettings:AnswerUrlBase")) { } public async Task> getAnswersAsync() { try { var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null); var answers = JsonConvert.DeserializeObject>(responseJsonString); if (answers == null || !answers.Any()) return new List(); else return answers; } catch (Exception ex) { logger?.LogError($"Exception Found : {ex.Message} - Ref: AnswerServiceProvider.getAnswersAsync()"); return new List(); } } public async Task> GetAnswersByResponseIdAsync(int responseId) { try { url = urlBase + string.Format(configuration.GetValue("RessourceSettings:AnswerByResponse"), responseId); var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null); var answers = JsonConvert.DeserializeObject>(responseJsonString); if (answers == null || !answers.Any()) return new List(); else return answers; } catch (Exception ex) { logger?.LogError($"Exception Found : {ex.Message} - Ref: AnswerServiceProvider.GetAnswersByResponseId()"); return new List(); } } public async Task PostAnswersAsync(Answer answer) { try { var requestJsonString = JsonConvert.SerializeObject(answer); var responseJsonString = await httpUtil.SendAsync(HttpMethod.Post, url, requestJsonString); var answers = JsonConvert.DeserializeObject(responseJsonString); if (answers == null) { logger?.LogError($"Answers cannot be added - Ref: AnswerServiceProvider.PostAnswersAsync()"); return null; } else return answers; } catch (Exception ex) { logger?.LogError($"Exception Found : {ex.Message} - Ref: AnswerServiceProvider.PostAnswersAsync()"); return null; } } } }