2023-08-15 22:52:30 -05:00
|
|
|
|
using DamageAssesment.Api.SurveyResponses.Bases;
|
|
|
|
|
using DamageAssesment.Api.SurveyResponses.Db;
|
|
|
|
|
using DamageAssesment.Api.SurveyResponses.Interfaces;
|
|
|
|
|
using DamageAssesment.Api.SurveyResponses.Models;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System.Data.Common;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text.Json.Nodes;
|
|
|
|
|
using System.Text;
|
2023-08-27 10:55:58 -05:00
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
using System;
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
|
|
|
|
namespace DamageAssesment.Api.SurveyResponses.Providers
|
|
|
|
|
{
|
|
|
|
|
public class AnswerServiceProvider : ServiceProviderBase, IAnswerServiceProvider
|
|
|
|
|
{
|
2023-09-04 20:31:41 -05:00
|
|
|
|
public AnswerServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<AnswerServiceProvider> logger) : base(configuration, httpUtil, logger, "/api/Answers", configuration.GetValue<string>("EndPointSettings:AnswerUrlBase"))
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
public async Task<List<Answer>> getAnswersAsync()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-09-04 20:31:41 -05:00
|
|
|
|
var url = urlBase + ressource;
|
|
|
|
|
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
|
|
|
|
|
var answers = JsonConvert.DeserializeObject<List<Answer>>(responseJsonString);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
|
|
|
|
if (answers == null || !answers.Any())
|
2023-09-04 20:31:41 -05:00
|
|
|
|
return new List<Answer>();
|
2023-08-15 22:52:30 -05:00
|
|
|
|
else return answers;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError($"Exception Found : {ex.Message} - Ref: AnswerServiceProvider.getAnswersAsync()");
|
2023-09-04 20:31:41 -05:00
|
|
|
|
return new List<Answer>();
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<List<Answer>> GetAnswersByResponseIdAsync(int responseId)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-09-04 20:31:41 -05:00
|
|
|
|
var url = urlBase + "/api/AnswersByResponse/" + responseId;
|
|
|
|
|
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
|
|
|
|
|
var answers = JsonConvert.DeserializeObject<List<Answer>>(responseJsonString);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
|
|
|
|
if (answers == null || !answers.Any())
|
2023-09-04 20:31:41 -05:00
|
|
|
|
return new List<Answer>();
|
2023-08-15 22:52:30 -05:00
|
|
|
|
else return answers;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError($"Exception Found : {ex.Message} - Ref: AnswerServiceProvider.GetAnswersByResponseId()");
|
2023-09-04 20:31:41 -05:00
|
|
|
|
return new List<Answer>();
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Answer> PostAnswersAsync(Answer answer)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-08-27 10:55:58 -05:00
|
|
|
|
var url = urlBase + ressource;
|
2023-09-04 20:31:41 -05:00
|
|
|
|
var requestJsonString = JsonConvert.SerializeObject(answer);
|
|
|
|
|
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Post, url, requestJsonString);
|
|
|
|
|
var answers = JsonConvert.DeserializeObject<Answer>(responseJsonString);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
2023-08-27 10:55:58 -05:00
|
|
|
|
if (answers == null)
|
|
|
|
|
{
|
2023-09-04 20:31:41 -05:00
|
|
|
|
logger?.LogError($"Answers cannot be added - Ref: AnswerServiceProvider.PostAnswersAsync()");
|
2023-08-15 22:52:30 -05:00
|
|
|
|
return null;
|
2023-08-27 10:55:58 -05:00
|
|
|
|
}
|
2023-08-15 22:52:30 -05:00
|
|
|
|
else return answers;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logger?.LogError($"Exception Found : {ex.Message} - Ref: AnswerServiceProvider.PostAnswersAsync()");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|