forked from MDCPS/DamageAssessment_Backend
Adding external services to Service folder
This commit is contained in:
@ -0,0 +1,74 @@
|
||||
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<AnswerServiceProvider> logger) : base(configuration, httpUtil, logger, "/api/Answers", configuration.GetValue<string>("EndPointSettings:AnswerUrlBase"))
|
||||
{
|
||||
}
|
||||
public async Task<List<Answer>> getAnswersAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = urlBase + ressource;
|
||||
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
|
||||
var answers = JsonConvert.DeserializeObject<List<Answer>>(responseJsonString);
|
||||
|
||||
if (answers == null || !answers.Any())
|
||||
return new List<Answer>();
|
||||
else return answers;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: AnswerServiceProvider.getAnswersAsync()");
|
||||
return new List<Answer>();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<Answer>> GetAnswersByResponseIdAsync(int responseId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = urlBase + "/api/AnswersByResponse/" + responseId;
|
||||
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
|
||||
var answers = JsonConvert.DeserializeObject<List<Answer>>(responseJsonString);
|
||||
|
||||
if (answers == null || !answers.Any())
|
||||
return new List<Answer>();
|
||||
else return answers;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: AnswerServiceProvider.GetAnswersByResponseId()");
|
||||
return new List<Answer>();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Answer> PostAnswersAsync(Answer answer)
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = urlBase + ressource;
|
||||
var requestJsonString = JsonConvert.SerializeObject(answer);
|
||||
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Post, url, requestJsonString);
|
||||
var answers = JsonConvert.DeserializeObject<Answer>(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Interfaces;
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Services
|
||||
{
|
||||
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"))
|
||||
{
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (attachments == null || !attachments.Any())
|
||||
return new List<Attachment>();
|
||||
else return attachments;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: AttachmentServiceProvider.getAttachmentsAsync()");
|
||||
return new List<Attachment>();
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Interfaces;
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Services
|
||||
{
|
||||
public class EmployeeServiceProvider :ServiceProviderBase, IEmployeeServiceProvider
|
||||
{
|
||||
public EmployeeServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<EmployeeServiceProvider> logger) : base(configuration, httpUtil, logger, "/api/Employees", configuration.GetValue<string>("EndPointSettings:EmployeeUrlBase"))
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<List<Employee>> getEmployeesAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = urlBase + ressource;
|
||||
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
|
||||
var employees = JsonConvert.DeserializeObject<List<Employee>>(responseJsonString);
|
||||
|
||||
if (employees == null || !employees.Any())
|
||||
return new List<Employee>();
|
||||
else return employees;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: EmployeeServiceProvider.getEmployeesAsync()");
|
||||
return new List<Employee>();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Employee> getEmployeeAsync(string employeeId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = urlBase + "/api/Employees/" + employeeId;
|
||||
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
|
||||
var employee = JsonConvert.DeserializeObject<Employee>(responseJsonString);
|
||||
|
||||
if (employee == null )
|
||||
return null;
|
||||
else return employee;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: EmployeeServiceProvider.getEmployeeAsync()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Interfaces;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Services
|
||||
{
|
||||
public class HttpUtil : IHttpUtil
|
||||
{
|
||||
private readonly HttpClient httpClient;
|
||||
private readonly ILogger<HttpUtil> logger;
|
||||
|
||||
public HttpUtil(HttpClient httpClient, ILogger<HttpUtil> logger)
|
||||
{
|
||||
this.httpClient = httpClient;
|
||||
this.logger = logger;
|
||||
}
|
||||
public async Task<string> SendAsync(HttpMethod method, string url, string JsonInput)
|
||||
{
|
||||
try
|
||||
{
|
||||
var request = new HttpRequestMessage(method, url);
|
||||
request.Headers.Accept.Clear();
|
||||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
||||
|
||||
//request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
||||
if (method == HttpMethod.Post)
|
||||
{
|
||||
request.Content = new StringContent(JsonInput, Encoding.UTF8, "application/json");
|
||||
}
|
||||
|
||||
var response = await httpClient.SendAsync(request, CancellationToken.None);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
return responseString;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Message : {ex.Message} - Ref: HttpUtil.SendAsync()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Interfaces;
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Services
|
||||
{
|
||||
public class LocationServiceProvider :ServiceProviderBase, ILocationServiceProvider
|
||||
{
|
||||
public LocationServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<LocationServiceProvider> logger) : base(configuration, httpUtil, logger, "/api/Locations", configuration.GetValue<string>("EndPointSettings:LocationUrlBase"))
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<List<Location>> getLocationsAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = urlBase + ressource;
|
||||
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
|
||||
var locations = JsonConvert.DeserializeObject<List<Location>>(responseJsonString);
|
||||
|
||||
if (locations == null || !locations.Any())
|
||||
return new List<Location>();
|
||||
else return locations;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: LocationServiceProvider.getLocationsAsync()");
|
||||
return new List<Location>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Interfaces;
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Services
|
||||
{
|
||||
public class QuestionServiceProvider : ServiceProviderBase, IQuestionServiceProvider
|
||||
{
|
||||
public QuestionServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<QuestionServiceProvider> logger) : base(configuration, httpUtil, logger, "/api/Questions", configuration.GetValue<string>("EndPointSettings:QuestionUrlBase"))
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<List<Question>> getQuestionsAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = urlBase + ressource;
|
||||
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
|
||||
var questions = JsonConvert.DeserializeObject<List<Question>>(responseJsonString);
|
||||
|
||||
if (questions == null || !questions.Any())
|
||||
return new List<Question>();
|
||||
else return questions;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: QuestionServiceProvider.getQuestionsAsync()");
|
||||
return new List<Question>();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<SurveyQuestions>> getSurveyQuestionsAsync(int surveyId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = urlBase + "/api/GetSurveyQuestions/" + surveyId;
|
||||
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
|
||||
var questions = JsonConvert.DeserializeObject<List<SurveyQuestions>>(responseJsonString);
|
||||
|
||||
if (questions == null || !questions.Any())
|
||||
return new List<SurveyQuestions>() ;
|
||||
else return questions;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: QuestionServiceProvider.getSurveyQuestionsAsync()");
|
||||
return new List<SurveyQuestions>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<Question> getQuestionsAsync(int questionId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = urlBase + "/api/Questions/" + questionId;
|
||||
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
|
||||
var question = JsonConvert.DeserializeObject<Question>(responseJsonString);
|
||||
|
||||
if (question == null)
|
||||
return null;
|
||||
else return question;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: QuestionServiceProvider.getQuestionsAsync(questionId)");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Interfaces;
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Services
|
||||
{
|
||||
public class RegionServiceProvider : ServiceProviderBase, IRegionServiceProvider
|
||||
{
|
||||
public RegionServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<RegionServiceProvider> logger) : base(configuration, httpUtil, logger, "/api/Regions", configuration.GetValue<string>("EndPointSettings:LocationUrlBase"))
|
||||
{
|
||||
}
|
||||
public async Task<List<Region>> getRegionsAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = urlBase + ressource;
|
||||
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
|
||||
var regions = JsonConvert.DeserializeObject<List<Region>>(responseJsonString);
|
||||
|
||||
if (regions == null || !regions.Any())
|
||||
return new List<Region>();
|
||||
else return regions;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: RegionServiceProvider.getRegionsAsync()");
|
||||
return new List<Region>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Interfaces;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Services
|
||||
{
|
||||
public class ServiceProviderBase
|
||||
{
|
||||
protected readonly IConfiguration configuration;
|
||||
protected readonly IHttpUtil httpUtil;
|
||||
protected readonly ILogger<ServiceProviderBase> logger;
|
||||
protected string ressource;
|
||||
protected string urlBase;
|
||||
|
||||
|
||||
public ServiceProviderBase(IConfiguration configuration, IHttpUtil httpUtil, ILogger<ServiceProviderBase> logger, string ressource, string urlBase)
|
||||
{
|
||||
this.configuration = configuration;
|
||||
this.httpUtil = httpUtil;
|
||||
this.logger = logger;
|
||||
this.ressource = ressource;
|
||||
this.urlBase = urlBase;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Interfaces;
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Services
|
||||
{
|
||||
public class SurveyServiceProvider :ServiceProviderBase, ISurveyServiceProvider
|
||||
{
|
||||
public SurveyServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<EmployeeServiceProvider> logger) : base(configuration, httpUtil, logger, "/api/Surveys", configuration.GetValue<string>("EndPointSettings:SurveyUrlBase"))
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<List<Survey>> getSurveysAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
var url = urlBase + ressource;
|
||||
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
|
||||
var surveys = JsonConvert.DeserializeObject<List<Survey>>(responseJsonString);
|
||||
|
||||
if (surveys == null || !surveys.Any())
|
||||
return new List<Survey>();
|
||||
else return surveys;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyServiceProvider.getSurveysAsync()");
|
||||
return new List<Survey>();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Survey> getSurveyAsync(int surveyId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = urlBase + ressource + "/" + surveyId;
|
||||
var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null);
|
||||
var survey = JsonConvert.DeserializeObject<Survey>(responseJsonString);
|
||||
|
||||
if (survey == null )
|
||||
return null;
|
||||
else return survey;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyServiceProvider.getSurveyAsync()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user