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 b93120b..f2a7bad 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Controllers/SurveysController.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Controllers/SurveysController.cs @@ -27,7 +27,36 @@ namespace DamageAssesment.Api.Surveys.Controllers } return NoContent(); } - + /// + /// GET request for retrieving all active surveys. + /// + [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. + /// + [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 4fd2f20..d4fa5e3 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) {