forked from MDCPS/DamageAssessment_Backend
Copy from old Repository
This commit is contained in:
@ -0,0 +1,21 @@
|
||||
namespace DamageAssesment.Api.SurveyResponses.Bases
|
||||
{
|
||||
public class ServiceProviderBase
|
||||
{
|
||||
protected readonly IConfiguration configuration;
|
||||
protected readonly HttpClient httpClient;
|
||||
protected private readonly ILogger<ServiceProviderBase> logger;
|
||||
protected string ressource;
|
||||
protected string urlBase;
|
||||
|
||||
|
||||
public ServiceProviderBase(IConfiguration configuration, HttpClient httpClient, ILogger<ServiceProviderBase> logger, string ressource, string urlBase)
|
||||
{
|
||||
this.configuration = configuration;
|
||||
this.httpClient = httpClient;
|
||||
this.logger = logger;
|
||||
this.ressource = ressource;
|
||||
this.urlBase = urlBase;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,157 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Interfaces;
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Controllers
|
||||
{
|
||||
[Route("api")]
|
||||
[ApiController]
|
||||
public class SurveyResponsesController : ControllerBase
|
||||
{
|
||||
private readonly ISurveysResponse surveyResponseProvider;
|
||||
|
||||
public SurveyResponsesController(ISurveysResponse surveyResponseProvider)
|
||||
{
|
||||
this.surveyResponseProvider = surveyResponseProvider;
|
||||
}
|
||||
|
||||
[HttpGet("SurveyResponses")]
|
||||
public async Task<ActionResult> GetSurveyResponsesAsync()
|
||||
{
|
||||
var result = await this.surveyResponseProvider.GetSurveyResponsesAsync();
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.surveyResponses);
|
||||
}
|
||||
|
||||
if (result.ErrorMessage == "No Data Found")
|
||||
return NoContent();
|
||||
|
||||
return BadRequest(result.ErrorMessage);
|
||||
}
|
||||
|
||||
[HttpGet("SurveyResponses/{surveyId}")]
|
||||
public async Task<ActionResult> GetSurveyResponsesAsync(int surveyId)
|
||||
{
|
||||
var result = await this.surveyResponseProvider.GetSurveyResponsesBySurveyAsync(surveyId);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.SurveyResponses);
|
||||
}
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpGet("Responses/{surveyId}/{locationId}")]
|
||||
public async Task<ActionResult> GetSurveyResponsesBySurveyAndLocationAsync(int surveyId, string locationId)
|
||||
{
|
||||
var result = await this.surveyResponseProvider.GetSurveyResponsesBySurveyAndLocationAsync(surveyId, locationId);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.SurveyResponses);
|
||||
}
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpGet("ResponsesByAnswer/{surveyId}/{questionId}/{answer}")]
|
||||
public async Task<ActionResult> GetSurveyResponsesByAnswerAsyncAsync(int surveyId, int questionId, string answer)
|
||||
{
|
||||
var result = await surveyResponseProvider.GetResponsesByAnswerAsync(surveyId, questionId, answer);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.SurveyResponses);
|
||||
}
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpGet("AnswersByRegion/{surveyId}")]
|
||||
public async Task<ActionResult> GetAnswersByRegionAsync(int surveyId)
|
||||
{
|
||||
var result = await this.surveyResponseProvider.GetAnswersByRegionAsync(surveyId);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.Answers);
|
||||
}
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpGet("AnswersByMaintenanceCenter/{surveyId}")]
|
||||
public async Task<ActionResult> GetAnswersByMaintenaceCentersync(int surveyId)
|
||||
{
|
||||
var result = await this.surveyResponseProvider.GetSurveyResponsesByMaintenanceCenterAsync(surveyId);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.SurveyResponses);
|
||||
}
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpGet("SurveyResponse/{responseId}")]
|
||||
public async Task<ActionResult> GetSurveyResponseByIdAsync(int responseId)
|
||||
{
|
||||
var result = await this.surveyResponseProvider.GetSurveyResponseByIdAsync(responseId);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.SurveyResponse);
|
||||
}
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
|
||||
[HttpPost("SurveyResponses")]
|
||||
public async Task<ActionResult> PostSurveysAsync(Models.SurveyResponse surveyResponse)
|
||||
{
|
||||
var result = await this.surveyResponseProvider.PostSurveyResponseAsync(surveyResponse);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.SurveyResponse);
|
||||
}
|
||||
return BadRequest(result.ErrorMessage);
|
||||
}
|
||||
|
||||
[HttpPut("SurveyResponses/{Id}")]
|
||||
public async Task<ActionResult> PutSurveyResponseAsync(int Id, Models.SurveyResponse surveyResponse)
|
||||
{
|
||||
var result = await this.surveyResponseProvider.PutSurveyResponseAsync(Id, surveyResponse);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.SurveyResponse);
|
||||
}
|
||||
if (result.ErrorMessage == "Not Found")
|
||||
return NotFound(result.ErrorMessage);
|
||||
|
||||
return BadRequest(result.ErrorMessage);
|
||||
}
|
||||
|
||||
[HttpDelete("SurveyResponses/{Id}")]
|
||||
public async Task<ActionResult> DeleteSurveyResponseAsync(int Id)
|
||||
{
|
||||
var result = await this.surveyResponseProvider.DeleteSurveyResponseAsync(Id);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.SurveyResponse);
|
||||
}
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
[HttpPost("SurveyResponses/Answers")]
|
||||
public async Task<ActionResult> PostSurveyAnswersAsync(AnswerRequest answers)
|
||||
{
|
||||
/* var result = await this.surveyResponseProvider.PostSurveyAnswersAsync(surveyAnswers);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.SurveyResponse);
|
||||
}
|
||||
return BadRequest(result.ErrorMessage);*/
|
||||
var result = await this.surveyResponseProvider.PostSurveyAnswersAsync(answers);
|
||||
|
||||
if (result.IsSuccess)
|
||||
return Ok(result.SurveyResponse);
|
||||
else
|
||||
return BadRequest(result.ErrorMessage);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.5" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="7.0.5" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,30 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Db
|
||||
{
|
||||
public class SurveyResponse
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
|
||||
[ForeignKey("Survey")]
|
||||
public int SurveyId { get; set; }
|
||||
|
||||
[StringLength(4)]
|
||||
[ForeignKey("Location")]
|
||||
public string LocationId { get; set; }
|
||||
|
||||
[StringLength(6)]
|
||||
[ForeignKey("Employee")]
|
||||
public string EmployeeId { get; set; }
|
||||
|
||||
//public DateTime? CreatedDate { get; set; }
|
||||
|
||||
//[StringLength(50)]
|
||||
// public string ClientDevice { get; set; }
|
||||
|
||||
// [StringLength(250)]
|
||||
//public string KeyAnswerResult { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Db
|
||||
{
|
||||
public class SurveyResponseDbContext:DbContext
|
||||
{
|
||||
public DbSet<Db.SurveyResponse> SurveyResponses { get; set; }
|
||||
|
||||
public SurveyResponseDbContext(DbContextOptions options) : base(options)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Interfaces
|
||||
{
|
||||
public interface IAnswerServiceProvider
|
||||
{
|
||||
Task<List<Answer>> getAnswersAsync();
|
||||
Task<List<Models.Answer>> GetAnswersByResponseIdAsync(int responseId);
|
||||
|
||||
Task<Models.Answer> PostAnswersAsync(Models.Answer answer);
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Interfaces
|
||||
{
|
||||
public interface IAttachmentServiceProvider
|
||||
{
|
||||
Task<List<Attachment>> getAttachmentsAsync();
|
||||
Task<IEnumerable<Attachment>> PostAttachmentsAsync(Models.AttachmentInfo attachmentInfo);
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Interfaces
|
||||
{
|
||||
public interface IEmployeeServiceProvider
|
||||
{
|
||||
Task<List<Employee>> getEmployeesAsync();
|
||||
Task<Employee> getEmployeeAsync(string employeeID);
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Interfaces
|
||||
{
|
||||
public interface ILocationServiceProvider
|
||||
{
|
||||
Task<List<Location>> getLocationsAsync();
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Interfaces
|
||||
{
|
||||
public interface IQuestionServiceProvider
|
||||
{
|
||||
Task<List<Question>> getQuestionsAsync();
|
||||
Task<List<SurveyQuestions>> getSurveyQuestionsAsync(int surveyId);
|
||||
Task<Question> getQuestionsAsync(int questionId);
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Interfaces
|
||||
{
|
||||
public interface IRegionServiceProvider
|
||||
{
|
||||
Task<List<Region>> getRegionsAsync();
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Interfaces
|
||||
{
|
||||
public interface ISurveyServiceProvider
|
||||
{
|
||||
Task<List<Survey>> getSurveysAsync();
|
||||
Task<Survey> getSurveyAsync(int surveyId);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Interfaces
|
||||
{
|
||||
public interface ISurveysResponse
|
||||
{
|
||||
Task<(bool IsSuccess, dynamic Answers, string ErrorMessage)> GetAnswersByRegionAsync(int surveyId);
|
||||
Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PostSurveyResponseAsync(Models.SurveyResponse surveyResponse);
|
||||
// Task<(bool IsSuccess,dynamic surveyResponses, string ErrorMessage)> GetSurveyResponseAsync(int responseId);
|
||||
Task<(bool IsSuccess, dynamic surveyResponses, string ErrorMessage)> GetSurveyResponsesAsync();
|
||||
Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PutSurveyResponseAsync(int Id, Models.SurveyResponse surveyResponse);
|
||||
Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> DeleteSurveyResponseAsync(int Id);
|
||||
Task<(bool IsSuccess, dynamic SurveyResponse, string ErrorMessage)> GetSurveyResponseByIdAsync(int responseId);
|
||||
Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesBySurveyAsync(int surveyId);
|
||||
Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesBySurveyAndLocationAsync(int surveyId, string location);
|
||||
Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesByMaintenanceCenterAsync(int surveyId);
|
||||
Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetResponsesByAnswerAsync(int surveyId, int questionId, string answer);
|
||||
|
||||
Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PostSurveyAnswersAsync(Models.AnswerRequest answers);
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Models
|
||||
{
|
||||
public class AggregateAnswer
|
||||
{
|
||||
public string? Answer { get; set; }
|
||||
public int Counter { get; set; }
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Models
|
||||
{
|
||||
public class AggregateResult
|
||||
{
|
||||
public string RegionId { get; set; }
|
||||
public AggregateAnswer Answers { get; set; }
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Models
|
||||
{
|
||||
public class Answer
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int QuestionId { get; set; }
|
||||
public string? LocationId { get; set; }
|
||||
public string AnswerText { get; set; }
|
||||
public string? Comment { get; set; }
|
||||
public int SurveyResponseId { get; set; }
|
||||
public string? RegionId { get; set; }
|
||||
// public string? Name { get; set; }
|
||||
// public string? Abbreviation { get; set; }
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Models
|
||||
{
|
||||
public class AnswerData
|
||||
{
|
||||
public string RegionId { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Abbreviation { get; set; }
|
||||
public List<AggregateAnswer> Answers { get; set; }
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
namespace DamageAssesment.Api.SurveyResponses.Models
|
||||
{
|
||||
public class AnswerRequest
|
||||
{
|
||||
public int SurveyId { get; set; }
|
||||
public string LocationId { get; set; }
|
||||
public string EmployeeId { get; set; }
|
||||
public List<QuestionRequest> Answers { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Models
|
||||
{
|
||||
public class Attachment
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public string URI { get; set; }
|
||||
public int ResponseId { get; set; }
|
||||
|
||||
public int? AnswerId { get; set; }
|
||||
public bool IsDeleted { get; set; }
|
||||
|
||||
public Attachment(int answerId, string uri)
|
||||
{
|
||||
this.AnswerId = answerId;
|
||||
this.URI = uri;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
namespace DamageAssesment.Api.SurveyResponses.Models
|
||||
{
|
||||
public class AttachmentInfo
|
||||
{
|
||||
public int ResponseId { get; set; }
|
||||
public List<AnswerInfo> Answers { get; set; }
|
||||
}
|
||||
public class AnswerInfo
|
||||
{
|
||||
public int AnswerId { get; set; }
|
||||
public List<FileModel> postedFiles { get; set; }
|
||||
}
|
||||
public class FileModel
|
||||
{
|
||||
public int? AttachmentId { get; set; }
|
||||
public string? FileName { get; set; }
|
||||
public string? FileContent { get; set; }
|
||||
public string? FileExtension { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Models
|
||||
{
|
||||
public class Employee
|
||||
{
|
||||
public string Id { get; set; }
|
||||
|
||||
[StringLength(50)]
|
||||
public string Name { get; set; }
|
||||
|
||||
public DateTime BirthDate { get; set; }
|
||||
|
||||
[StringLength(50)]
|
||||
public string OfficePhoneNumber { get; set; }
|
||||
|
||||
[StringLength(50)]
|
||||
public string Email { get; set; }
|
||||
public bool IsActive { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
namespace DamageAssesment.Api.SurveyResponses.Models
|
||||
{
|
||||
public class Location
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string RegionId { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string MaintenanceCenter { get; set; }
|
||||
public string SchoolType { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Models
|
||||
{
|
||||
public class Question
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public List<QuestionsTranslation> Questions { get; set; }
|
||||
|
||||
//public int QuestionTypeID { get; set; }
|
||||
|
||||
public string TypeText { get; set; } = string.Empty;
|
||||
|
||||
public int QuestionNumber { get; set; }
|
||||
public bool IsRequired { get; set; }
|
||||
public bool Comment { get; set; }
|
||||
|
||||
public bool Key { get; set; }
|
||||
public int? SurveyId { get; set; }
|
||||
public string QuestionGroup { get; set; }
|
||||
public int CategoryId { get; set; }
|
||||
// public int? Survey_SurveyID { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
namespace DamageAssesment.Api.SurveyResponses.Models
|
||||
{
|
||||
public class QuestionRequest
|
||||
{
|
||||
public int QuestionId { get; set; }
|
||||
public string AnswerText { get; set; }
|
||||
public string Comment { get; set; }
|
||||
public List<FileModel> PostedFiles { get; set; } = new List<FileModel>();
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
namespace DamageAssesment.Api.SurveyResponses.Models
|
||||
{
|
||||
public class QuestionsTranslation
|
||||
{
|
||||
public string QuestionText { get; set; }
|
||||
public string Language { get; set; } = "En";
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Models
|
||||
{
|
||||
public class Region
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Abbreviation { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Models
|
||||
{
|
||||
public class ResultData
|
||||
{
|
||||
public List<AnswerData> Regions { get; set; }
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Models
|
||||
{
|
||||
public class Survey
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
|
||||
[StringLength(100)]
|
||||
public string Title { get; set; }
|
||||
|
||||
//[StringLength(1000)]
|
||||
//public string Description { get; set; }
|
||||
|
||||
public bool IsEnabled { get; set; }
|
||||
|
||||
public DateTime? StartDate { get; set; }
|
||||
|
||||
public DateTime? EndDate { get; set; }
|
||||
|
||||
//public DateTime CreatedDate { get; set; }
|
||||
|
||||
//[StringLength(6)]
|
||||
//public string EmployeeID { get; set; }
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
namespace DamageAssesment.Api.SurveyResponses.Models
|
||||
{
|
||||
public class SurveyQuestions
|
||||
{
|
||||
public int CategoryId { get; set; }
|
||||
public string CategoryName { get; set; }
|
||||
public string CategoryImage { get; set; }
|
||||
public List<Question> Questions { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Models
|
||||
{
|
||||
public class SurveyResponse
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
|
||||
[ForeignKey("Survey")]
|
||||
public int SurveyId { get; set; }
|
||||
|
||||
[ForeignKey("Location")]
|
||||
public string LocationId { get; set; }
|
||||
|
||||
[ForeignKey("Employee")]
|
||||
public string EmployeeId { get; set; }
|
||||
|
||||
//public DateTime? CreatedDate { get; set; }
|
||||
|
||||
//[StringLength(50)]
|
||||
//public string ClientDevice { get; set; }
|
||||
|
||||
//[StringLength(250)]
|
||||
//public string KeyAnswerResult { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
namespace DamageAssesment.Api.SurveyResponses.Profiles
|
||||
{
|
||||
public class SurveyResponsesProvider : AutoMapper.Profile
|
||||
{
|
||||
public SurveyResponsesProvider()
|
||||
{
|
||||
CreateMap<Models.SurveyResponse, Db.SurveyResponse>();
|
||||
CreateMap<Db.SurveyResponse, Models.SurveyResponse>();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Db;
|
||||
using DamageAssesment.Api.SurveyResponses.Interfaces;
|
||||
using DamageAssesment.Api.SurveyResponses.Providers;
|
||||
using Microsoft.AspNetCore.DataProtection.XmlEncryption;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Polly;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
const int maxApiCallRetries = 3;
|
||||
const int intervalToRetry = 2; //2 seconds
|
||||
const int maxRetryForCircuitBraker = 5;
|
||||
const int intervalForCircuitBraker = 5; //5 seconds
|
||||
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
|
||||
builder.Services.AddScoped<ISurveysResponse, SurveyResponsesProvider>();
|
||||
//builder.Services.AddScoped<ILogger, Logger>();
|
||||
|
||||
builder.Services.AddHttpClient<IAnswerServiceProvider, AnswerServiceProvider>().
|
||||
AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(maxApiCallRetries, _ => TimeSpan.FromSeconds(intervalToRetry))).
|
||||
AddTransientHttpErrorPolicy(policy => policy.CircuitBreakerAsync(maxRetryForCircuitBraker, TimeSpan.FromSeconds(intervalForCircuitBraker) ));
|
||||
|
||||
builder.Services.AddHttpClient<ILocationServiceProvider, LocationServiceProvider>().
|
||||
AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(maxApiCallRetries, _ => TimeSpan.FromSeconds(intervalToRetry))).
|
||||
AddTransientHttpErrorPolicy(policy => policy.CircuitBreakerAsync(maxRetryForCircuitBraker, TimeSpan.FromSeconds(intervalForCircuitBraker)));
|
||||
|
||||
builder.Services.AddHttpClient<IRegionServiceProvider, RegionServiceProvider>().
|
||||
AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(maxApiCallRetries, _ => TimeSpan.FromSeconds(intervalToRetry))).
|
||||
AddTransientHttpErrorPolicy(policy => policy.CircuitBreakerAsync(maxRetryForCircuitBraker, TimeSpan.FromSeconds(intervalForCircuitBraker)));
|
||||
|
||||
builder.Services.AddHttpClient<IQuestionServiceProvider, QuestionServiceProvider>().
|
||||
AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(maxApiCallRetries, _ => TimeSpan.FromSeconds(intervalToRetry))).
|
||||
AddTransientHttpErrorPolicy(policy => policy.CircuitBreakerAsync(maxRetryForCircuitBraker, TimeSpan.FromSeconds(intervalForCircuitBraker)));
|
||||
|
||||
builder.Services.AddHttpClient<IEmployeeServiceProvider, EmployeeServiceProvider>().
|
||||
AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(maxApiCallRetries, _ => TimeSpan.FromSeconds(intervalToRetry))).
|
||||
AddTransientHttpErrorPolicy(policy => policy.CircuitBreakerAsync(maxRetryForCircuitBraker, TimeSpan.FromSeconds(intervalForCircuitBraker)));
|
||||
|
||||
builder.Services.AddHttpClient<IAttachmentServiceProvider, AttachmentServiceProvider>().
|
||||
AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(maxApiCallRetries, _ => TimeSpan.FromSeconds(intervalToRetry))).
|
||||
AddTransientHttpErrorPolicy(policy => policy.CircuitBreakerAsync(maxRetryForCircuitBraker, TimeSpan.FromSeconds(intervalForCircuitBraker)));
|
||||
|
||||
builder.Services.AddHttpClient<ISurveyServiceProvider, SurveyServiceProvider>().
|
||||
AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(maxApiCallRetries, _ => TimeSpan.FromSeconds(intervalToRetry))).
|
||||
AddTransientHttpErrorPolicy(policy => policy.CircuitBreakerAsync(maxRetryForCircuitBraker, TimeSpan.FromSeconds(intervalForCircuitBraker)));
|
||||
|
||||
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Services.AddDbContext<SurveyResponseDbContext>(option =>
|
||||
{
|
||||
option.UseInMemoryDatabase("SurveyResponses");
|
||||
});
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
@ -0,0 +1,31 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:58856",
|
||||
"sslPort": 0
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"DamageAssesment.Api.SurveyResponses": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5104",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
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;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Providers
|
||||
{
|
||||
public class AnswerServiceProvider : ServiceProviderBase, IAnswerServiceProvider
|
||||
{
|
||||
|
||||
public AnswerServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger<AnswerServiceProvider> logger, IRegionServiceProvider regionServiceProvider, ILocationServiceProvider locationServiceProvider) : base(configuration, httpClient, logger, "/api/Answers", configuration.GetValue<string>("EndPointSettings:AnswerUrlBase"))
|
||||
{
|
||||
}
|
||||
public async Task<List<Answer>> getAnswersAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
httpClient.BaseAddress = new Uri(urlBase);
|
||||
var response = await httpClient.GetAsync(ressource);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
var answers = JsonConvert.DeserializeObject<List<Answer>>(responseString);
|
||||
|
||||
if (answers == null || !answers.Any())
|
||||
return null;
|
||||
else return answers;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: AnswerServiceProvider.getAnswersAsync()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<Answer>> GetAnswersByResponseIdAsync(int responseId)
|
||||
{
|
||||
try
|
||||
{
|
||||
httpClient.BaseAddress = new Uri(urlBase);
|
||||
|
||||
var response = await httpClient.GetAsync("/api/AnswersByResponse/"+ responseId);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
var answers = JsonConvert.DeserializeObject<List<Answer>>(responseString);
|
||||
|
||||
if (answers == null || !answers.Any())
|
||||
return null;
|
||||
else return answers;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: AnswerServiceProvider.GetAnswersByResponseId()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Answer> PostAnswersAsync(Answer answer)
|
||||
{
|
||||
try
|
||||
{
|
||||
httpClient.BaseAddress = new Uri(urlBase);
|
||||
var jsonObject = JsonConvert.SerializeObject(answer);
|
||||
var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
|
||||
var response = await httpClient.PostAsync(ressource,content);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
var answers = JsonConvert.DeserializeObject<Answer>(responseString);
|
||||
|
||||
if (answers == null) {
|
||||
logger?.LogError($"Answer 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,67 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Bases;
|
||||
using DamageAssesment.Api.SurveyResponses.Interfaces;
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.Http;
|
||||
using System.Runtime.Intrinsics.Arm;
|
||||
using System.Text;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Providers
|
||||
{
|
||||
public class AttachmentServiceProvider : ServiceProviderBase, IAttachmentServiceProvider
|
||||
{
|
||||
public AttachmentServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger<AttachmentServiceProvider> logger) : base(configuration, httpClient, logger, "/api/Attachments", configuration.GetValue<string>("EndPointSettings:AttachmentUrlBase"))
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<List<Attachment>> getAttachmentsAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
httpClient.BaseAddress = new Uri(urlBase);
|
||||
var response = await httpClient.GetAsync(ressource);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
var attachments = JsonConvert.DeserializeObject<List<Attachment>>(responseString);
|
||||
|
||||
if (attachments == null || !attachments.Any())
|
||||
return null;
|
||||
else return attachments;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: AttachmentServiceProvider.getAttachmentsAsync()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Attachment>> PostAttachmentsAsync(AttachmentInfo attachmentInfo)
|
||||
{
|
||||
try
|
||||
{
|
||||
httpClient.BaseAddress = new Uri(urlBase);
|
||||
var jsonObject = JsonConvert.SerializeObject(attachmentInfo);
|
||||
var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
|
||||
var response = await httpClient.PostAsync(ressource, content);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
var attachments = JsonConvert.DeserializeObject<IEnumerable<Attachment>>(responseString);
|
||||
|
||||
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,58 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Bases;
|
||||
using DamageAssesment.Api.SurveyResponses.Interfaces;
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
using Newtonsoft.Json;
|
||||
using System.Reflection;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Providers
|
||||
{
|
||||
public class EmployeeServiceProvider :ServiceProviderBase, IEmployeeServiceProvider
|
||||
{
|
||||
public EmployeeServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger<EmployeeServiceProvider> logger) : base(configuration, httpClient, logger, "/api/Employees", configuration.GetValue<string>("EndPointSettings:EmployeeUrlBase"))
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<List<Employee>> getEmployeesAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
httpClient.BaseAddress = new Uri(urlBase);
|
||||
var response = await httpClient.GetAsync(ressource);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
var employees = JsonConvert.DeserializeObject<List<Employee>>(responseString);
|
||||
|
||||
if (employees == null || !employees.Any())
|
||||
return null;
|
||||
else return employees;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: EmployeeServiceProvider.getEmployeesAsync()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Employee> getEmployeeAsync(string employeeID)
|
||||
{
|
||||
try
|
||||
{
|
||||
httpClient.BaseAddress = new Uri(urlBase);
|
||||
//ressource = ressource + "/" + employeeID;
|
||||
var response = await httpClient.GetAsync("/api/Employees/"+ employeeID);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
var employee = JsonConvert.DeserializeObject<Employee>(responseString);
|
||||
|
||||
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,35 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Bases;
|
||||
using DamageAssesment.Api.SurveyResponses.Interfaces;
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Providers
|
||||
{
|
||||
public class LocationServiceProvider :ServiceProviderBase, ILocationServiceProvider
|
||||
{
|
||||
public LocationServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger<LocationServiceProvider> logger) : base(configuration, httpClient, logger, "/api/Locations", configuration.GetValue<string>("EndPointSettings:LocationUrlBase"))
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<List<Location>> getLocationsAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
httpClient.BaseAddress = new Uri(urlBase);
|
||||
var response = await httpClient.GetAsync(ressource);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
var locations = JsonConvert.DeserializeObject<List<Location>>(responseString);
|
||||
|
||||
if (locations == null || !locations.Any())
|
||||
return null;
|
||||
else return locations;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: LocationServiceProvider.getLocationsAsync()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Bases;
|
||||
using DamageAssesment.Api.SurveyResponses.Interfaces;
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Providers
|
||||
{
|
||||
public class QuestionServiceProvider : ServiceProviderBase, IQuestionServiceProvider
|
||||
{
|
||||
public QuestionServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger<QuestionServiceProvider> logger) : base(configuration, httpClient, logger, "/api/Questions", configuration.GetValue<string>("EndPointSettings:QuestionUrlBase"))
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<List<Question>> getQuestionsAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
httpClient.BaseAddress = new Uri(urlBase);
|
||||
var response = await httpClient.GetAsync(ressource);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
var questions = JsonConvert.DeserializeObject<List<Question>>(responseString);
|
||||
|
||||
if (questions == null || !questions.Any())
|
||||
return null;
|
||||
else return questions;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: QuestionServiceProvider.getQuestionsAsync()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<SurveyQuestions>> getSurveyQuestionsAsync(int surveyId)
|
||||
{
|
||||
try
|
||||
{
|
||||
httpClient.BaseAddress = new Uri(urlBase);
|
||||
var response = await httpClient.GetAsync("/api/GetSurveyQuestions/" + surveyId);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
var questions = JsonConvert.DeserializeObject<List<SurveyQuestions>>(responseString);
|
||||
|
||||
if (questions == null || !questions.Any())
|
||||
return null;
|
||||
else return questions;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: QuestionServiceProvider.getSurveyQuestionsAsync()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<Question> getQuestionsAsync(int questionId)
|
||||
{
|
||||
try
|
||||
{
|
||||
httpClient.BaseAddress = new Uri(urlBase);
|
||||
var response = await httpClient.GetAsync("/api/Questions/" + questionId);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
var question = JsonConvert.DeserializeObject<Question>(responseString);
|
||||
|
||||
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,34 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Bases;
|
||||
using DamageAssesment.Api.SurveyResponses.Interfaces;
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Providers
|
||||
{
|
||||
public class RegionServiceProvider : ServiceProviderBase, IRegionServiceProvider
|
||||
{
|
||||
public RegionServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger<RegionServiceProvider> logger) : base(configuration, httpClient, logger, "/api/Regions", configuration.GetValue<string>("EndPointSettings:LocationUrlBase"))
|
||||
{
|
||||
}
|
||||
public async Task<List<Region>> getRegionsAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
httpClient.BaseAddress = new Uri(urlBase);
|
||||
var response = await httpClient.GetAsync(ressource);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
var regions = JsonConvert.DeserializeObject<List<Region>>(responseString);
|
||||
|
||||
if (regions == null || !regions.Any())
|
||||
return null;
|
||||
else return regions;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: RegionServiceProvider.getRegionsAsync()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,795 @@
|
||||
using AutoMapper;
|
||||
using DamageAssesment.Api.SurveyResponses.Db;
|
||||
using DamageAssesment.Api.SurveyResponses.Interfaces;
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Internal;
|
||||
using System.Diagnostics;
|
||||
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Providers
|
||||
{
|
||||
public class SurveyResponsesProvider : ISurveysResponse
|
||||
{
|
||||
private readonly SurveyResponseDbContext surveyResponseDbContext;
|
||||
private readonly ILogger<SurveyResponsesProvider> logger;
|
||||
private readonly IAnswerServiceProvider answerServiceProvider;
|
||||
private readonly IRegionServiceProvider regionServiceProvider;
|
||||
private readonly ILocationServiceProvider locationServiceProvider;
|
||||
private readonly IEmployeeServiceProvider employeeServiceProvider;
|
||||
private readonly IAttachmentServiceProvider attachmentServiceProvider;
|
||||
private readonly IQuestionServiceProvider questionServiceProvider;
|
||||
private readonly ISurveyServiceProvider surveyServiceProvider;
|
||||
private readonly IMapper mapper;
|
||||
|
||||
public SurveyResponsesProvider(SurveyResponseDbContext surveyResponseDbContext, ILogger<SurveyResponsesProvider> logger, IAnswerServiceProvider answerServiceProvider, IRegionServiceProvider regionServiceProvider, ILocationServiceProvider locationServiceProvider, IEmployeeServiceProvider employeeServiceProvider, IAttachmentServiceProvider attachmentServiceProvider, IQuestionServiceProvider questionServiceProvider, ISurveyServiceProvider surveyServiceProvider, IMapper mapper)
|
||||
{
|
||||
this.surveyResponseDbContext = surveyResponseDbContext;
|
||||
this.logger = logger;
|
||||
this.answerServiceProvider = answerServiceProvider;
|
||||
this.regionServiceProvider = regionServiceProvider;
|
||||
this.locationServiceProvider = locationServiceProvider;
|
||||
this.employeeServiceProvider = employeeServiceProvider;
|
||||
this.attachmentServiceProvider = attachmentServiceProvider;
|
||||
this.questionServiceProvider = questionServiceProvider;
|
||||
this.surveyServiceProvider = surveyServiceProvider;
|
||||
this.mapper = mapper;
|
||||
|
||||
seedData();
|
||||
}
|
||||
|
||||
private void seedData()
|
||||
{
|
||||
if (!surveyResponseDbContext.SurveyResponses.Any())
|
||||
{
|
||||
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 1, SurveyId = 1, EmployeeId = "Emp1", LocationId = "Loc1" });
|
||||
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 2, SurveyId = 1, EmployeeId = "Emp2", LocationId = "Loc2" });
|
||||
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 3, SurveyId = 3, EmployeeId = "Emp4", LocationId = "Loc1" });
|
||||
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 4, SurveyId = 4, EmployeeId = "Emp1", LocationId = "Loc2" });
|
||||
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 5, SurveyId = 1, EmployeeId = "Emp3", LocationId = "Loc3" });
|
||||
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 6, SurveyId = 1, EmployeeId = "Emp4", LocationId = "Loc2" });
|
||||
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { Id = 7, SurveyId = 1, EmployeeId = "Emp4", LocationId = "Loc3" });
|
||||
surveyResponseDbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<(bool IsSuccess, dynamic Answers, string ErrorMessage)> GetAnswersByRegionAsync(int surveyId)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger?.LogInformation("Querying to get SurveyResponse object from DB");
|
||||
var listSurveyResponse = surveyResponseDbContext.SurveyResponses.Where(s => s.SurveyId == surveyId);
|
||||
|
||||
if (listSurveyResponse.Any())
|
||||
{
|
||||
var answers = await getAnswersByRegionAndSurveyIdAsync(listSurveyResponse);
|
||||
return (true, answers, "Request Successful.");
|
||||
}
|
||||
else
|
||||
{
|
||||
return (false, null, "Not found");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<(bool IsSuccess, dynamic SurveyResponse, string ErrorMessage)> GetSurveyResponseByIdAsync(int responseId)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger?.LogInformation("Querying to get SurveyResponse object from DB");
|
||||
var surveyResponse = surveyResponseDbContext.SurveyResponses.Where(s => s.Id == responseId).SingleOrDefault();
|
||||
|
||||
if (surveyResponse != null)
|
||||
{
|
||||
var answers = await getSurveyResponseByResponseIdAsync(surveyResponse);
|
||||
|
||||
if (answers != null)
|
||||
return (true, answers, "Request Successful.");
|
||||
else
|
||||
{
|
||||
answers = new { };
|
||||
return (true, answers, "Empty object returned");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return (false, null, "Not found");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public async Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesBySurveyAsync(int surveyId)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger?.LogInformation("Querying to get Survey object from microservice");
|
||||
var survey = await surveyServiceProvider.getSurveyAsync(surveyId);
|
||||
|
||||
if (survey != null)
|
||||
{
|
||||
var answers = await getSurveyResponsesBySurveyIdAsync(surveyId);
|
||||
|
||||
if (answers != null)
|
||||
return (true, answers, "Request Successful.");
|
||||
else
|
||||
{
|
||||
answers = new List<Models.SurveyResponse>();
|
||||
return (true, answers, "Empty object returned");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return (false, null, "Not found");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesBySurveyAndLocationAsync(int surveyId, string locationId)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger?.LogInformation("Querying to get Survey object from microservice");
|
||||
var survey = await surveyServiceProvider.getSurveyAsync(surveyId);
|
||||
|
||||
if (survey != null)
|
||||
{
|
||||
var answers = await getSurveyResponsesBySurveyIdLocationIdAsync(surveyId, locationId);
|
||||
|
||||
if (answers != null)
|
||||
return (true, answers, "Request Successful.");
|
||||
else
|
||||
{
|
||||
answers = new List<Models.SurveyResponse>();
|
||||
return (true, answers, "Empty object returned");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return (false, null, "Not found");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesByMaintenanceCenterAsync(int surveyId)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger?.LogInformation("Querying to get Survey object from microservice");
|
||||
var survey = await surveyServiceProvider.getSurveyAsync(surveyId);
|
||||
|
||||
if (survey != null)
|
||||
{
|
||||
var answers = await getResultsByMaintenanceCenterAsync(surveyId);
|
||||
|
||||
if (answers != null)
|
||||
return (true, answers, "Request Successful.");
|
||||
else
|
||||
{
|
||||
answers = new List<Models.SurveyResponse>();
|
||||
return (true, answers, "Empty object returned");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return (false, null, "Not found");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetResponsesByAnswerAsync(int surveyId, int questionId, string answer)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger?.LogInformation("Querying to get Survey object from microservice");
|
||||
var survey = await surveyServiceProvider.getSurveyAsync(surveyId);
|
||||
var question = await questionServiceProvider.getQuestionsAsync(questionId);
|
||||
bool IsCorrectAnswer = answer.ToLower().Equals("yes") || answer.ToLower().Equals("no") ? true : false;
|
||||
|
||||
|
||||
if (survey != null && question != null && IsCorrectAnswer)
|
||||
{
|
||||
var answers = await getSurveyResponsesByAnswerAsync(survey, question, answer);
|
||||
|
||||
if (answers != null)
|
||||
return (true, answers, "Request Successful.");
|
||||
else
|
||||
{
|
||||
answers = new List<Models.SurveyResponse>();
|
||||
return (true, answers, "Empty object returned");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return (false, null, "Not found");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<(bool IsSuccess, dynamic surveyResponses, string ErrorMessage)> GetSurveyResponsesAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var answers = await getAllSurveyResponsesAsync();
|
||||
|
||||
if (answers != null)
|
||||
return (true, answers, "Request Successful.");
|
||||
else
|
||||
{
|
||||
answers = new List<Models.SurveyResponse>();
|
||||
return (true, answers, "Empty object returned");
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PostSurveyResponseAsync(Models.SurveyResponse surveyResponse)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (surveyResponse != null)
|
||||
{
|
||||
var surveyResponses = await surveyResponseDbContext.SurveyResponses.ToListAsync();
|
||||
surveyResponse.Id = surveyResponses.Count + 1;
|
||||
surveyResponseDbContext.SurveyResponses.Add(mapper.Map<Models.SurveyResponse, Db.SurveyResponse>(surveyResponse));
|
||||
surveyResponseDbContext.SaveChanges();
|
||||
return (true, surveyResponse, "Request Successful");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger?.LogInformation($"SurveyResponseID={surveyResponse.Id} cannot be added");
|
||||
return (false, null, "Survey cannot be added");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PutSurveyResponseAsync(int Id, Models.SurveyResponse SurveyResponse)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (SurveyResponse != null)
|
||||
{
|
||||
var _SurveyResponse = await surveyResponseDbContext.SurveyResponses.Where(s => s.Id == Id).FirstOrDefaultAsync();
|
||||
|
||||
if (_SurveyResponse != null)
|
||||
{
|
||||
_SurveyResponse.SurveyId = SurveyResponse.SurveyId;
|
||||
_SurveyResponse.EmployeeId = SurveyResponse.EmployeeId;
|
||||
_SurveyResponse.LocationId = SurveyResponse.LocationId;
|
||||
surveyResponseDbContext.SaveChanges();
|
||||
return (true, mapper.Map<Db.SurveyResponse, Models.SurveyResponse>(_SurveyResponse), "Successful");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger?.LogInformation($"SurveyReponseId = {Id} Not found");
|
||||
return (false, null, "Not Found");
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
logger?.LogInformation($"SurveyReponseId = {Id} Bad Request");
|
||||
return (false, null, "Bad request");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> DeleteSurveyResponseAsync(int Id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var _SurveyResponse = await surveyResponseDbContext.SurveyResponses.Where(s => s.Id == Id).FirstOrDefaultAsync();
|
||||
|
||||
if (_SurveyResponse != null)
|
||||
{
|
||||
surveyResponseDbContext.Remove(_SurveyResponse);
|
||||
surveyResponseDbContext.SaveChanges();
|
||||
return (true, mapper.Map<Db.SurveyResponse, Models.SurveyResponse>(_SurveyResponse), "Successful");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger?.LogInformation($"SurveyReponseId = {Id} Not found");
|
||||
return (false, null, "Not Found");
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
//Method to get Answers by region with surveyId as input parameter
|
||||
private async Task<dynamic> getAnswersByRegionAndSurveyIdAsync(IQueryable<Db.SurveyResponse> surveyResponses)
|
||||
{
|
||||
try
|
||||
{
|
||||
var answersList = await answerServiceProvider.getAnswersAsync();
|
||||
if (answersList == null || !answersList.Any())
|
||||
return null;
|
||||
|
||||
//get all the answers for the particular survey
|
||||
var surveyAnswers = answersList.Join(
|
||||
surveyResponses,
|
||||
answer => answer.SurveyResponseId,
|
||||
surveyResponse => surveyResponse.Id,
|
||||
(answer, surveyResponse) => new
|
||||
Answer
|
||||
{
|
||||
Id = answer.Id,
|
||||
QuestionId = answer.QuestionId,
|
||||
AnswerText = answer.AnswerText,
|
||||
Comment = answer.Comment,
|
||||
LocationId = surveyResponse.LocationId,
|
||||
SurveyResponseId = surveyResponse.Id
|
||||
});
|
||||
|
||||
if (surveyAnswers == null || !surveyAnswers.Any())
|
||||
return null;
|
||||
|
||||
var regions = await regionServiceProvider.getRegionsAsync();
|
||||
var locations = await locationServiceProvider.getLocationsAsync();
|
||||
|
||||
if (regions == null || !regions.Any() || locations == null || !locations.Any())
|
||||
return null;
|
||||
|
||||
|
||||
//get all the answers based on the locations
|
||||
var result = from answer in surveyAnswers
|
||||
from location in locations
|
||||
where answer.LocationId == location.Id
|
||||
select new Answer
|
||||
{
|
||||
Id = answer.Id,
|
||||
QuestionId = answer.QuestionId,
|
||||
AnswerText = answer.AnswerText,
|
||||
Comment = answer.Comment,
|
||||
RegionId = location.RegionId,
|
||||
LocationId = location.Id,
|
||||
SurveyResponseId = answer.SurveyResponseId
|
||||
};
|
||||
|
||||
//group records by answer and region
|
||||
var q = from e in result
|
||||
group e by (e.RegionId, e.AnswerText) into g
|
||||
select new AggregateResult
|
||||
{
|
||||
RegionId = g.Key.RegionId,
|
||||
Answers = new AggregateAnswer
|
||||
{
|
||||
Answer = g.Key.AnswerText,
|
||||
Counter = g.Count()
|
||||
}
|
||||
};
|
||||
|
||||
//build the result
|
||||
List<AnswerData> resultList = new List<AnswerData>();
|
||||
foreach (Region region in regions)
|
||||
{
|
||||
var answers = q.Where(x => x.RegionId.Equals(region.Id)).Select(x => x.Answers).ToList();
|
||||
resultList.Add(new AnswerData { RegionId = region.Id, Name = region.Name, Abbreviation = region.Abbreviation, Answers = answers });
|
||||
}
|
||||
|
||||
//return the object result
|
||||
return new ResultData { Regions = resultList };
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyResponsesProvider.getSurveyResultAsync()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//Method to get Survey Response by ResponseId
|
||||
private async Task<dynamic> getSurveyResponseByResponseIdAsync(Db.SurveyResponse surveyResponse)
|
||||
{
|
||||
try
|
||||
{
|
||||
var surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.Id == surveyResponse.Id).ToListAsync();
|
||||
//var surveyResponse = surveyResonses.SingleOrDefault();
|
||||
var employee = await employeeServiceProvider.getEmployeeAsync(surveyResponse.EmployeeId);
|
||||
var answers = await answerServiceProvider.GetAnswersByResponseIdAsync(surveyResponse.Id);
|
||||
var allQuestions = await questionServiceProvider.getQuestionsAsync();
|
||||
var questions = allQuestions.Where(s=> s.SurveyId == surveyResponse.Id);
|
||||
var attachments = await attachmentServiceProvider.getAttachmentsAsync();
|
||||
|
||||
|
||||
var result = from r in surveyResonses
|
||||
select new
|
||||
{
|
||||
r.Id,
|
||||
r.SurveyId,
|
||||
r.LocationId,
|
||||
r.EmployeeId,
|
||||
Employee = employee,
|
||||
answers = from ans in answers
|
||||
select new
|
||||
{
|
||||
ans.QuestionId,
|
||||
ans.Id,
|
||||
ans.AnswerText,
|
||||
ans.Comment,
|
||||
Questions = (from q in questions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.QuestionGroup, q.Questions }).SingleOrDefault(),
|
||||
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
|
||||
}
|
||||
};
|
||||
return result.SingleOrDefault();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyResponsesProvider.getSurveyResultAsync()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Method to get Survey Responses by surveyId
|
||||
private async Task<dynamic> getSurveyResponsesBySurveyIdAsync(int surveyId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId).ToListAsync();
|
||||
|
||||
var employees = await employeeServiceProvider.getEmployeesAsync();
|
||||
var answers = await answerServiceProvider.getAnswersAsync();
|
||||
var questions = await questionServiceProvider.getQuestionsAsync();
|
||||
var surveyQuestions = from q in questions where q.SurveyId == surveyId select q;
|
||||
|
||||
//var surveyQuestions = await questionServiceProvider.getSurveyQuestionsAsync(surveyId);
|
||||
|
||||
var attachments = await attachmentServiceProvider.getAttachmentsAsync();
|
||||
var result = from r in surveyResonses
|
||||
select new
|
||||
{
|
||||
r.Id,
|
||||
r.SurveyId,
|
||||
r.LocationId,
|
||||
r.EmployeeId,
|
||||
Employee = (from e in employees where e.Id == r.EmployeeId select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(),
|
||||
answers = from ans in answers
|
||||
where ans.SurveyResponseId == r.Id
|
||||
select new
|
||||
{
|
||||
ans.Id,
|
||||
ans.QuestionId,
|
||||
ans.AnswerText,
|
||||
ans.Comment,
|
||||
Questions = (from q in surveyQuestions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.QuestionGroup, q.Questions }).SingleOrDefault(),
|
||||
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
|
||||
|
||||
}
|
||||
};
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyResponsesProvider.getSurveyResponseBySurveyIdAsync()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Method to get All Survey Responses
|
||||
private async Task<dynamic> getAllSurveyResponsesAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var surveyResonses = await surveyResponseDbContext.SurveyResponses.ToListAsync();
|
||||
|
||||
var employees = await employeeServiceProvider.getEmployeesAsync();
|
||||
var answers = await answerServiceProvider.getAnswersAsync();
|
||||
var questions = await questionServiceProvider.getQuestionsAsync();
|
||||
var attachments = await attachmentServiceProvider.getAttachmentsAsync();
|
||||
|
||||
var result = from r in surveyResonses
|
||||
select new
|
||||
{
|
||||
r.Id,
|
||||
r.SurveyId,
|
||||
r.LocationId,
|
||||
r.EmployeeId,
|
||||
Employee = (from e in employees where r.EmployeeId == e.Id select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(),
|
||||
answers = from ans in answers
|
||||
where ans.SurveyResponseId == r.Id
|
||||
select new
|
||||
{
|
||||
ans.Id,
|
||||
ans.QuestionId,
|
||||
ans.AnswerText,
|
||||
ans.Comment,
|
||||
Questions = (from q in questions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.QuestionGroup, q.Questions }).SingleOrDefault(),
|
||||
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
|
||||
}
|
||||
};
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyResponsesProvider.getSurveyResponseBySurveyIdAsync()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Method to get Answers By Maintenance Center by surveyId
|
||||
private async Task<dynamic> getResultsByMaintenanceCenterAsync(int surveyId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId).ToListAsync();
|
||||
var answers = await answerServiceProvider.getAnswersAsync();
|
||||
var locations = await locationServiceProvider.getLocationsAsync();
|
||||
var maintenanceCenters = locations.DistinctBy(m => m.MaintenanceCenter);
|
||||
|
||||
//get all the answers for the particular survey
|
||||
var surveyAnswers = from resp in surveyResponses
|
||||
from ans in answers
|
||||
where ans.SurveyResponseId.Equals(resp.Id)
|
||||
select new
|
||||
{
|
||||
AnswerId = ans.Id,
|
||||
ans.QuestionId,
|
||||
ans.AnswerText,
|
||||
ans.Comment,
|
||||
resp.LocationId,
|
||||
ResponseId = resp.Id
|
||||
};
|
||||
|
||||
//get all the answers with location
|
||||
var surveyAnswersLocations = from surveyAns in surveyAnswers
|
||||
from location in locations
|
||||
where surveyAns.LocationId == location.Id
|
||||
select new { surveyAns, location.MaintenanceCenter };
|
||||
|
||||
//aggreting the answers
|
||||
var aggregatedResult = from e in surveyAnswersLocations
|
||||
group e by (e.MaintenanceCenter, e.surveyAns.AnswerText) into g
|
||||
select new
|
||||
{
|
||||
g.Key.MaintenanceCenter,
|
||||
Answers = new AggregateAnswer
|
||||
{
|
||||
Answer = g.Key.AnswerText,
|
||||
Counter = g.Count()
|
||||
}
|
||||
};
|
||||
|
||||
//format the result foreach maintenance center
|
||||
List<dynamic> results = new List<dynamic>();
|
||||
foreach (Location m in maintenanceCenters)
|
||||
{
|
||||
var _answers = aggregatedResult.Where(x => x.MaintenanceCenter.Equals(m.MaintenanceCenter)).Select(x => x.Answers).ToList();
|
||||
results.Add(new { m.Name, m.Id, m.RegionId, m.MaintenanceCenter, m.SchoolType, Answers = _answers });
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyResponsesProvider.getSurveyResponseBySurveyIdAsync()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//Method to get Survey Responses by surveyId and LocationId
|
||||
private async Task<dynamic> getSurveyResponsesBySurveyIdLocationIdAsync(int surveyId, string locationId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId && x.LocationId.Equals(locationId)).ToListAsync();
|
||||
|
||||
var employees = await employeeServiceProvider.getEmployeesAsync();
|
||||
var answers = await answerServiceProvider.getAnswersAsync();
|
||||
var questions = await questionServiceProvider.getQuestionsAsync();
|
||||
var surveyQuestions = from q in questions where q.SurveyId == surveyId select q;
|
||||
var attachments = await attachmentServiceProvider.getAttachmentsAsync();
|
||||
|
||||
var result = from r in surveyResonses
|
||||
select new
|
||||
{
|
||||
r.Id,
|
||||
r.SurveyId,
|
||||
r.LocationId,
|
||||
r.EmployeeId,
|
||||
Employee = (from e in employees where r.EmployeeId == e.Id select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(),
|
||||
answers = from ans in answers
|
||||
where ans.SurveyResponseId == r.Id
|
||||
|
||||
select new
|
||||
{
|
||||
ans.QuestionId,
|
||||
ans.Id,
|
||||
ans.AnswerText,
|
||||
ans.Comment,
|
||||
Questions = (from q in surveyQuestions where q.Id == ans.QuestionId select new { q.Id, q.QuestionNumber, q.QuestionGroup, q.Questions }).SingleOrDefault(),
|
||||
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
|
||||
}
|
||||
};
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyResponsesProvider.getSurveyResponseBySurveyIdAsync()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Method to get Survey Responses by surveyId questionId and answer
|
||||
private async Task<dynamic> getSurveyResponsesByAnswerAsync(Survey survey, Question question, string answer)
|
||||
{
|
||||
try
|
||||
{
|
||||
var surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == survey.Id).ToListAsync();
|
||||
//var questions = await questionServiceProvider.getQuestionsAsync();
|
||||
|
||||
var answers = await answerServiceProvider.getAnswersAsync();
|
||||
var employees = await employeeServiceProvider.getEmployeesAsync();
|
||||
var attachments = await attachmentServiceProvider.getAttachmentsAsync();
|
||||
|
||||
var result = from r in surveyResponses
|
||||
select new
|
||||
{
|
||||
r.Id,
|
||||
r.SurveyId,
|
||||
r.LocationId,
|
||||
r.EmployeeId,
|
||||
Employee = (from e in employees where r.EmployeeId == e.Id select new { e.Id, e.Name, e.BirthDate, e.Email, e.OfficePhoneNumber }).SingleOrDefault(),
|
||||
answers = from ans in answers
|
||||
where ans.SurveyResponseId == r.Id
|
||||
&& ans.QuestionId == question.Id
|
||||
&& ans.AnswerText.ToLower().Equals(answer.ToLower())
|
||||
|
||||
select new
|
||||
{
|
||||
ans.QuestionId,
|
||||
AnswerId = ans.Id,
|
||||
ans.AnswerText,
|
||||
ans.Comment,
|
||||
Question = question,
|
||||
Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI }
|
||||
}
|
||||
};
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyResponsesProvider.getSurveyResponseBySurveyIdAsync()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async Task<bool> ProcessAnswers(QuestionRequest questionRequest, int surveyResponseId)
|
||||
{
|
||||
if (questionRequest != null)
|
||||
{
|
||||
var answer = await answerServiceProvider.PostAnswersAsync(new Answer { Id = 0, QuestionId = questionRequest.QuestionId, AnswerText = questionRequest.AnswerText, Comment = questionRequest.Comment, SurveyResponseId = surveyResponseId });
|
||||
if (answer != null)
|
||||
{
|
||||
List<AnswerInfo> listAnswerInfo = new List<AnswerInfo>();
|
||||
listAnswerInfo.Add(new AnswerInfo { AnswerId = answer.Id, postedFiles = questionRequest.PostedFiles });
|
||||
var attachments = await attachmentServiceProvider.PostAttachmentsAsync(new AttachmentInfo { ResponseId = surveyResponseId, Answers = listAnswerInfo });
|
||||
|
||||
string message = $"Answer for question {questionRequest.QuestionId} saved to the database";
|
||||
logger?.LogInformation(message);
|
||||
return (true);
|
||||
}
|
||||
else
|
||||
{
|
||||
string message = $"Answer for question {questionRequest.QuestionId} cannot be saved to the database";
|
||||
logger?.LogInformation(message);
|
||||
return (false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var message = $"Answer for question {questionRequest.QuestionId} cannot be saved to the database - questionRequest object is null";
|
||||
logger?.LogInformation(message);
|
||||
return (false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PostSurveyAnswersAsync(Models.AnswerRequest answers)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (answers != null)
|
||||
{
|
||||
var response = await PostSurveyResponseAsync(new Models.SurveyResponse { Id = 0, SurveyId = answers.SurveyId, EmployeeId = answers.EmployeeId, LocationId = answers.LocationId });
|
||||
|
||||
if (response.IsSuccess)
|
||||
{
|
||||
var surveyResponse = response.SurveyResponse;
|
||||
|
||||
var answerTasks = new List<Task>(); //new List<string>();
|
||||
|
||||
//var tasks = answers.Answers.Select(x => ProcessAnswers(x,surveyResponse.SurveyResponseID));
|
||||
foreach (QuestionRequest ans in answers.Answers)
|
||||
{
|
||||
//var stopwatch = new Stopwatch();
|
||||
//stopwatch.Start();
|
||||
var task = Task.Run(() => ProcessAnswers(ans, surveyResponse.Id));
|
||||
|
||||
//var task = await ProcessAnswers(ans, surveyResponse.Id);
|
||||
answerTasks.Add(task);
|
||||
|
||||
|
||||
//stopwatch.Stop();
|
||||
//answerTasks.Add(ProcessAnswers(ans, surveyResponse.Id));
|
||||
}
|
||||
await Task.WhenAll(answerTasks);
|
||||
return (true, surveyResponse, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
var message = "Survey answers cannot be saved to the database";
|
||||
logger?.LogInformation(message);
|
||||
return (false, null, message);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var message = "Request failed because null object is submitted";
|
||||
logger?.LogInformation(message);
|
||||
return (false, null, message);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
using DamageAssesment.Api.SurveyResponses.Bases;
|
||||
using DamageAssesment.Api.SurveyResponses.Interfaces;
|
||||
using DamageAssesment.Api.SurveyResponses.Models;
|
||||
using Newtonsoft.Json;
|
||||
using System.Reflection;
|
||||
|
||||
namespace DamageAssesment.Api.SurveyResponses.Providers
|
||||
{
|
||||
public class SurveyServiceProvider :ServiceProviderBase, ISurveyServiceProvider
|
||||
{
|
||||
public SurveyServiceProvider(IConfiguration configuration, HttpClient httpClient, ILogger<EmployeeServiceProvider> logger) : base(configuration, httpClient, logger, "/api/Surveys", configuration.GetValue<string>("EndPointSettings:SurveyUrlBase"))
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<List<Survey>> getSurveysAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
httpClient.BaseAddress = new Uri(urlBase);
|
||||
var response = await httpClient.GetAsync(ressource);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
var surveys = JsonConvert.DeserializeObject<List<Survey>>(responseString);
|
||||
|
||||
if (surveys == null || !surveys.Any())
|
||||
return null;
|
||||
else return surveys;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyServiceProvider.getSurveysAsync()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Survey> getSurveyAsync(int surveyId)
|
||||
{
|
||||
try
|
||||
{
|
||||
httpClient.BaseAddress = new Uri(urlBase);
|
||||
var response = await httpClient.GetAsync(ressource+"/"+ surveyId);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
var survey = JsonConvert.DeserializeObject<Survey>(responseString);
|
||||
|
||||
if (survey == null )
|
||||
return null;
|
||||
else return survey;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError($"Exception Found : {ex.Message} - Ref: SurveyServiceProvider.getSurveyAsync()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"EndPointSettings": {
|
||||
"AnswerUrlBase": "http://localhost:5200",
|
||||
"LocationUrlBase": "http://localhost:5213",
|
||||
"RegionUrlBase": "http://localhost:5211",
|
||||
"QuestionUrlBase": "http://localhost:5133",
|
||||
"EmployeeUrlBase": "http://localhost:5135",
|
||||
"AttachmentUrlBase": "http://localhost:5243",
|
||||
"SurveyUrlBase": "http://localhost:5009"
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user