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

74 lines
3.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;
using Microsoft.Extensions.Primitives;
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 AnswerServiceProvider : ServiceProviderBase, IAnswerServiceProvider
{
public AnswerServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<AnswerServiceProvider> logger) : base(configuration, httpUtil, logger, configuration.GetValue<string>("RessourceSettings:Answer"), configuration.GetValue<string>("EndPointSettings:AnswerUrlBase"))
2023-08-15 22:52:30 -05:00
{
}
public async Task<List<Answer>> getAnswersAsync(string token)
2023-08-15 22:52:30 -05:00
{
try
{
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null, token);
var answers = JsonConvert.DeserializeObject<List<Answer>>(responseJsonString);
2023-08-15 22:52:30 -05:00
if (answers == null || !answers.Any())
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()");
return new List<Answer>();
2023-08-15 22:52:30 -05:00
}
}
public async Task<List<Answer>> GetAnswersByResponseIdAsync(int responseId, string token)
2023-08-15 22:52:30 -05:00
{
try
{
url = urlBase + string.Format(configuration.GetValue<string>("RessourceSettings:AnswerByResponse"), responseId);
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null,token);
var answers = JsonConvert.DeserializeObject<List<Answer>>(responseJsonString);
2023-08-15 22:52:30 -05:00
if (answers == null || !answers.Any())
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()");
return new List<Answer>();
2023-08-15 22:52:30 -05:00
}
}
public async Task<Answer> PostAnswersAsync(Answer answer, string token )
2023-08-15 22:52:30 -05:00
{
try
{
var requestJsonString = JsonConvert.SerializeObject(answer);
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Post, url, requestJsonString, token);
var answers = JsonConvert.DeserializeObject<Answer>(responseJsonString);
2023-08-15 22:52:30 -05:00
if (answers == null)
{
logger?.LogError($"Answers cannot be added - Ref: AnswerServiceProvider.PostAnswersAsync()");
2023-08-15 22:52:30 -05:00
return null;
}
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;
}
}
}
}