forked from MDCPS/DamageAssessment_Backend
		
	updated latest changes from dev
This commit is contained in:
		| @ -0,0 +1,228 @@ | ||||
| using DamageAssesment.Api.Responses.Interfaces; | ||||
| using DamageAssesment.Api.Responses.Models; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Controllers | ||||
| { | ||||
|     [ApiController] | ||||
|     public class SurveyResponsesController : ControllerBase | ||||
|     { | ||||
|         private readonly ISurveysResponse surveyResponseProvider; | ||||
|  | ||||
|         public SurveyResponsesController(ISurveysResponse surveyResponseProvider) | ||||
|         { | ||||
|             this.surveyResponseProvider = surveyResponseProvider; | ||||
|         } | ||||
|         /// <summary> | ||||
|         /// GET request for retrieving survey responses. | ||||
|         /// </summary> | ||||
|  | ||||
|         [Route("responses/{employeeid:int}")] | ||||
|         [Route("responses")] | ||||
|         [HttpGet] | ||||
|         public async Task<ActionResult> GetSurveyResponsesAsync(int? employeeid) | ||||
|         { | ||||
|             var result = await this.surveyResponseProvider.GetSurveyResponsesAsync(employeeid ?? 0); | ||||
|             if (result.IsSuccess) | ||||
|             { | ||||
|                 return Ok(result.surveyResponses); | ||||
|             } | ||||
|  | ||||
|             if (result.ErrorMessage == "No Data Found") | ||||
|                 return NoContent(); | ||||
|  | ||||
|             return BadRequest(result.ErrorMessage); | ||||
|         } | ||||
|         /// <summary> | ||||
|         /// GET request for retrieving survey responses by survey ID. | ||||
|         /// </summary> | ||||
|         [Route("responses/bysurvey/{surveyid:int}/{employeeid:int}")] | ||||
|         [Route("responses/bysurvey/{surveyid:int}")] | ||||
|         [HttpGet] | ||||
|         public async Task<ActionResult> GetSurveyResponsesAsync(int surveyid, int? employeeid) | ||||
|         { | ||||
|             var result = await this.surveyResponseProvider.GetSurveyResponsesBySurveyAsync(surveyid, employeeid ?? 0); | ||||
|             if (result.IsSuccess) | ||||
|             { | ||||
|                 return Ok(result.SurveyResponses); | ||||
|             } | ||||
|             return NoContent(); | ||||
|         } | ||||
|         /// <summary> | ||||
|         /// GET request for retrieving survey responses by survey and location IDs. | ||||
|         /// </summary> | ||||
|         /// <param name="surveyid">The ID of the survey for which responses are to be retrieved.</param> | ||||
|         /// <param name="locationid">The ID of the location for which responses are to be retrieved.</param> | ||||
|  | ||||
|         [Route("responses/{surveyid:int}/{locationid:int}/{employeeid:int}")] | ||||
|         [Route("responses/{surveyid:int}/{locationid:int}")] | ||||
|         [HttpGet] | ||||
|         public async Task<ActionResult> GetSurveyResponsesBySurveyAndLocationAsync(int surveyid, int locationid,int? employeeid) | ||||
|         { | ||||
|             var result = await this.surveyResponseProvider.GetSurveyResponsesBySurveyAndLocationAsync(surveyid, locationid,employeeid ?? 0); | ||||
|             if (result.IsSuccess) | ||||
|             { | ||||
|                 return Ok(result.SurveyResponses); | ||||
|             } | ||||
|             return NoContent(); | ||||
|         } | ||||
|         /// <summary> | ||||
|         /// GET request for retrieving survey responses by survey, question, and answer. | ||||
|         /// </summary> | ||||
|         /// <param name="surveyId">The ID of the survey for which responses are to be retrieved.</param> | ||||
|         /// <param name="questionId">The ID of the question for which responses are to be retrieved.</param> | ||||
|         /// <param name="answer">The answer for which responses are to be retrieved.</param> | ||||
|  | ||||
|         [Route("responses/byanswer/{surveyid:int}/{questionid:int}/{answer:alpha}/{employeeid:int}")] | ||||
|         [Route("responses/byanswer/{surveyid:int}/{questionid:int}/{answer:alpha}")] | ||||
|         [HttpGet] | ||||
|         public async Task<ActionResult> GetSurveyResponsesByAnswerAsyncAsync(int surveyid, int questionid, string answer, int? employeeid) | ||||
|         { | ||||
|             var result = await surveyResponseProvider.GetResponsesByAnswerAsync(surveyid, questionid, answer, employeeid ?? 0); | ||||
|             if (result.IsSuccess) | ||||
|             { | ||||
|                 return Ok(result.SurveyResponses); | ||||
|             } | ||||
|  | ||||
|             return NoContent(); | ||||
|         } | ||||
|         /// <summary> | ||||
|         /// GET request for retrieving answers from survey responses by survey ID and region. | ||||
|         /// </summary> | ||||
|         /// <param name="surveyId">The ID of the survey for which answers are to be retrieved.</param> | ||||
|  | ||||
|         [Route("responses/byregion/{surveyid:int}")] | ||||
|         [Route("responses/byregion/{surveyid:int}/{employeeid}")] | ||||
|         [HttpGet] | ||||
|         public async Task<ActionResult> GetAnswersByRegionAsync(int surveyid, int? employeeid) | ||||
|         { | ||||
|             var result = await this.surveyResponseProvider.GetAnswersByRegionAsync(surveyid, employeeid ?? 0); | ||||
|             if (result.IsSuccess) | ||||
|             { | ||||
|                 return Ok(result.Answers); | ||||
|             } | ||||
|             return NoContent(); | ||||
|         } | ||||
|         /// <summary> | ||||
|         /// GET request for retrieving survey responses by survey ID and maintenance center. | ||||
|         /// </summary> | ||||
|         /// <param name="surveyId">The ID of the survey for which responses are to be retrieved.</param> | ||||
|         [Route("responses/bymaintenancecenter/{surveyid:int}/{employeeid:int}")] | ||||
|         [Route("responses/bymaintenancecenter/{surveyid:int}")] | ||||
|         [HttpGet] | ||||
|         public async Task<ActionResult> GetAnswersByMaintenaceCentersync(int surveyid, int? employeeid) | ||||
|         { | ||||
|             var result = await this.surveyResponseProvider.GetSurveyResponsesByMaintenanceCenterAsync(surveyid, employeeid ?? 0); | ||||
|             if (result.IsSuccess) | ||||
|             { | ||||
|                 return Ok(result.SurveyResponses); | ||||
|             } | ||||
|             return NoContent(); | ||||
|         } | ||||
|         /// <summary> | ||||
|         /// GET request for retrieving a survey response by response ID. | ||||
|         /// </summary> | ||||
|         /// <param name="responseId">The ID of the survey response to be retrieved.</param> | ||||
|  | ||||
|         [HttpGet("responses/{id}")] | ||||
|         public async Task<ActionResult> GetSurveyResponseByIdAsync(int id) | ||||
|         { | ||||
|             var result = await this.surveyResponseProvider.GetSurveyResponseByIdAsync(id); | ||||
|             if (result.IsSuccess) | ||||
|             { | ||||
|                 return Ok(result.SurveyResponse); | ||||
|             } | ||||
|             return NoContent(); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// POST request for creating a new survey response. | ||||
|         /// </summary> | ||||
|         /// <param name="surveyResponse">The survey response object to be created.</param> | ||||
|  | ||||
|         [HttpPost("responses")] | ||||
|         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); | ||||
|         } | ||||
|         /// <summary> | ||||
|         /// PUT request for updating an existing survey response. | ||||
|         /// </summary> | ||||
|         /// <param name="Id">The ID of the survey response to be updated.</param> | ||||
|         /// <param name="surveyResponse">The updated survey response object.</param> | ||||
|  | ||||
|         [HttpPut("responses/{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); | ||||
|         } | ||||
|         /// <summary> | ||||
|         /// DELETE request for deleting an existing survey response. | ||||
|         /// </summary> | ||||
|  | ||||
|         [HttpDelete("responses/{id}")] | ||||
|         public async Task<ActionResult> DeleteSurveyResponseAsync(int id) | ||||
|         { | ||||
|             var result = await this.surveyResponseProvider.DeleteSurveyResponseAsync(id); | ||||
|             if (result.IsSuccess) | ||||
|             { | ||||
|                 return Ok(result.SurveyResponse); | ||||
|             } | ||||
|             return NotFound(); | ||||
|         } | ||||
|         /// <summary> | ||||
|         /// POST request for submitting survey with multiple answers. | ||||
|         /// </summary> | ||||
|         /// <param name="request">The answers to be submitted for the survey.</param> | ||||
|  | ||||
|         [HttpPost("responses/answers")] | ||||
|         public async Task<ActionResult> PostSurveyAnswersAsync(Request request) | ||||
|         { | ||||
|             var result = await this.surveyResponseProvider.PostSurveyAnswersAsync(request); | ||||
|  | ||||
|             if (result.IsSuccess) | ||||
|                 return Ok(result.SurveyResponse); | ||||
|             else | ||||
|                 return BadRequest(result.ErrorMessage); | ||||
|         } | ||||
|  | ||||
|         [Route("responses/surveys/active/{employeeid:int}")] | ||||
|         [Route("responses/surveys/active/{employeeid:int}/{language:alpha}")] | ||||
|         [HttpGet] | ||||
|         public async Task<ActionResult> GetActiveSurveysAsync(int employeeid, string? language) | ||||
|         { | ||||
|             var result = await this.surveyResponseProvider.GetActiveSurveysAsync(employeeid, language); | ||||
|             if (result.IsSuccess) | ||||
|             { | ||||
|                 return Ok(result.Surveys); | ||||
|             } | ||||
|             return NoContent(); | ||||
|         } | ||||
|  | ||||
|         [Route("responses/surveys/historic/{employeeid:int}")] | ||||
|         [Route("responses/surveys/historic/{employeeid:int}/{language:alpha}")] | ||||
|         [HttpGet] | ||||
|         public async Task<ActionResult> GetHistoricSurveysAsync(int employeeid, string? language) | ||||
|         { | ||||
|             var result = await this.surveyResponseProvider.GetHistoricSurveysAsync(employeeid, language); | ||||
|             if (result.IsSuccess) | ||||
|             { | ||||
|                 return Ok(result.Surveys); | ||||
|             } | ||||
|             return NoContent(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,33 @@ | ||||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||||
|  | ||||
|   <PropertyGroup> | ||||
|     <TargetFramework>net6.0</TargetFramework> | ||||
|     <Nullable>enable</Nullable> | ||||
|     <ImplicitUsings>enable</ImplicitUsings> | ||||
|     <GenerateDocumentationFile>True</GenerateDocumentationFile> | ||||
|   </PropertyGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|     <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" /> | ||||
|     <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.9" /> | ||||
|     <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.9"> | ||||
|       <PrivateAssets>all</PrivateAssets> | ||||
|       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||||
|     </PackageReference> | ||||
|     <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.9" /> | ||||
|     <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.9" /> | ||||
|     <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.9"> | ||||
|       <PrivateAssets>all</PrivateAssets> | ||||
|       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||||
|     </PackageReference> | ||||
|     <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" /> | ||||
|     <PackageReference Include="Microsoft.Extensions.Http.Polly" Version="7.0.9" /> | ||||
|     <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.21" /> | ||||
|     <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" /> | ||||
|     <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.5" /> | ||||
|     <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,32 @@ | ||||
| using System.ComponentModel.DataAnnotations; | ||||
| using System.ComponentModel.DataAnnotations.Schema; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Db | ||||
| { | ||||
|     [Table("SurveyResponses")] | ||||
|     public class SurveyResponse | ||||
|     { | ||||
|         [Key] | ||||
|         public int Id { get; set; } | ||||
|  | ||||
|         [ForeignKey("Survey")] | ||||
|         public int SurveyId { get; set; } | ||||
|         [ForeignKey("Location")] | ||||
|         public int LocationId { get; set; } | ||||
|  | ||||
|         [StringLength(6)] | ||||
|         [ForeignKey("Employee")] | ||||
|         public int EmployeeId { get; set; } | ||||
|  | ||||
|         public DateTime? CreatedDate { get; set; } = DateTime.Now; | ||||
|  | ||||
|         [StringLength(50)] | ||||
|         public string? ClientDevice { get; set; } | ||||
|  | ||||
|         [StringLength(250)] | ||||
|         public string? KeyAnswerResult { get; set; } | ||||
|         public double? Longitute { get; set; } | ||||
|         public double? Latitude { get; set; } | ||||
|  | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,27 @@ | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Db | ||||
| { | ||||
|     public class SurveyResponseDbContext:DbContext | ||||
|     { | ||||
|         private IConfiguration _Configuration { get; set; } | ||||
|         public SurveyResponseDbContext(DbContextOptions options, IConfiguration configuration) : base(options) | ||||
|         { | ||||
|             _Configuration = configuration; | ||||
|         } | ||||
|         protected override void OnConfiguring(DbContextOptionsBuilder options) | ||||
|         { | ||||
|             // connect to sql server with connection string from app settings | ||||
|             options.UseSqlServer(_Configuration.GetConnectionString("ResponsesConnection")); | ||||
|         } | ||||
|         public DbSet<Db.SurveyResponse> SurveyResponses { get; set; } | ||||
|  | ||||
|         protected override void OnModelCreating(ModelBuilder modelBuilder) | ||||
|         { | ||||
|             base.OnModelCreating(modelBuilder); | ||||
|             modelBuilder.Entity<SurveyResponse>() | ||||
|                 .Property(item => item.Id) | ||||
|                 .ValueGeneratedOnAdd(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,12 @@ | ||||
| using DamageAssesment.Api.Responses.Models; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.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.Responses.Models; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Interfaces | ||||
| { | ||||
|     public interface IAttachmentServiceProvider | ||||
|     { | ||||
|         Task<List<Attachment>> getAttachmentsAsync(); | ||||
|         Task<IEnumerable<Attachment>> PostAttachmentsAsync(Models.AttachmentInfo attachmentInfo); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,10 @@ | ||||
| using DamageAssesment.Api.Responses.Models; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Interfaces | ||||
| { | ||||
|     public interface IEmployeeServiceProvider | ||||
|     { | ||||
|         Task<List<Employee>> getEmployeesAsync(); | ||||
|         Task<Employee> getEmployeeAsync(int employeeId); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,9 @@ | ||||
| using DamageAssesment.Api.Responses.Models; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Interfaces | ||||
| { | ||||
|     public interface IHttpUtil | ||||
|     { | ||||
|         Task<string> SendAsync(HttpMethod method, string url, string JsonInput); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,9 @@ | ||||
| using DamageAssesment.Api.Responses.Models; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Interfaces | ||||
| { | ||||
|     public interface ILocationServiceProvider | ||||
|     { | ||||
|         Task<List<Location>> getLocationsAsync(); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,11 @@ | ||||
| using DamageAssesment.Api.Responses.Models; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.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.Responses.Models; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Interfaces | ||||
| { | ||||
|     public interface IRegionServiceProvider | ||||
|     { | ||||
|         Task<List<Region>> getRegionsAsync(); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,10 @@ | ||||
| using DamageAssesment.Api.Responses.Models; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Interfaces | ||||
| { | ||||
|     public interface ISurveyServiceProvider | ||||
|     { | ||||
|         Task<List<Survey>> getSurveysAsync(string language); | ||||
|         Task<Survey> getSurveyAsync(int surveyId); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,24 @@ | ||||
| using DamageAssesment.Api.Responses.Models; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Interfaces | ||||
| { | ||||
|     public interface ISurveysResponse | ||||
|     { | ||||
|         Task<(bool IsSuccess, dynamic Answers, string ErrorMessage)> GetAnswersByRegionAsync(int surveyId, int employeeid); | ||||
|         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(int employeeid); | ||||
|         Task<(bool IsSuccess, dynamic Surveys, string ErrorMessage)> GetActiveSurveysAsync(int employeeid, string language); | ||||
|         Task<(bool IsSuccess, dynamic Surveys, string ErrorMessage)> GetHistoricSurveysAsync(int employeeid, string language); | ||||
|         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, int employeeid); | ||||
|         Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesBySurveyAndLocationAsync(int surveyId, int locationId, int employeeid); | ||||
|         Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesByMaintenanceCenterAsync(int surveyId, int employeeid); | ||||
|         Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetResponsesByAnswerAsync(int surveyId, int questionId, string answer, int employeeid); | ||||
|         Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PostSurveyAnswersAsync(Request request); | ||||
|  | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,18 @@ | ||||
| using System.ComponentModel.DataAnnotations; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Models | ||||
| { | ||||
|     public class Answer | ||||
|     { | ||||
|         public int Id { get; set; } | ||||
|         public int QuestionId { get; set; } | ||||
|         public int LocationId { get; set; } | ||||
|         public string AnswerText { get; set; } | ||||
|         public string Comment { get; set; } | ||||
|         public int SurveyResponseId { get; set; } | ||||
|         public int RegionId { get; set; } | ||||
|        // public string? Name { get; set; } | ||||
|        // public string? Abbreviation { get; set; } | ||||
|  | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,10 @@ | ||||
| namespace DamageAssesment.Api.Responses.Models | ||||
| { | ||||
|     public class AnswerRequest | ||||
|     { | ||||
|         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,21 @@ | ||||
| using System.ComponentModel.DataAnnotations; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.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.Responses.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,16 @@ | ||||
| using System.ComponentModel.DataAnnotations; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Models | ||||
| { | ||||
|     public class Employee | ||||
|     { | ||||
|         public int Id { get; set; } | ||||
|         public string EmployeeCode { get; set; } | ||||
|         public string Name { get; set; } | ||||
|         public DateTime BirthDate { get; set; } | ||||
|         public string OfficePhoneNumber { get; set; } | ||||
|         public string Email { get; set; } | ||||
|         public bool IsActive { get; set; } | ||||
|         public string? PreferredLanguage { get; set; } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,11 @@ | ||||
| namespace DamageAssesment.Api.Responses.Models | ||||
| { | ||||
|     public class Location | ||||
|     { | ||||
|         public int Id { get; set; } | ||||
|         public int RegionId { get; set; } | ||||
|         public string Name { get; set; } | ||||
|         public string MaintenanceCenter { get; set; } | ||||
|         public string SchoolType { get; set; } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,17 @@ | ||||
| using System.Collections.Generic; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Models | ||||
| { | ||||
|      public class Question | ||||
|     { | ||||
|         public int Id { get; set; } | ||||
|         public string TypeText { get; set; } | ||||
|         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 int CategoryId { get; set; } | ||||
|         public Dictionary<string, string> Text { get; set; } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,11 @@ | ||||
| using System.ComponentModel.DataAnnotations; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Models | ||||
| { | ||||
|     public class Region | ||||
|     { | ||||
|         public int Id { get; set; } | ||||
|         public string Name { get; set; } | ||||
|         public string Abbreviation { get; set; } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,15 @@ | ||||
| namespace DamageAssesment.Api.Responses.Models | ||||
| { | ||||
|     public class Request | ||||
|     { | ||||
|         public int SurveyId { get; set; } | ||||
|         public int LocationId { get; set; } | ||||
|         public int EmployeeId { get; set; } | ||||
|         public string? ClientDevice { get; set; } | ||||
|         public string? KeyAnswerResult { get; set; } | ||||
|         public double? Longitute { get; set; } | ||||
|         public double? Latitude { get; set; } | ||||
|         public DateTime? CreatedDate { get; set; } = DateTime.Now; | ||||
|         public List<AnswerRequest> Answers { get; set; } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,14 @@ | ||||
| using System.ComponentModel.DataAnnotations; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Models | ||||
| { | ||||
|     public class Survey | ||||
|     { | ||||
|         public int Id { get; set; } | ||||
|         public bool IsEnabled { get; set; } | ||||
|         public DateTime StartDate { get; set; } | ||||
|         public DateTime EndDate { get; set; } | ||||
|         public DateTime CreatedDate { get; set; } | ||||
|         public Dictionary<string, string> Titles { get; set; } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,11 @@ | ||||
| namespace DamageAssesment.Api.Responses.Models | ||||
| { | ||||
|     public class SurveyQuestions | ||||
|     { | ||||
|         public int CategoryId { get; set; } | ||||
|         public string CategoryName { get; set; } | ||||
|         public string CategoryImage { get; set; } | ||||
|         public object CategoryNames { get; set; } | ||||
|         public List<Question> Questions { get; set; } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,18 @@ | ||||
| using System.ComponentModel.DataAnnotations; | ||||
| using System.ComponentModel.DataAnnotations.Schema; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Models | ||||
| { | ||||
|     public class SurveyResponse | ||||
|     { | ||||
|         public int Id { get; set; } | ||||
|         public int SurveyId { get; set; } | ||||
|         public int LocationId { get; set; } | ||||
|         public int EmployeeId { get; set; } | ||||
|         public DateTime CreatedDate { get; set; } | ||||
|         public string ClientDevice { get; set; } | ||||
|         public string KeyAnswerResult { get; set; } | ||||
|         public double Longitute { get; set; } | ||||
|         public double Latitude { get; set; } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,13 @@ | ||||
| namespace DamageAssesment.Api.Responses.Models | ||||
| { | ||||
|     public class SurveyTranslation | ||||
|     {         | ||||
|         public string Title { get; set; } | ||||
|         public string Language { get; set; } | ||||
|     } | ||||
| } | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| @ -0,0 +1,11 @@ | ||||
| namespace DamageAssesment.Api.Responses.Profiles | ||||
| { | ||||
|     public class SurveyResponsesProvider : AutoMapper.Profile | ||||
|     { | ||||
|         public SurveyResponsesProvider() | ||||
|         { | ||||
|             CreateMap<Models.SurveyResponse, Db.SurveyResponse>(); | ||||
|             CreateMap<Db.SurveyResponse, Models.SurveyResponse>(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										62
									
								
								DamageAssesmentApi/DamageAssesment.Api.Responses/Program.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								DamageAssesmentApi/DamageAssesment.Api.Responses/Program.cs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,62 @@ | ||||
| using DamageAssesment.Api.Responses.Db; | ||||
| using DamageAssesment.Api.Responses.Interfaces; | ||||
| using DamageAssesment.Api.Responses.Services; | ||||
| using DamageAssesment.Api.Responses.Providers; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
| using Polly; | ||||
| using System.Reflection; | ||||
|  | ||||
| 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<IAnswerServiceProvider, AnswerServiceProvider>(); | ||||
| builder.Services.AddScoped<ILocationServiceProvider, LocationServiceProvider>(); | ||||
| builder.Services.AddScoped<IRegionServiceProvider, RegionServiceProvider>(); | ||||
| builder.Services.AddScoped<IQuestionServiceProvider, QuestionServiceProvider>(); | ||||
| builder.Services.AddScoped<IEmployeeServiceProvider, EmployeeServiceProvider>(); | ||||
| builder.Services.AddScoped<IAttachmentServiceProvider, AttachmentServiceProvider>(); | ||||
| builder.Services.AddScoped<ISurveyServiceProvider, SurveyServiceProvider>(); | ||||
|  | ||||
| builder.Services.AddHttpClient<IHttpUtil, HttpUtil>(). | ||||
|     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.AddSwaggerGen(c => | ||||
| { | ||||
|     // Include XML comments from your assembly | ||||
|     var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; | ||||
|     var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); | ||||
|     c.IncludeXmlComments(xmlPath); | ||||
| }); | ||||
| builder.Services.AddDbContext<SurveyResponseDbContext>(option => | ||||
| { | ||||
|     option.UseSqlServer("ResponsesConnection"); | ||||
| }); | ||||
| 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,963 @@ | ||||
| using AutoMapper; | ||||
| using DamageAssesment.Api.Responses.Db; | ||||
| using DamageAssesment.Api.Responses.Interfaces; | ||||
| using DamageAssesment.Api.Responses.Models; | ||||
| using Microsoft.EntityFrameworkCore; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.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(); | ||||
|         } | ||||
|  | ||||
|         public void SeedData() | ||||
|         { | ||||
|             // Check if SurveyResponses exist, if not, seed data | ||||
|             if (!surveyResponseDbContext.SurveyResponses.Any()) | ||||
|             { | ||||
|  | ||||
|                 // Create and save SurveyResponse records with references to existing Employee and Location records | ||||
|                 surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = 1, LocationId = 1, ClientDevice = "Mobile", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "true", CreatedDate = DateTime.Now }); | ||||
|                 surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = 2, LocationId = 2, ClientDevice = "Mobile", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "true", CreatedDate = DateTime.Now }); | ||||
|  | ||||
|                 surveyResponseDbContext.SaveChanges(); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|  | ||||
|         public async Task<(bool IsSuccess, dynamic Answers, string ErrorMessage)> GetAnswersByRegionAsync(int surveyId, int employeeid) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger?.LogInformation("Querying to get SurveyResponse object from DB"); | ||||
|                 IQueryable<Db.SurveyResponse> listSurveyResponse = null; | ||||
|                 if (employeeid == 0) | ||||
|                 { | ||||
|                     listSurveyResponse = surveyResponseDbContext.SurveyResponses.Where(s => s.SurveyId == surveyId); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     listSurveyResponse = surveyResponseDbContext.SurveyResponses.Where(s => s.SurveyId == surveyId && s.EmployeeId == employeeid); | ||||
|                 } | ||||
|  | ||||
|                 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 Surveys, string ErrorMessage)> GetActiveSurveysAsync(int employeeid, string language) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger?.LogInformation("Querying to get SurveyResponse object from DB"); | ||||
|                 //get all the survey that already taken by the employee | ||||
|                 var listOfsurveysId = await surveyResponseDbContext.SurveyResponses.Where(x => x.EmployeeId == employeeid).Select(y => y.SurveyId).ToListAsync(); | ||||
|                 var surveys = await surveyServiceProvider.getSurveysAsync(language); | ||||
|  | ||||
|                 if (surveys != null) | ||||
|                 { | ||||
|                     surveys = surveys.Where(s => s.IsEnabled == true && s.StartDate <= DateTime.Now && s.EndDate >= DateTime.Now).ToList(); | ||||
|                 } | ||||
|  | ||||
|                 if (listOfsurveysId==null || listOfsurveysId.Count == 0) | ||||
|                     return (true, surveys, null); | ||||
|  | ||||
|                 var activeSurveys = surveys.Where(s => s.IsEnabled == true && s.StartDate <= DateTime.Now && s.EndDate >= DateTime.Now && !listOfsurveysId.Contains(s.Id)); | ||||
|                 return (true, activeSurveys, null); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger?.LogError(ex.ToString()); | ||||
|                 return (false, null, ex.Message); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async Task<(bool IsSuccess, dynamic Surveys, string ErrorMessage)> GetHistoricSurveysAsync(int employeeid, string language) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger?.LogInformation("Querying to get SurveyResponse object from DB"); | ||||
|                 //get all the survey that already taken by the employee | ||||
|                 var surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.EmployeeId == employeeid).ToListAsync(); | ||||
|                 var surveys = await surveyServiceProvider.getSurveysAsync(language); | ||||
|  | ||||
|                 var historicSurveys = from s in surveys | ||||
|                                       from r in surveyResponses | ||||
|                                       where s.Id == r.SurveyId | ||||
|                                       select s; | ||||
|  | ||||
|                 return (true, historicSurveys, null); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger?.LogError(ex.ToString()); | ||||
|                 return (false, null, ex.Message); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async Task<(bool IsSuccess, dynamic SurveyResponses, string ErrorMessage)> GetSurveyResponsesBySurveyAsync(int surveyId, int employeeid) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger?.LogInformation("Querying to get Survey object from microservice"); | ||||
|                 var survey = await surveyServiceProvider.getSurveyAsync(surveyId); | ||||
|  | ||||
|                 if (survey != null) | ||||
|                 { | ||||
|                     var answers = await getSurveyResponsesBySurveyIdAsync(surveyId, employeeid); | ||||
|  | ||||
|                     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, int locationId, int employeeid) | ||||
|         { | ||||
|             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, employeeid); | ||||
|  | ||||
|                     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, int employeeid) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger?.LogInformation("Querying to get Survey object from microservice"); | ||||
|                 var survey = await surveyServiceProvider.getSurveyAsync(surveyId); | ||||
|  | ||||
|                 if (survey != null) | ||||
|                 { | ||||
|                     var answers = await getResultsByMaintenanceCenterAsync(surveyId, employeeid); | ||||
|  | ||||
|                     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, int employeeid) | ||||
|         { | ||||
|             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, employeeid); | ||||
|  | ||||
|                     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(int employeeid) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 var responses = await getAllSurveyResponsesAsync(employeeid); | ||||
|  | ||||
|                 if (responses != null) | ||||
|                     return (true, responses, "Request Successful."); | ||||
|                 else | ||||
|                 { | ||||
|                     responses = new List<Models.SurveyResponse>(); | ||||
|                     return (true, responses, "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 _surveyResponse = mapper.Map<Models.SurveyResponse, Db.SurveyResponse>(surveyResponse); | ||||
|                     surveyResponseDbContext.SurveyResponses.Add(_surveyResponse); | ||||
|                     await surveyResponseDbContext.SaveChangesAsync(); | ||||
|                     surveyResponse.Id = _surveyResponse.Id; | ||||
|                     return (true, surveyResponse, "Request Successful"); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     logger?.LogInformation($"SurveyResponse cannot be added"); | ||||
|                     return (false, null, "SurveyResponse 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.AsNoTracking().Where(s => s.Id == Id).FirstOrDefaultAsync(); | ||||
|  | ||||
|                     if (_SurveyResponse != null) | ||||
|                     { | ||||
|                         var response = mapper.Map<Models.SurveyResponse, Db.SurveyResponse>(SurveyResponse); | ||||
|                         surveyResponseDbContext.Update(response); | ||||
|                         await surveyResponseDbContext.SaveChangesAsync(); | ||||
|                         return (true, 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); | ||||
|                     await surveyResponseDbContext.SaveChangesAsync(); | ||||
|                     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.QuestionId, | ||||
|                                            answer.AnswerText, | ||||
|                                            answer.Comment, | ||||
|                                            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.Equals(location.Id) | ||||
|                              select new | ||||
|                              { | ||||
|                                  answer.Id, | ||||
|                                  answer.QuestionId, | ||||
|                                  answer.AnswerText, | ||||
|                                  answer.Comment, | ||||
|                                  location.RegionId, | ||||
|                                  LocationId = location.Id, | ||||
|                                  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 | ||||
|                         { | ||||
|                             g.Key.RegionId, | ||||
|                             Answers = new | ||||
|                             { | ||||
|                                 g.Key.AnswerText, | ||||
|                                 Counter = g.Count() | ||||
|                             } | ||||
|                         }; | ||||
|  | ||||
|                 //build the result | ||||
|  | ||||
|                 List<dynamic> resultList = new List<dynamic>(); | ||||
|                 foreach (Region region in regions) | ||||
|                 { | ||||
|                     var answers = from u in q.ToList() | ||||
|                                   where u.RegionId.Equals(region.Id) | ||||
|                                   select u.Answers; | ||||
|  | ||||
|                     resultList.Add(new { RegionId = region.Id, region.Name, region.Abbreviation, Answers = answers }); | ||||
|                 } | ||||
|                 //return the object result | ||||
|                 return new { 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 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.SurveyId); | ||||
|                 var attachments = await attachmentServiceProvider.getAttachmentsAsync(); | ||||
|  | ||||
|                 var result = new | ||||
|                 { | ||||
|                     surveyResponse.Id, | ||||
|                     surveyResponse.SurveyId, | ||||
|                     surveyResponse.LocationId, | ||||
|                     surveyResponse.EmployeeId, | ||||
|                     surveyResponse.ClientDevice, | ||||
|                     surveyResponse.KeyAnswerResult, | ||||
|                     surveyResponse.Longitute, | ||||
|                     surveyResponse.Latitude, | ||||
|                     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.CategoryId, q.Text }).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.getSurveyResultAsync()"); | ||||
|                 return null; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|  | ||||
|         //Method to get Survey Responses by surveyId | ||||
|         private async Task<dynamic> getSurveyResponsesBySurveyIdAsync(int surveyId, int employeeid) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 List<Db.SurveyResponse> surveyResonses = null; | ||||
|                 Employee employee = null; | ||||
|                 List<Employee> employees = null; | ||||
|                 if (employeeid == 0) | ||||
|                 { | ||||
|                     surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId).ToListAsync(); | ||||
|                     employees = await employeeServiceProvider.getEmployeesAsync(); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId && x.EmployeeId == employeeid).ToListAsync(); | ||||
|                     employee = await employeeServiceProvider.getEmployeeAsync(employeeid); | ||||
|                 } | ||||
|  | ||||
|                 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(); | ||||
|  | ||||
|                 if (employeeid == 0) | ||||
|                 { | ||||
|                     var result = from r in surveyResonses | ||||
|                                  select new | ||||
|                                  { | ||||
|                                      r.Id, | ||||
|                                      r.SurveyId, | ||||
|                                      r.LocationId, | ||||
|                                      r.EmployeeId, | ||||
|                                      r.ClientDevice, | ||||
|                                      r.KeyAnswerResult, | ||||
|                                      r.Longitute, | ||||
|                                      r.Latitude, | ||||
|                                      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.CategoryId, q.Text }).SingleOrDefault(), | ||||
|                                                    Attachments = from att in attachments where att.AnswerId == ans.Id select new { att.Id, att.URI } | ||||
|  | ||||
|                                                } | ||||
|                                  }; | ||||
|                     return result; | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     object _employee = new { }; | ||||
|                     if (employee != null) | ||||
|                     { | ||||
|                         _employee = new { employee.Id, employee.Name, employee.BirthDate, employee.Email, employee.OfficePhoneNumber }; | ||||
|                     } | ||||
|                     var result = from r in surveyResonses | ||||
|                                  select new | ||||
|                                  { | ||||
|                                      r.Id, | ||||
|                                      r.SurveyId, | ||||
|                                      r.LocationId, | ||||
|                                      r.EmployeeId, | ||||
|                                      r.ClientDevice, | ||||
|                                      r.KeyAnswerResult, | ||||
|                                      r.Longitute, | ||||
|                                      r.Latitude, | ||||
|                                      Employee = _employee, | ||||
|                                      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.CategoryId, q.Text }).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(int employeeid) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 List<Db.SurveyResponse> surveyResonses = null; | ||||
|                 Employee employee = null; | ||||
|                 List<Employee> employees = null; | ||||
|                 object _employee = new { }; | ||||
|                 if (employeeid == 0) | ||||
|                 { | ||||
|                     surveyResonses = await surveyResponseDbContext.SurveyResponses.ToListAsync(); | ||||
|                     employees = await employeeServiceProvider.getEmployeesAsync(); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.EmployeeId == employeeid).ToListAsync(); | ||||
|                     employee = await employeeServiceProvider.getEmployeeAsync(employeeid); | ||||
|  | ||||
|                     if (employee != null) | ||||
|                     { | ||||
|                         _employee = new { employee.Id, employee.Name, employee.BirthDate, employee.Email, employee.OfficePhoneNumber }; | ||||
|                     } | ||||
|                 } | ||||
|  | ||||
|  | ||||
|                 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, | ||||
|                                  r.ClientDevice, | ||||
|                                  r.KeyAnswerResult, | ||||
|                                  r.Longitute, | ||||
|                                  r.Latitude, | ||||
|                                  Employee = employeeid != 0 ? _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.CategoryId, q.Text }).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, int employeeid) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 List<Db.SurveyResponse> surveyResponses = null; | ||||
|                 if (employeeid == 0) | ||||
|                 { | ||||
|                     surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId).ToListAsync(); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId && x.EmployeeId == employeeid).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.Equals(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 | ||||
|                                            { | ||||
|                                                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, int locationId, int employeeid) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 List<Db.SurveyResponse> surveyResonses = null; | ||||
|                 Employee employee = null; | ||||
|                 List<Employee> employees = null; | ||||
|                 object _employee = new { }; | ||||
|  | ||||
|                 if (employeeid == 0) | ||||
|                 { | ||||
|                     surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId && x.LocationId == locationId).ToListAsync(); | ||||
|                     employees = await employeeServiceProvider.getEmployeesAsync(); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     surveyResonses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == surveyId && x.EmployeeId == employeeid && x.LocationId == locationId).ToListAsync(); | ||||
|                     employee = await employeeServiceProvider.getEmployeeAsync(employeeid); | ||||
|  | ||||
|                     if (employee != null) | ||||
|                     { | ||||
|                         _employee = new { employee.Id, employee.Name, employee.BirthDate, employee.Email, employee.OfficePhoneNumber }; | ||||
|                     } | ||||
|                 } | ||||
|  | ||||
|                 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, | ||||
|                                  r.ClientDevice, | ||||
|                                  r.KeyAnswerResult, | ||||
|                                  r.Longitute, | ||||
|                                  r.Latitude, | ||||
|                                  Employee = employeeid != 0 ? _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.CategoryId, q.Text }).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, int employeeid) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 List<Db.SurveyResponse> surveyResponses = null; | ||||
|                 Employee employee = null; | ||||
|                 List<Employee> employees = null; | ||||
|                 object _employee = new { }; | ||||
|  | ||||
|                 if (employeeid == 0) | ||||
|                 { | ||||
|                     surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == survey.Id).ToListAsync(); | ||||
|                     employees = await employeeServiceProvider.getEmployeesAsync(); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.SurveyId == survey.Id && x.EmployeeId == employeeid).ToListAsync(); | ||||
|                     employee = await employeeServiceProvider.getEmployeeAsync(employeeid); | ||||
|  | ||||
|                     if (employee != null) | ||||
|                     { | ||||
|                         _employee = new { employee.Id, employee.Name, employee.BirthDate, employee.Email, employee.OfficePhoneNumber }; | ||||
|                     } | ||||
|                 } | ||||
|  | ||||
|                 //var surveyResponses = await surveyResponseDbContext.Responses.Where(x => x.SurveyId == survey.Id).ToListAsync(); | ||||
|                 // var employees = await employeeServiceProvider.getEmployeesAsync(); | ||||
|                 var answers = await answerServiceProvider.getAnswersAsync(); | ||||
|                 var attachments = await attachmentServiceProvider.getAttachmentsAsync(); | ||||
|  | ||||
|                 var result = from r in surveyResponses | ||||
|                              select new | ||||
|                              { | ||||
|                                  r.Id, | ||||
|                                  r.SurveyId, | ||||
|                                  r.LocationId, | ||||
|                                  r.EmployeeId, | ||||
|                                  r.ClientDevice, | ||||
|                                  r.KeyAnswerResult, | ||||
|                                  r.Longitute, | ||||
|                                  r.Latitude, | ||||
|                                  Employee = employeeid != 0 ? _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(AnswerRequest answerRequest, int surveyResponseId) | ||||
|         { | ||||
|             if (answerRequest != null) | ||||
|             { | ||||
|                 var answer = await answerServiceProvider.PostAnswersAsync(new Models.Answer { QuestionId = answerRequest.QuestionId, AnswerText = answerRequest.AnswerText, Comment = answerRequest.Comment, SurveyResponseId = surveyResponseId }); | ||||
|                 if (answer != null) | ||||
|                 { | ||||
|                     List<AnswerInfo> listAnswerInfo = new List<AnswerInfo>(); | ||||
|                     listAnswerInfo.Add(new AnswerInfo { AnswerId = answer.Id, postedFiles = answerRequest.PostedFiles }); | ||||
|                     var attachments = attachmentServiceProvider.PostAttachmentsAsync(new AttachmentInfo { ResponseId = surveyResponseId, Answers = listAnswerInfo }); | ||||
|  | ||||
|                     string message = $"Answer for question {answerRequest.QuestionId} saved to the database"; | ||||
|                     logger?.LogInformation(message); | ||||
|                     return (true); | ||||
|                 } | ||||
|                 else | ||||
|                 { | ||||
|                     string message = $"Answer for question {answerRequest.QuestionId} cannot be saved to the database"; | ||||
|                     logger?.LogInformation(message); | ||||
|                     return (false); | ||||
|                 } | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 var message = $"Answer for question {answerRequest.QuestionId} cannot be saved to the database - answerRequest object is null"; | ||||
|                 logger?.LogInformation(message); | ||||
|                 return (false); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|  | ||||
|         public async Task<(bool IsSuccess, Models.SurveyResponse SurveyResponse, string ErrorMessage)> PostSurveyAnswersAsync(Models.Request request) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 if (request != null) | ||||
|                 { | ||||
|                     var response = await PostSurveyResponseAsync(new Models.SurveyResponse { SurveyId = request.SurveyId, EmployeeId = request.EmployeeId, LocationId = request.LocationId, ClientDevice = request.ClientDevice, KeyAnswerResult = request.KeyAnswerResult, Latitude = Convert.ToDouble(request.Latitude), Longitute = Convert.ToDouble(request.Longitute), CreatedDate = DateTime.Now }); | ||||
|                     if (response.IsSuccess) | ||||
|                     { | ||||
|                         var surveyResponse = response.SurveyResponse; | ||||
|                         var tasks = request.Answers.Select(x => ProcessAnswers(x, surveyResponse.Id)); | ||||
|                         await Task.WhenAll(tasks); | ||||
|                         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,72 @@ | ||||
| using DamageAssesment.Api.Responses.Interfaces; | ||||
| using DamageAssesment.Api.Responses.Models; | ||||
| using Newtonsoft.Json; | ||||
|  | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Services | ||||
| { | ||||
|     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")) | ||||
|         { | ||||
|         } | ||||
|         public async Task<List<Answer>> getAnswersAsync() | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 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 | ||||
|             { | ||||
|                 url = urlBase + string.Format(configuration.GetValue<string>("RessourceSettings:AnswerByResponse"), 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 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,55 @@ | ||||
| using DamageAssesment.Api.Responses.Interfaces; | ||||
| using DamageAssesment.Api.Responses.Models; | ||||
| using Newtonsoft.Json; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Services | ||||
| { | ||||
|     public class AttachmentServiceProvider : ServiceProviderBase, IAttachmentServiceProvider | ||||
|     { | ||||
|         public AttachmentServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<AttachmentServiceProvider> logger) : base(configuration, httpUtil, logger, configuration.GetValue<string>("RessourceSettings:Attachment"), configuration.GetValue<string>("EndPointSettings:AttachmentUrlBase")) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public async Task<List<Attachment>> getAttachmentsAsync() | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 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 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.Responses.Interfaces; | ||||
| using DamageAssesment.Api.Responses.Models; | ||||
| using Microsoft.AspNetCore.Mvc.Routing; | ||||
| using Newtonsoft.Json; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Services | ||||
| { | ||||
|     public class EmployeeServiceProvider : ServiceProviderBase, IEmployeeServiceProvider | ||||
|     { | ||||
|         public EmployeeServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<EmployeeServiceProvider> logger) : base(configuration, httpUtil, logger, configuration.GetValue<string>("RessourceSettings:Employee"), configuration.GetValue<string>("EndPointSettings:EmployeeUrlBase")) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public async Task<List<Employee>> getEmployeesAsync() | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 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(int employeeId) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 url = urlBase + string.Format(configuration.GetValue<string>("RessourceSettings:EmployeeById"), 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.Responses.Interfaces; | ||||
| using System.Net.Http.Headers; | ||||
| using System.Text; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.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,31 @@ | ||||
| using DamageAssesment.Api.Responses.Interfaces; | ||||
| using DamageAssesment.Api.Responses.Models; | ||||
| using Newtonsoft.Json; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Services | ||||
| { | ||||
|     public class LocationServiceProvider :ServiceProviderBase, ILocationServiceProvider | ||||
|     { | ||||
|         public LocationServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<LocationServiceProvider> logger) : base(configuration, httpUtil, logger, configuration.GetValue<string>("RessourceSettings:Location"), configuration.GetValue<string>("EndPointSettings:LocationUrlBase")) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public async Task<List<Location>> getLocationsAsync() | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 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,70 @@ | ||||
| using DamageAssesment.Api.Responses.Interfaces; | ||||
| using DamageAssesment.Api.Responses.Models; | ||||
| using Newtonsoft.Json; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Services | ||||
| { | ||||
|     public class QuestionServiceProvider : ServiceProviderBase, IQuestionServiceProvider | ||||
|     { | ||||
|         public QuestionServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<QuestionServiceProvider> logger) : base(configuration, httpUtil, logger, configuration.GetValue<string>("RessourceSettings:Question"), configuration.GetValue<string>("EndPointSettings:QuestionUrlBase")) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public async Task<List<Question>> getQuestionsAsync() | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 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 | ||||
|             { | ||||
|                 url = urlBase + string.Format(configuration.GetValue<string>("RessourceSettings:SurveyQuestion"), 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 | ||||
|             { | ||||
|                 url = urlBase + string.Format(configuration.GetValue<string>("RessourceSettings:QuestionById"), 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,30 @@ | ||||
| using DamageAssesment.Api.Responses.Interfaces; | ||||
| using DamageAssesment.Api.Responses.Models; | ||||
| using Newtonsoft.Json; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Services | ||||
| { | ||||
|     public class RegionServiceProvider : ServiceProviderBase, IRegionServiceProvider | ||||
|     { | ||||
|         public RegionServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<RegionServiceProvider> logger) : base(configuration, httpUtil, logger, configuration.GetValue<string>("RessourceSettings:Region"), configuration.GetValue<string>("EndPointSettings:LocationUrlBase")) | ||||
|         { | ||||
|         } | ||||
|         public async Task<List<Region>> getRegionsAsync() | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 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,25 @@ | ||||
| using DamageAssesment.Api.Responses.Interfaces; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Services | ||||
| { | ||||
|     public class ServiceProviderBase | ||||
|     { | ||||
|         protected readonly IConfiguration configuration; | ||||
|         protected readonly IHttpUtil httpUtil; | ||||
|         protected readonly ILogger<ServiceProviderBase> logger; | ||||
|         protected string ressource; | ||||
|         protected string urlBase; | ||||
|         protected string url; | ||||
|          | ||||
|  | ||||
|         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; | ||||
|             url = urlBase + ressource; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,52 @@ | ||||
| using DamageAssesment.Api.Responses.Interfaces; | ||||
| using DamageAssesment.Api.Responses.Models; | ||||
| using Newtonsoft.Json; | ||||
|  | ||||
| namespace DamageAssesment.Api.Responses.Services | ||||
| { | ||||
|     public class SurveyServiceProvider : ServiceProviderBase, ISurveyServiceProvider | ||||
|     { | ||||
|         public SurveyServiceProvider(IConfiguration configuration, IHttpUtil httpUtil, ILogger<EmployeeServiceProvider> logger) : base(configuration, httpUtil, logger, configuration.GetValue<string>("RessourceSettings:Survey"), configuration.GetValue<string>("EndPointSettings:SurveyUrlBase")) | ||||
|         { | ||||
|         } | ||||
|  | ||||
|         public async Task<List<Survey>> getSurveysAsync(string language) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 if (!string.IsNullOrEmpty(language)) | ||||
|                     url = url + "/" + language; | ||||
|                 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 | ||||
|             { | ||||
|                 url = urlBase + string.Format(configuration.GetValue<string>("RessourceSettings:SurveyById"), 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; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,36 @@ | ||||
| { | ||||
|   "Logging": { | ||||
|     "LogLevel": { | ||||
|       "Default": "Information", | ||||
|       "Microsoft.AspNetCore": "Warning" | ||||
|     } | ||||
|   }, | ||||
|   "AllowedHosts": "*", | ||||
|   "ConnectionStrings": { | ||||
|     "ResponsesConnection": "Server=DESKTOP-OF5DPLQ\\SQLEXPRESS;Database=da_survey_dev;Trusted_Connection=True;TrustServerCertificate=True;" | ||||
|   }, | ||||
|   "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" | ||||
|   }, | ||||
|  | ||||
|   "RessourceSettings": { | ||||
|     "Employee": "/employees", | ||||
|     "EmployeeById": "/employees/{0}", | ||||
|     "Question": "/questions", | ||||
|     "QuestionById": "/questions/{0}", | ||||
|     "SurveyQuestion": "/questions/bysurvey/{0}", | ||||
|     "Survey": "/surveys", | ||||
|     "SurveyById": "/surveys/{0}", | ||||
|     "Attachment": "/attachments", | ||||
|     "Answer": "/answers", | ||||
|     "AnswerByResponse": "/answers/byresponse/{0}", | ||||
|     "Location": "/locations", | ||||
|     "Region": "/regions" | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user