From 556dc5e4e77eda2576c3226df5d4572356128dc5 Mon Sep 17 00:00:00 2001 From: Vijay Uppu <913468@dadeschools.net> Date: Tue, 19 Dec 2023 13:16:17 -0500 Subject: [PATCH] added active and in active survey endpoints --- .../SurveyServiceTest.cs | 48 +++++++++++++++++++ .../Controllers/SurveysController.cs | 33 ++++++++++++- .../Interfaces/ISurveyProvider.cs | 1 + .../Providers/SurveysProvider.cs | 37 ++++++++++++++ 4 files changed, 118 insertions(+), 1 deletion(-) diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys.Test/SurveyServiceTest.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys.Test/SurveyServiceTest.cs index 44f1f6e..ecbb6a1 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys.Test/SurveyServiceTest.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys.Test/SurveyServiceTest.cs @@ -34,7 +34,55 @@ namespace DamageAssesment.Api.Surveys.Test Assert.Equal(204, result.StatusCode); } + [Fact(DisplayName = "Get active Surveys - Ok case")] + public async Task GetActiveSurveysAsync_ShouldReturnStatusCode200() + { + var mockSurveyService = new Mock(); + var mockResponse = await MockData.getOkResponse(); + mockSurveyService.Setup(service => service.GetActiveSurveysAsync(true,null)).ReturnsAsync(mockResponse); + var surveyProvider = new SurveysController(mockSurveyService.Object); + var result = (OkObjectResult)await surveyProvider.GetActiveSurveysAsync(null); + + Assert.Equal(200, result.StatusCode); + } + + [Fact(DisplayName = "Get active Surveys - NoContent Case")] + public async Task GetActiveSurveysAsync_ShouldReturnStatusCode204() + { + var mockSurveyService = new Mock(); + var mockResponse = await MockData.getNoContentResponse(); + mockSurveyService.Setup(service => service.GetActiveSurveysAsync(true,null)).ReturnsAsync(mockResponse); + + var surveyProvider = new SurveysController(mockSurveyService.Object); + var result = (NoContentResult)await surveyProvider.GetActiveSurveysAsync(null); + + Assert.Equal(204, result.StatusCode); + } + [Fact(DisplayName = "Get inactive Surveys - Ok case")] + public async Task GetInActiveSurveysAsync_ShouldReturnStatusCode200() + { + var mockSurveyService = new Mock(); + var mockResponse = await MockData.getOkResponse(); + mockSurveyService.Setup(service => service.GetActiveSurveysAsync(false, null)).ReturnsAsync(mockResponse); + + var surveyProvider = new SurveysController(mockSurveyService.Object); + var result = (OkObjectResult)await surveyProvider.GetInActiveSurveysAsync(null); + + Assert.Equal(200, result.StatusCode); + } + [Fact(DisplayName = "Get in active Surveys - NoContent Case")] + public async Task GetInActiveSurveysAsync_ShouldReturnStatusCode204() + { + var mockSurveyService = new Mock(); + var mockResponse = await MockData.getNoContentResponse(); + mockSurveyService.Setup(service => service.GetActiveSurveysAsync(false, null)).ReturnsAsync(mockResponse); + + var surveyProvider = new SurveysController(mockSurveyService.Object); + var result = (NoContentResult)await surveyProvider.GetInActiveSurveysAsync(null); + + Assert.Equal(204, result.StatusCode); + } [Fact(DisplayName = "Get Survey by Id - Ok case")] public async Task GetSurveyAsync_ShouldReturnStatusCode200() { diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Controllers/SurveysController.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Controllers/SurveysController.cs index 2a4415c..aa5b625 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Controllers/SurveysController.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Controllers/SurveysController.cs @@ -29,7 +29,38 @@ namespace DamageAssesment.Api.Surveys.Controllers } return NoContent(); } - + /// + /// GET request for retrieving all active surveys. + /// + [Authorize(Roles = "admin,survey,user,report")] + [Route("surveys/active")] + [Route("surveys/active/{language:alpha}")] + [HttpGet] + public async Task GetActiveSurveysAsync(string? language) + { + var result = await this.surveyProvider.GetActiveSurveysAsync(true,language); + if (result.IsSuccess) + { + return Ok(result.Surveys); + } + return NoContent(); + } + /// + /// GET request for retrieving all inactive surveys. + /// + [Authorize(Roles = "admin,survey,user,report")] + [Route("surveys/inactive")] + [Route("surveys/inactive/{language:alpha}")] + [HttpGet] + public async Task GetInActiveSurveysAsync(string? language) + { + var result = await this.surveyProvider.GetActiveSurveysAsync(false, language); + if (result.IsSuccess) + { + return Ok(result.Surveys); + } + return NoContent(); + } /// /// GET request for retrieving surveys by ID. /// diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Interfaces/ISurveyProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Interfaces/ISurveyProvider.cs index 22b2910..8764e9d 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Interfaces/ISurveyProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Interfaces/ISurveyProvider.cs @@ -3,6 +3,7 @@ public interface ISurveyProvider { Task<(bool IsSuccess, IEnumerable< Models.MultiLanSurvey> Surveys, string ErrorMessage)> GetSurveysAsync(string language); + Task<(bool IsSuccess, IEnumerable Surveys, string ErrorMessage)> GetActiveSurveysAsync(bool IsActive,string language); Task<(bool IsSuccess, Models.MultiLanSurvey Surveys, string ErrorMessage)> GetSurveysAsync(int id, string language); Task<(bool IsSuccess, Models.MultiLanSurvey Survey, string ErrorMessage)> PostSurveyAsync(Models.Survey Survey); Task<(bool IsSuccess, Models.MultiLanSurvey Survey, string ErrorMessage)> PutSurveyAsync(int id, Models.Survey Survey); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs index d58cb17..0707ec2 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs @@ -101,6 +101,43 @@ namespace DamageAssesment.Api.Surveys.Providers return SurveyStatus.INACTIVE.ToString(); } } + + // Method to get surveys asynchronously with multi-language support + public async Task<(bool IsSuccess, IEnumerable Surveys, string ErrorMessage)> GetActiveSurveysAsync(bool IsActive,string language) + { + IEnumerable surveysList = null; + try + { + string status = SurveyStatus.ACTIVE.ToString(); + if(!IsActive) status = SurveyStatus.INACTIVE.ToString(); + logger?.LogInformation("Get all Surveys from DB"); + //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 + select new Models.MultiLanSurvey + { + Id = s.Id, + StartDate = s.StartDate, + EndDate = s.EndDate, + IsEnabled = s.IsEnabled, + CreatedDate = s.CreatedDate, + Status = GetStatus(s.StartDate, s.EndDate), + Titles = CreateMultiLanguageObject(GetSurveyTranslations(s.Id, null, language)) + }; + logger?.LogInformation($"{surveys.Count} Items(s) found"); + return (true, surveysList.Where(a=>a.Status==status).OrderByDescending(a=>a.Id), null); + } + return (false, null, "Not found"); + } + catch (Exception ex) + { + logger?.LogError(ex.ToString()); + return (false, null, ex.Message); + } + } // Method to get surveys asynchronously with multi-language support public async Task<(bool IsSuccess, IEnumerable Surveys, string ErrorMessage)> GetSurveysAsync(string language) {