Update Question and Survey to support multilingual functionnality and Make language optional in URL

This commit is contained in:
Reginald Cherenfant Jasmin 2023-08-25 08:55:11 -04:00
parent 22261f42ca
commit 47b0c7b202
16 changed files with 321 additions and 134 deletions

View File

@ -1,8 +1,4 @@
using DamageAssesment.Api.Questions.Db; using DamageAssesment.Api.Questions.Interfaces;
using DamageAssesment.Api.Questions.Interfaces;
using DamageAssesment.Api.Questions.Models;
using DamageAssesment.Api.Questions.Providers;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace DamageAssesment.Api.Questions.Controllers namespace DamageAssesment.Api.Questions.Controllers
@ -20,10 +16,12 @@ namespace DamageAssesment.Api.Questions.Controllers
} }
// get all questions // get all questions
[HttpGet("Questions")] [Route("{Language}/Questions")]
public async Task<IActionResult> GetQuestionsAsync() [Route("Questions")]
[HttpGet]
public async Task<IActionResult> GetQuestionsAsync(string? Language)
{ {
var result = await this.questionsProvider.GetQuestionsAsync(); var result = await this.questionsProvider.GetQuestionsAsync(Language);
if (result.IsSuccess) if (result.IsSuccess)
{ {
return Ok(result.Questions); return Ok(result.Questions);
@ -31,10 +29,12 @@ namespace DamageAssesment.Api.Questions.Controllers
return NoContent(); return NoContent();
} }
//Get questions based on question id //Get questions based on question id
[HttpGet("Questions/{id}")] [Route("{Language}/Questions/{id}")]
public async Task<IActionResult> GetQuestionAsync(int id) [Route("Questions/{id}")]
[HttpGet]
public async Task<IActionResult> GetQuestionAsync(int id, string? Language)
{ {
var result = await this.questionsProvider.GetQuestionAsync(id); var result = await this.questionsProvider.GetQuestionAsync(id,Language);
if (result.IsSuccess) if (result.IsSuccess)
{ {
return Ok(result.Question); return Ok(result.Question);
@ -42,10 +42,11 @@ namespace DamageAssesment.Api.Questions.Controllers
return NotFound(); return NotFound();
} }
//get all questions based on survey id //get all questions based on survey id
[HttpGet("GetSurveyQuestions/{surveyId}")] [Route("{Language}/GetSurveyQuestions/{surveyId}")]
[Route("GetSurveyQuestions/{surveyId}")]
[HttpGet]
public async Task<IActionResult> GetSurveyQuestions(int surveyId,string? Language) public async Task<IActionResult> GetSurveyQuestions(int surveyId,string? Language)
{ {
if (string.IsNullOrEmpty(Language)) Language = "en";
var result = await this.questionsProvider.GetSurveyQuestionAsync(surveyId, Language); var result = await this.questionsProvider.GetSurveyQuestionAsync(surveyId, Language);
if (result.IsSuccess) if (result.IsSuccess)
{ {

View File

@ -4,8 +4,8 @@ namespace DamageAssesment.Api.Questions.Interfaces
{ {
public interface IQuestionsProvider : IQuestionTypesProvider public interface IQuestionsProvider : IQuestionTypesProvider
{ {
Task<(bool IsSuccess, Models.Question Question, string ErrorMessage)> GetQuestionAsync(int Id); Task<(bool IsSuccess, Models.Question Question, string ErrorMessage)> GetQuestionAsync(int Id, string Language);
Task<(bool IsSuccess, IEnumerable<Models.Question> Questions, string ErrorMessage)> GetQuestionsAsync(); Task<(bool IsSuccess, IEnumerable<Models.Question> Questions, string ErrorMessage)> GetQuestionsAsync(string Language);
Task<(bool IsSuccess, List<SurveyQuestions> SurveyQuestions, string ErrorMessage)> GetSurveyQuestionAsync(int surveyId,string Language); Task<(bool IsSuccess, List<SurveyQuestions> SurveyQuestions, string ErrorMessage)> GetSurveyQuestionAsync(int surveyId,string Language);
Task<(bool IsSuccess, Models.Question Question, string ErrorMessage)> PostQuestionAsync(Models.Question Question); Task<(bool IsSuccess, Models.Question Question, string ErrorMessage)> PostQuestionAsync(Models.Question Question);
Task<(bool IsSuccess, Models.Question Question, string ErrorMessage)> UpdateQuestionAsync(Models.Question Question); Task<(bool IsSuccess, Models.Question Question, string ErrorMessage)> UpdateQuestionAsync(Models.Question Question);

View File

@ -71,7 +71,7 @@ namespace DamageAssesment.Api.Questions.Providers
} }
public async Task<(bool IsSuccess, IEnumerable<Models.Question> Questions, string ErrorMessage)> GetQuestionsAsync() public async Task<(bool IsSuccess, IEnumerable<Models.Question> Questions, string ErrorMessage)> GetQuestionsAsync(string Language)
{ {
try try
{ {
@ -82,11 +82,21 @@ namespace DamageAssesment.Api.Questions.Providers
//logger?.LogInformation($"{question} customer(s) found"); //logger?.LogInformation($"{question} customer(s) found");
var result = mapper.Map<IEnumerable<Db.Question>, IEnumerable<Models.Question>>(questions); var result = mapper.Map<IEnumerable<Db.Question>, IEnumerable<Models.Question>>(questions);
foreach (var question in result) foreach (var question in result)
{
if (string.IsNullOrEmpty(Language))
{ {
question.Questions = mapper.Map<List<Db.QuestionsTranslation>, List<Models.QuestionsTranslation>>( question.Questions = mapper.Map<List<Db.QuestionsTranslation>, List<Models.QuestionsTranslation>>(
questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == question.Id).ToList()); questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == question.Id).ToList());
} }
else
{
question.Questions = mapper.Map<List<Db.QuestionsTranslation>, List<Models.QuestionsTranslation>>(
questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == question.Id && a.Language == Language).ToList());
}
}
return (true, result, null); return (true, result, null);
} }
return (false, null, "Not found"); return (false, null, "Not found");
@ -97,7 +107,7 @@ namespace DamageAssesment.Api.Questions.Providers
return (false, null, ex.Message); return (false, null, ex.Message);
} }
} }
public async Task<(bool IsSuccess, Models.Question Question, string ErrorMessage)> GetQuestionAsync(int Id) public async Task<(bool IsSuccess, Models.Question Question, string ErrorMessage)> GetQuestionAsync(int Id, string Language)
{ {
try try
{ {
@ -107,8 +117,17 @@ namespace DamageAssesment.Api.Questions.Providers
{ {
logger?.LogInformation($"{question} customer(s) found"); logger?.LogInformation($"{question} customer(s) found");
var result = mapper.Map<Db.Question, Models.Question>(question); var result = mapper.Map<Db.Question, Models.Question>(question);
if (string.IsNullOrEmpty(Language))
{
result.Questions = mapper.Map<List<Db.QuestionsTranslation>, List<Models.QuestionsTranslation>>( result.Questions = mapper.Map<List<Db.QuestionsTranslation>, List<Models.QuestionsTranslation>>(
questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == result.Id).ToList()); questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == result.Id).ToList());
}
else
{
result.Questions = mapper.Map<List<Db.QuestionsTranslation>, List<Models.QuestionsTranslation>>(
questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == result.Id && a.Language == Language).ToList());
}
return (true, result, null); return (true, result, null);
} }
return (false, null, "Not found"); return (false, null, "Not found");
@ -120,12 +139,23 @@ namespace DamageAssesment.Api.Questions.Providers
} }
} }
public List<Models.Question> GetSurveyQuestion(List<Models.Question> questions, string Language) public List<Models.Question> GetSurveyQuestion(List<Models.Question> questions, string Language)
{
if (string.IsNullOrEmpty(Language))
{
foreach (var item in questions)
{
item.Questions = mapper.Map<List<Db.QuestionsTranslation>, List<Models.QuestionsTranslation>>(
questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == item.Id).ToList());
}
}
else
{ {
foreach (var item in questions) foreach (var item in questions)
{ {
item.Questions = mapper.Map<List<Db.QuestionsTranslation>, List<Models.QuestionsTranslation>>( item.Questions = mapper.Map<List<Db.QuestionsTranslation>, List<Models.QuestionsTranslation>>(
questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == item.Id && a.Language == Language).ToList()); questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == item.Id && a.Language == Language).ToList());
} }
}
return questions; return questions;
} }
public async Task<(bool IsSuccess, List<SurveyQuestions> SurveyQuestions, string ErrorMessage)> GetSurveyQuestionAsync(int SurveyId, string Language) public async Task<(bool IsSuccess, List<SurveyQuestions> SurveyQuestions, string ErrorMessage)> GetSurveyQuestionAsync(int SurveyId, string Language)

View File

@ -1,16 +1,7 @@
using AutoMapper;
using DamageAssesment.Api.Questions.Controllers; using DamageAssesment.Api.Questions.Controllers;
using DamageAssesment.Api.Questions.Db;
using DamageAssesment.Api.Questions.Interfaces; using DamageAssesment.Api.Questions.Interfaces;
using DamageAssesment.Api.Questions.Models;
using DamageAssesment.Api.Questions.Profiles;
using DamageAssesment.Api.Questions.Providers;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Moq; using Moq;
using Newtonsoft.Json;
using Xunit; using Xunit;
namespace DamageAssesment.Api.Questions.Test namespace DamageAssesment.Api.Questions.Test
@ -22,10 +13,10 @@ namespace DamageAssesment.Api.Questions.Test
{ {
var mockQuestionService = new Mock<IQuestionsProvider>(); var mockQuestionService = new Mock<IQuestionsProvider>();
var mockResponse = await MockData.getOkResponse(); var mockResponse = await MockData.getOkResponse();
mockQuestionService.Setup(service => service.GetQuestionsAsync()).ReturnsAsync(mockResponse); mockQuestionService.Setup(service => service.GetQuestionsAsync(null)).ReturnsAsync(mockResponse);
var QuestionProvider = new QuestionsController(mockQuestionService.Object); var QuestionProvider = new QuestionsController(mockQuestionService.Object);
var result = (OkObjectResult)await QuestionProvider.GetQuestionsAsync(); var result = (OkObjectResult)await QuestionProvider.GetQuestionsAsync(null);
Assert.Equal(200, result.StatusCode); Assert.Equal(200, result.StatusCode);
} }
@ -35,10 +26,10 @@ namespace DamageAssesment.Api.Questions.Test
{ {
var mockQuestionService = new Mock<IQuestionsProvider>(); var mockQuestionService = new Mock<IQuestionsProvider>();
var mockResponse = await MockData.getNoContentResponse(); var mockResponse = await MockData.getNoContentResponse();
mockQuestionService.Setup(service => service.GetQuestionsAsync()).ReturnsAsync(mockResponse); mockQuestionService.Setup(service => service.GetQuestionsAsync(null)).ReturnsAsync(mockResponse);
var QuestionProvider = new QuestionsController(mockQuestionService.Object); var QuestionProvider = new QuestionsController(mockQuestionService.Object);
var result = (NoContentResult)await QuestionProvider.GetQuestionsAsync(); var result = (NoContentResult)await QuestionProvider.GetQuestionsAsync(null);
Assert.Equal(204, result.StatusCode); Assert.Equal(204, result.StatusCode);
} }
@ -48,10 +39,10 @@ namespace DamageAssesment.Api.Questions.Test
{ {
var mockQuestionService = new Mock<IQuestionsProvider>(); var mockQuestionService = new Mock<IQuestionsProvider>();
var mockResponse = await MockData.getOkResponse(1); var mockResponse = await MockData.getOkResponse(1);
mockQuestionService.Setup(service => service.GetQuestionAsync(1)).ReturnsAsync(mockResponse); mockQuestionService.Setup(service => service.GetQuestionAsync(1,null)).ReturnsAsync(mockResponse);
var QuestionProvider = new QuestionsController(mockQuestionService.Object); var QuestionProvider = new QuestionsController(mockQuestionService.Object);
var result = (OkObjectResult)await QuestionProvider.GetQuestionAsync(1); var result = (OkObjectResult)await QuestionProvider.GetQuestionAsync(1,null);
Assert.Equal(200, result.StatusCode); Assert.Equal(200, result.StatusCode);
} }
@ -61,10 +52,10 @@ namespace DamageAssesment.Api.Questions.Test
{ {
var mockQuestionService = new Mock<IQuestionsProvider>(); var mockQuestionService = new Mock<IQuestionsProvider>();
var mockResponse = await MockData.getNotFoundResponse(); var mockResponse = await MockData.getNotFoundResponse();
mockQuestionService.Setup(service => service.GetQuestionAsync(99)).ReturnsAsync(mockResponse); mockQuestionService.Setup(service => service.GetQuestionAsync(99,null)).ReturnsAsync(mockResponse);
var QuestionProvider = new QuestionsController(mockQuestionService.Object); var QuestionProvider = new QuestionsController(mockQuestionService.Object);
var result = (NotFoundResult)await QuestionProvider.GetQuestionAsync(99); var result = (NotFoundResult)await QuestionProvider.GetQuestionAsync(99,null);
Assert.Equal(404, result.StatusCode); Assert.Equal(404, result.StatusCode);
} }

View File

@ -4,25 +4,11 @@ namespace DamageAssesment.Api.SurveyResponses.Models
{ {
public class Survey public class Survey
{ {
[Key]
public int Id { get; set; } public int Id { get; set; }
[StringLength(100)]
public string Title { get; set; }
//[StringLength(1000)]
//public string Description { get; set; }
public bool IsEnabled { get; set; } public bool IsEnabled { get; set; }
public DateTime? StartDate { get; set; } public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; } public DateTime? EndDate { get; set; }
public DateTime CreatedDate { get; set; }
//public DateTime CreatedDate { get; set; } public IEnumerable<SurveyTranslation> Titles { get; set; }
//[StringLength(6)]
//public string EmployeeID { get; set; }
} }
} }

View File

@ -0,0 +1,13 @@
namespace DamageAssesment.Api.SurveyResponses.Models
{
public class SurveyTranslation
{
public string Title { get; set; }
public string Language { get; set; }
}
}

View File

@ -15,7 +15,7 @@ namespace DamageAssesment.Api.Survey.Test
for (int i = 0; i < 10; i++) for (int i = 0; i < 10; i++)
{ {
list.Append(new Surveys.Models.Survey { Id = i, Title = "Survey Title - " + i }); list.Append(new Surveys.Models.Survey { Id = i, /*Title = "Survey Title - " + i */});
} }
return (true, list, null); return (true, list, null);
} }
@ -45,7 +45,7 @@ namespace DamageAssesment.Api.Survey.Test
public static async Task<Surveys.Models.Survey> getInputSurveyData() public static async Task<Surveys.Models.Survey> getInputSurveyData()
{ {
return new Surveys.Models.Survey { Id = 100, Title = "Mock survey", IsEnabled= true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) }; return new Surveys.Models.Survey { Id = 100, /*Title = "Mock survey",*/ IsEnabled= true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) };
} }

View File

@ -14,10 +14,10 @@ namespace DamageAssesment.Api.Surveys.Test
{ {
var mockSurveyService = new Mock<ISurveyProvider>(); var mockSurveyService = new Mock<ISurveyProvider>();
var mockResponse = await MockData.getOkResponse(); var mockResponse = await MockData.getOkResponse();
mockSurveyService.Setup(service => service.GetSurveysAsync()).ReturnsAsync(mockResponse); mockSurveyService.Setup(service => service.GetSurveysAsync(null)).ReturnsAsync(mockResponse);
var surveyProvider = new SurveysController(mockSurveyService.Object); var surveyProvider = new SurveysController(mockSurveyService.Object);
var result = (OkObjectResult) await surveyProvider.GetSurveysAsync(); var result = (OkObjectResult) await surveyProvider.GetSurveysAsync(null);
Assert.Equal(200, result.StatusCode); Assert.Equal(200, result.StatusCode);
} }
@ -27,10 +27,10 @@ namespace DamageAssesment.Api.Surveys.Test
{ {
var mockSurveyService = new Mock<ISurveyProvider>(); var mockSurveyService = new Mock<ISurveyProvider>();
var mockResponse = await MockData.getNoContentResponse(); var mockResponse = await MockData.getNoContentResponse();
mockSurveyService.Setup(service => service.GetSurveysAsync()).ReturnsAsync(mockResponse); mockSurveyService.Setup(service => service.GetSurveysAsync(null)).ReturnsAsync(mockResponse);
var surveyProvider = new SurveysController(mockSurveyService.Object); var surveyProvider = new SurveysController(mockSurveyService.Object);
var result = (NoContentResult)await surveyProvider.GetSurveysAsync(); var result = (NoContentResult)await surveyProvider.GetSurveysAsync(null);
Assert.Equal(204, result.StatusCode); Assert.Equal(204, result.StatusCode);
} }
@ -40,10 +40,10 @@ namespace DamageAssesment.Api.Surveys.Test
{ {
var mockSurveyService = new Mock<ISurveyProvider>(); var mockSurveyService = new Mock<ISurveyProvider>();
var mockResponse = await MockData.getOkResponse(1); var mockResponse = await MockData.getOkResponse(1);
mockSurveyService.Setup(service => service.GetSurveysAsync(1)).ReturnsAsync(mockResponse); mockSurveyService.Setup(service => service.GetSurveysAsync(1,null)).ReturnsAsync(mockResponse);
var surveyProvider = new SurveysController(mockSurveyService.Object); var surveyProvider = new SurveysController(mockSurveyService.Object);
var result = (OkObjectResult)await surveyProvider.GetSurveysAsync(1); var result = (OkObjectResult)await surveyProvider.GetSurveysAsync(1,null);
Assert.Equal(200, result.StatusCode); Assert.Equal(200, result.StatusCode);
} }
@ -53,10 +53,10 @@ namespace DamageAssesment.Api.Surveys.Test
{ {
var mockSurveyService = new Mock<ISurveyProvider>(); var mockSurveyService = new Mock<ISurveyProvider>();
var mockResponse = await MockData.getNotFoundResponse(); var mockResponse = await MockData.getNotFoundResponse();
mockSurveyService.Setup(service => service.GetSurveysAsync(99)).ReturnsAsync(mockResponse); mockSurveyService.Setup(service => service.GetSurveysAsync(99,null)).ReturnsAsync(mockResponse);
var surveyProvider = new SurveysController(mockSurveyService.Object); var surveyProvider = new SurveysController(mockSurveyService.Object);
var result = (NotFoundResult)await surveyProvider.GetSurveysAsync(99); var result = (NotFoundResult)await surveyProvider.GetSurveysAsync(99,null);
Assert.Equal(404, result.StatusCode); Assert.Equal(404, result.StatusCode);
} }

View File

@ -5,7 +5,7 @@ using Microsoft.AspNetCore.Mvc;
namespace DamageAssesment.Api.Surveys.Controllers namespace DamageAssesment.Api.Surveys.Controllers
{ {
[Route("api/[controller]")] [Route("api")]
[ApiController] [ApiController]
public class SurveysController : ControllerBase public class SurveysController : ControllerBase
{ {
@ -16,10 +16,13 @@ namespace DamageAssesment.Api.Surveys.Controllers
this.surveyProvider = surveyProvider; this.surveyProvider = surveyProvider;
} }
[Route("Surveys")]
[Route("{Language}/Surveys")]
[HttpGet] [HttpGet]
public async Task<ActionResult> GetSurveysAsync() public async Task<ActionResult> GetSurveysAsync(string? Language)
{ {
var result = await this.surveyProvider.GetSurveysAsync(); var result = await this.surveyProvider.GetSurveysAsync(Language);
if (result.IsSuccess) if (result.IsSuccess)
{ {
return Ok(result.Surveys); return Ok(result.Surveys);
@ -27,10 +30,12 @@ namespace DamageAssesment.Api.Surveys.Controllers
return NoContent(); return NoContent();
} }
[HttpGet("{Id}")] [Route("Surveys/{Id}")]
public async Task<ActionResult> GetSurveysAsync(int Id) [Route("{Language}/Surveys/{Id}")]
[HttpGet]
public async Task<ActionResult> GetSurveysAsync(int Id, string? Language)
{ {
var result = await this.surveyProvider.GetSurveysAsync(Id); var result = await this.surveyProvider.GetSurveysAsync(Id, Language);
if (result.IsSuccess) if (result.IsSuccess)
{ {
return Ok(result.Surveys); return Ok(result.Surveys);
@ -38,7 +43,7 @@ namespace DamageAssesment.Api.Surveys.Controllers
return NotFound(); return NotFound();
} }
[HttpPost] [HttpPost("Surveys")]
public async Task<ActionResult> PostSurveysAsync(Models.Survey survey) public async Task<ActionResult> PostSurveysAsync(Models.Survey survey)
{ {
var result = await this.surveyProvider.PostSurveyAsync(survey); var result = await this.surveyProvider.PostSurveyAsync(survey);
@ -49,7 +54,7 @@ namespace DamageAssesment.Api.Surveys.Controllers
return BadRequest(result.ErrorMessage); return BadRequest(result.ErrorMessage);
} }
[HttpPut("{Id}")] [HttpPut("Surveys/{Id}")]
public async Task<ActionResult> PutSurveysAsync(int Id, Models.Survey survey) public async Task<ActionResult> PutSurveysAsync(int Id, Models.Survey survey)
{ {
var result = await this.surveyProvider.PutSurveyAsync(Id, survey); var result = await this.surveyProvider.PutSurveyAsync(Id, survey);
@ -63,7 +68,7 @@ namespace DamageAssesment.Api.Surveys.Controllers
return BadRequest(result.ErrorMessage); return BadRequest(result.ErrorMessage);
} }
[HttpDelete("{Id}")] [HttpDelete("Surveys/{Id}")]
public async Task<ActionResult> DeleteSurveysAsync(int Id) public async Task<ActionResult> DeleteSurveysAsync(int Id)
{ {
var result = await this.surveyProvider.DeleteSurveyAsync(Id); var result = await this.surveyProvider.DeleteSurveyAsync(Id);

View File

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace DamageAssesment.Api.Surveys.Db namespace DamageAssesment.Api.Surveys.Db
{ {
@ -6,11 +7,6 @@ namespace DamageAssesment.Api.Surveys.Db
{ {
[Key] [Key]
public int Id { get; set; } public int Id { get; set; }
[StringLength(100)]
[Required]
public string Title { get; set; }
//[StringLength(1000)] //[StringLength(1000)]
//public string Description { get; set; } //public string Description { get; set; }
@ -20,9 +16,11 @@ namespace DamageAssesment.Api.Surveys.Db
public DateTime? EndDate { get; set; } public DateTime? EndDate { get; set; }
//public DateTime CreatedDate { get; set; } public DateTime CreatedDate { get; set; } = DateTime.Now;
/*
//[StringLength(6)] [StringLength(10)]
//public string EmployeeID { get; set; } [ForeignKey("Employee")]
public string EmployeeId { get; set; }
*/
} }
} }

View File

@ -0,0 +1,23 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace DamageAssesment.Api.Surveys.Db
{
public class SurveyTranslation
{
[Key]
public int Id { get; set; }
[ForeignKey("Survey")]
public int SurveyId { get; set; }
[StringLength(200)]
[Required]
public string Title { get; set; }
public string Language { get; set; }
}
}

View File

@ -9,6 +9,6 @@ namespace DamageAssesment.Api.Surveys.Db
} }
public DbSet<Db.Survey> Surveys { get; set; } public DbSet<Db.Survey> Surveys { get; set; }
public DbSet<Db.SurveyTranslation> SurveysTranslation { get; set; }
} }
} }

View File

@ -2,8 +2,8 @@
{ {
public interface ISurveyProvider public interface ISurveyProvider
{ {
Task<(bool IsSuccess, IEnumerable< Models.Survey> Surveys, string ErrorMessage)> GetSurveysAsync(); Task<(bool IsSuccess, IEnumerable< Models.Survey> Surveys, string ErrorMessage)> GetSurveysAsync(string Language);
Task<(bool IsSuccess, Models.Survey Surveys, string ErrorMessage)> GetSurveysAsync(int Id); Task<(bool IsSuccess, Models.Survey Surveys, string ErrorMessage)> GetSurveysAsync(int Id, string Language);
Task<(bool IsSuccess, Models.Survey Survey, string ErrorMessage)> PostSurveyAsync(Models.Survey Survey); Task<(bool IsSuccess, Models.Survey Survey, string ErrorMessage)> PostSurveyAsync(Models.Survey Survey);
Task<(bool IsSuccess, Models.Survey Survey, string ErrorMessage)> PutSurveyAsync(int Id,Models.Survey Survey); Task<(bool IsSuccess, Models.Survey Survey, string ErrorMessage)> PutSurveyAsync(int Id,Models.Survey Survey);
Task<(bool IsSuccess, Models.Survey Survey, string ErrorMessage)> DeleteSurveyAsync(int Id); Task<(bool IsSuccess, Models.Survey Survey, string ErrorMessage)> DeleteSurveyAsync(int Id);

View File

@ -4,25 +4,11 @@ namespace DamageAssesment.Api.Surveys.Models
{ {
public class Survey public class Survey
{ {
[Key]
public int Id { get; set; } public int Id { get; set; }
[StringLength(100)]
public string Title { get; set; }
//[StringLength(1000)]
// public string? Description { get; set; }
public bool IsEnabled { get; set; } public bool IsEnabled { get; set; }
public DateTime? StartDate { get; set; } public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; } public DateTime? EndDate { get; set; }
public DateTime CreatedDate { get; set; }
//public DateTime CreatedDate { get; set; } public IEnumerable<SurveyTranslation> Titles { get; set; }
//[StringLength(6)]
//public string EmployeeID { get; set; }
} }
} }

View File

@ -0,0 +1,13 @@
namespace DamageAssesment.Api.Surveys.Models
{
public class SurveyTranslation
{
public string Title { get; set; }
public string Language { get; set; }
}
}

View File

@ -1,7 +1,9 @@
using AutoMapper; using AutoMapper;
using DamageAssesment.Api.Surveys.Db; using DamageAssesment.Api.Surveys.Db;
using DamageAssesment.Api.Surveys.Interfaces; using DamageAssesment.Api.Surveys.Interfaces;
using DamageAssesment.Api.Surveys.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
namespace DamageAssesment.Api.Surveys.Providers namespace DamageAssesment.Api.Surveys.Providers
{ {
@ -23,24 +25,88 @@ namespace DamageAssesment.Api.Surveys.Providers
{ {
if (!surveyDbContext.Surveys.Any()) if (!surveyDbContext.Surveys.Any())
{ {
surveyDbContext.Surveys.Add(new Db.Survey { Id = 1, Title = "Sample Survey Title:Damage Assesment 2014", IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) }); surveyDbContext.Surveys.Add(new Db.Survey { Id = 1, IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90), CreatedDate = DateTime.Now });
surveyDbContext.Surveys.Add(new Db.Survey { Id = 2, Title = "Sample Survey Title: Damage Assesment 2016", IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) }); surveyDbContext.Surveys.Add(new Db.Survey { Id = 2, IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90), CreatedDate = DateTime.Now });
surveyDbContext.Surveys.Add(new Db.Survey { Id = 3, Title = "Sample Survey Title: Damage Assesment 2018", IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) }); surveyDbContext.Surveys.Add(new Db.Survey { Id = 3, IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90), CreatedDate = DateTime.Now });
surveyDbContext.SaveChangesAsync();
}
if (!surveyDbContext.SurveysTranslation.Any())
{
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { Id = 1, SurveyId = 1, Language = "en", Title = "Impact of Tropical Storm Emily on Florida's Economy" });
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { Id = 2, SurveyId = 1, Language = "es", Title = "Impacto de la tormenta tropical Emily en la economía de Florida" });
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { Id = 3, SurveyId = 1, Language = "fr", Title = "Impact de la tempête tropicale Emily sur l'économie de la Floride" });
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { Id = 4, SurveyId = 2, Language = "en", Title = "Hurricane Andrew Aftermath Survey" });
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { Id = 5, SurveyId = 2, Language = "es", Title = "Encuesta sobre las secuelas del huracán Andrew" });
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { Id = 6, SurveyId = 2, Language = "fr", Title = "Enquête sur les conséquences de l'ouragan Andrew" });
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { Id = 7, SurveyId = 3, Language = "en", Title = "Public Perception of Hurricane Michael's Response" });
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { Id = 8, SurveyId = 3, Language = "es", Title = "Percepción pública de la respuesta del huracán Michael" });
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { Id = 9, SurveyId = 3, Language = "fr", Title = "Perception du public de la réponse de l'ouragan Michael" });
surveyDbContext.SaveChangesAsync(); surveyDbContext.SaveChangesAsync();
} }
} }
public async Task<(bool IsSuccess, IEnumerable<Models.Survey> Surveys, string ErrorMessage)> GetSurveysAsync() public async Task<(bool IsSuccess, IEnumerable<Models.Survey> Surveys, string ErrorMessage)> GetSurveysAsync(string Language)
{ {
IEnumerable<Models.Survey> surveysList = null;
try try
{ {
logger?.LogInformation("Gell all Surveys from DB"); logger?.LogInformation("Gell all Surveys from DB");
var surveys = await surveyDbContext.Surveys.ToListAsync(); var surveys = await surveyDbContext.Surveys.Where(s => s.IsEnabled == true).ToListAsync();
var surveyTranslations = await surveyDbContext.SurveysTranslation.ToListAsync();
if (surveys != null) if (surveys != null)
{ {
if (string.IsNullOrEmpty(Language))
{
surveysList = from s in surveys
select new
Models.Survey
{
Id = s.Id,
StartDate = s.StartDate,
EndDate = s.EndDate,
IsEnabled = s.IsEnabled,
CreatedDate = s.CreatedDate,
Titles = from t in surveyTranslations
where t.SurveyId == s.Id
select new Models.SurveyTranslation
{
Title = t.Title,
Language = t.Language
}
};
}
else
{
surveysList = from s in surveys
select new
Models.Survey
{
Id = s.Id,
StartDate = s.StartDate,
EndDate = s.EndDate,
IsEnabled = s.IsEnabled,
CreatedDate = s.CreatedDate,
Titles = from t in surveyTranslations
where t.SurveyId == s.Id
&& t.Language == Language
select new Models.SurveyTranslation
{
Title = t.Title,
Language = t.Language
}
};
}
logger?.LogInformation($"{surveys.Count} Items(s) found"); logger?.LogInformation($"{surveys.Count} Items(s) found");
var result = mapper.Map<IEnumerable<Db.Survey>, IEnumerable<Models.Survey>>(surveys); return (true, surveysList, null);
return (true, result, null);
} }
return (false, null, "Not found"); return (false, null, "Not found");
} }
@ -50,16 +116,55 @@ namespace DamageAssesment.Api.Surveys.Providers
return (false, null, ex.Message); return (false, null, ex.Message);
} }
} }
public async Task<(bool IsSuccess, Models.Survey Surveys, string ErrorMessage)> GetSurveysAsync(int Id) public async Task<(bool IsSuccess, Models.Survey Surveys, string ErrorMessage)> GetSurveysAsync(int Id, string Language)
{ {
try try
{ {
logger?.LogInformation("Query Survey"); logger?.LogInformation("Query Survey");
var survey = await surveyDbContext.Surveys.SingleOrDefaultAsync(s => s.Id == Id); var survey = await surveyDbContext.Surveys.SingleOrDefaultAsync(s => s.Id == Id && s.IsEnabled == true);
if (survey != null) if (survey != null)
{ {
Models.Survey result = null;
var surveyTranslations = await surveyDbContext.SurveysTranslation.Where(s => s.SurveyId == survey.Id).ToListAsync();
if (string.IsNullOrEmpty(Language))
{
result = new Models.Survey
{
Id = survey.Id,
StartDate = survey.StartDate,
EndDate = survey.EndDate,
IsEnabled = survey.IsEnabled,
CreatedDate = survey.CreatedDate,
Titles = from t in surveyTranslations
select new Models.SurveyTranslation
{
Title = t.Title,
Language = t.Language
}
};
}
else
{
result = new Models.Survey
{
Id = survey.Id,
StartDate = survey.StartDate,
EndDate = survey.EndDate,
IsEnabled = survey.IsEnabled,
CreatedDate = survey.CreatedDate,
Titles = from t in surveyTranslations
where t.Language == Language
select new Models.SurveyTranslation
{
Title = t.Title,
Language = t.Language
}
};
}
logger?.LogInformation($"Survey Id: {Id} found"); logger?.LogInformation($"Survey Id: {Id} found");
var result = mapper.Map<Db.Survey, Models.Survey>(survey);
return (true, result, null); return (true, result, null);
} }
return (false, null, "Not found"); return (false, null, "Not found");
@ -78,8 +183,16 @@ namespace DamageAssesment.Api.Surveys.Providers
if (survey != null) if (survey != null)
{ {
var surveys = await surveyDbContext.Surveys.ToListAsync(); var surveys = await surveyDbContext.Surveys.ToListAsync();
survey.Id = surveys.Count + 1;
surveyDbContext.Surveys.Add(mapper.Map<Models.Survey, Db.Survey>(survey)); int Id = surveys.Count + 1;
surveyDbContext.Surveys.Add(new Db.Survey { Id = Id, IsEnabled = survey.IsEnabled, StartDate = survey.StartDate, EndDate = survey.EndDate, CreatedDate = DateTime.Now });
var surveyTranslations = await surveyDbContext.SurveysTranslation.ToListAsync();
int count = surveyTranslations.Count;
foreach (var title in survey.Titles)
{
count++;
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { Id = count, SurveyId = Id, Language = title.Language, Title = title.Title });
}
await surveyDbContext.SaveChangesAsync(); await surveyDbContext.SaveChangesAsync();
return (true, survey, "Successful"); return (true, survey, "Successful");
} }
@ -106,12 +219,40 @@ namespace DamageAssesment.Api.Surveys.Providers
if (_survey != null) if (_survey != null)
{ {
_survey.Title = survey.Title; var surveysTranslation = await surveyDbContext.SurveysTranslation.Where(s => s.SurveyId == Id).ToListAsync();
surveyDbContext.SurveysTranslation.RemoveRange(surveysTranslation);
await surveyDbContext.SaveChangesAsync();
_survey.IsEnabled = survey.IsEnabled; _survey.IsEnabled = survey.IsEnabled;
_survey.StartDate = survey.StartDate; _survey.StartDate = survey.StartDate;
_survey.EndDate = survey.EndDate; _survey.EndDate = survey.EndDate;
await surveyDbContext.SaveChangesAsync(); await surveyDbContext.SaveChangesAsync();
return (true, mapper.Map<Db.Survey, Models.Survey>(_survey), "Successful");
List<Db.SurveyTranslation> listSurveyTranslation = new List<Db.SurveyTranslation>();
Random random = new Random();
foreach (var title in survey.Titles)
{
listSurveyTranslation.Add(new Db.SurveyTranslation { Id = random.Next(), SurveyId = _survey.Id, Language = title.Language, Title = title.Title });
}
surveyDbContext.SurveysTranslation.AddRange(listSurveyTranslation);
await surveyDbContext.SaveChangesAsync();
var result = new Models.Survey
{
Id = Id,
StartDate = survey.StartDate,
EndDate = survey.EndDate,
IsEnabled = survey.IsEnabled,
CreatedDate = survey.CreatedDate,
Titles = from t in listSurveyTranslation
select new Models.SurveyTranslation
{
Title = t.Title,
Language = t.Language
}
};
return (true, result, "Successful");
} }
else else
{ {