diff --git a/DamageAssesmentApi/DamageAssesment.Api.Responses/Controllers/ResponsesController.cs b/DamageAssesmentApi/DamageAssesment.Api.Responses/Controllers/ResponsesController.cs index 0cfc94a..4cdeab6 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Responses/Controllers/ResponsesController.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Responses/Controllers/ResponsesController.cs @@ -212,10 +212,12 @@ namespace DamageAssesment.Api.Responses.Controllers } [Authorize(Roles = "admin,survey,user,report")] + [Route("responses/surveys/active")] + [Route("responses/surveys/active/{language:alpha}")] [Route("responses/surveys/active/{employeeid:int}")] [Route("responses/surveys/active/{employeeid:int}/{language:alpha}")] [HttpGet] - public async Task GetActiveSurveysAsync(int employeeid, string? language) + public async Task GetActiveSurveysAsync(int? employeeid, string? language) { var result = await this.surveyResponseProvider.GetActiveSurveysAsync(employeeid, language); if (result.IsSuccess) @@ -226,10 +228,12 @@ namespace DamageAssesment.Api.Responses.Controllers } [Authorize(Roles = "admin,survey,user,report")] + [Route("responses/surveys/historic")] + [Route("responses/surveys/historic/{language:alpha}")] [Route("responses/surveys/historic/{employeeid:int}")] [Route("responses/surveys/historic/{employeeid:int}/{language:alpha}")] [HttpGet] - public async Task GetHistoricSurveysAsync(int employeeid, string? language) + public async Task GetHistoricSurveysAsync(int? employeeid, string? language) { var result = await this.surveyResponseProvider.GetHistoricSurveysAsync(employeeid, language); if (result.IsSuccess) diff --git a/DamageAssesmentApi/DamageAssesment.Api.Responses/Interfaces/ISurveyServiceProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Responses/Interfaces/ISurveyServiceProvider.cs index 60de3bd..8c66bda 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Responses/Interfaces/ISurveyServiceProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Responses/Interfaces/ISurveyServiceProvider.cs @@ -4,7 +4,7 @@ namespace DamageAssesment.Api.Responses.Interfaces { public interface ISurveyServiceProvider { - Task> getSurveysAsync(string token); + Task> getSurveysAsync(string language,string token); Task getSurveyAsync(int surveyId,string token); } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Responses/Interfaces/ISurveysResponse.cs b/DamageAssesmentApi/DamageAssesment.Api.Responses/Interfaces/ISurveysResponse.cs index dea50eb..19893c8 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Responses/Interfaces/ISurveysResponse.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Responses/Interfaces/ISurveysResponse.cs @@ -9,8 +9,8 @@ namespace DamageAssesment.Api.Responses.Interfaces 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, 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); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs index 8220197..00f030e 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs @@ -125,25 +125,18 @@ namespace DamageAssesment.Api.Responses.Providers return (false, null, ex.Message); } } - - public async Task<(bool IsSuccess, dynamic Surveys, string ErrorMessage)> GetActiveSurveysAsync(int employeeid, string language) + 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) + var surveys = await surveyServiceProvider.getSurveysAsync(language, token); + surveys = surveys.Where(s => s.IsEnabled == true && s.StartDate <= DateTime.Now && s.EndDate >= DateTime.Now).ToList(); + if (employeeid == null || employeeid == 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)); + List listOfsurveysId = await surveyResponseDbContext.SurveyResponses.Where(x => x.EmployeeId == employeeid.Value).Select(y => y.SurveyId).ToListAsync(); + var activeSurveys = surveys.Where(s => !listOfsurveysId.Contains(s.Id)); return (true, activeSurveys, null); } catch (Exception ex) @@ -153,21 +146,24 @@ namespace DamageAssesment.Api.Responses.Providers } } - public async Task<(bool IsSuccess, dynamic Surveys, string ErrorMessage)> GetHistoricSurveysAsync(int employeeid, string language) + 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 surveys = await surveyServiceProvider.getSurveysAsync(language,token); + // returning only historic data: end date is less than current date. + surveys = surveys.Where(s => s.EndDate < DateTime.Now).ToList(); + if (employeeid == null || employeeid == 0) + return (true, surveys, null); + var surveyResponses = await surveyResponseDbContext.SurveyResponses.Where(x => x.EmployeeId == employeeid).ToListAsync(); var historicSurveys = from s in surveys from r in surveyResponses where s.Id == r.SurveyId select s; - return (true, historicSurveys, null); + } catch (Exception ex) { diff --git a/DamageAssesmentApi/DamageAssesment.Api.Responses/Services/SurveyServiceProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Responses/Services/SurveyServiceProvider.cs index 2e9732e..1a30be0 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Responses/Services/SurveyServiceProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Responses/Services/SurveyServiceProvider.cs @@ -10,10 +10,12 @@ namespace DamageAssesment.Api.Responses.Services { } - public async Task> getSurveysAsync(string token) + public async Task> getSurveysAsync(string language, string token) { try { + if (!string.IsNullOrEmpty(language)) + url = url + "/" + language; var responseJsonString = await httpUtil.SendAsync(HttpMethod.Get, url, null, token); var surveys = JsonConvert.DeserializeObject>(responseJsonString); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs index 3332f6d..5acb580 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs @@ -93,8 +93,8 @@ namespace DamageAssesment.Api.Surveys.Providers try { logger?.LogInformation("Get all Surveys from DB"); - var surveys = await surveyDbContext.Surveys.Where(s => s.IsEnabled == true).ToListAsync(); - + //checking is enabled in survey response + var surveys = await surveyDbContext.Surveys.ToListAsync();//Where(s => s.IsEnabled == true) if (surveys != null) { surveysList = from s in surveys