DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.SurveyResponses/Services/AttachmentServiceProvider.cs

58 lines
2.3 KiB
C#
Raw Normal View History

using DamageAssesment.Api.SurveyResponses.Interfaces;
2023-08-15 22:52:30 -05:00
using DamageAssesment.Api.SurveyResponses.Models;
using Newtonsoft.Json;
namespace DamageAssesment.Api.SurveyResponses.Services
2023-08-15 22:52:30 -05:00
{
public class AttachmentServiceProvider : ServiceProviderBase, IAttachmentServiceProvider
{
public AttachmentServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<AttachmentServiceProvider> logger) : base(configuration, httpUtil, logger, "/api/Attachments", configuration.GetValue<string>("EndPointSettings:AttachmentUrlBase"))
2023-08-15 22:52:30 -05:00
{
}
public async Task<List<Attachment>> getAttachmentsAsync()
{
try
{
var url = urlBase + ressource;
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
var attachments = JsonConvert.DeserializeObject<List<Attachment>>(responseJsonString);
2023-08-15 22:52:30 -05:00
if (attachments == null || !attachments.Any())
return new List<Attachment>();
2023-08-15 22:52:30 -05:00
else return attachments;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: AttachmentServiceProvider.getAttachmentsAsync()");
return new List<Attachment>();
2023-08-15 22:52:30 -05:00
}
}
public async Task<IEnumerable<Attachment>> PostAttachmentsAsync(AttachmentInfo attachmentInfo)
{
try
{
var url = urlBase + ressource;
var requestJsonString = JsonConvert.SerializeObject(attachmentInfo);
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Post, url, requestJsonString);
var attachments = JsonConvert.DeserializeObject<IEnumerable<Attachment>>(responseJsonString);
2023-08-15 22:52:30 -05:00
if (attachments == null)
{
logger?.LogError($"Attachments cannot be added - Ref: AttachmentServiceProvider.PostAttachmentsAsync()");
return null;
}
else return attachments;
}
catch (Exception ex)
{
logger?.LogError($"Exception Found : {ex.Message} - Ref: AttachmentServiceProvider.PostAttachmentsAsync()");
return null;
}
}
}
}