Update Question and Survey to support multilingual functionnality and Make language optional in URL
This commit is contained in:
parent
22261f42ca
commit
47b0c7b202
@ -1,8 +1,4 @@
|
||||
using DamageAssesment.Api.Questions.Db;
|
||||
using DamageAssesment.Api.Questions.Interfaces;
|
||||
using DamageAssesment.Api.Questions.Models;
|
||||
using DamageAssesment.Api.Questions.Providers;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using DamageAssesment.Api.Questions.Interfaces;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace DamageAssesment.Api.Questions.Controllers
|
||||
@ -20,10 +16,12 @@ namespace DamageAssesment.Api.Questions.Controllers
|
||||
|
||||
}
|
||||
// get all questions
|
||||
[HttpGet("Questions")]
|
||||
public async Task<IActionResult> GetQuestionsAsync()
|
||||
[Route("{Language}/Questions")]
|
||||
[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)
|
||||
{
|
||||
return Ok(result.Questions);
|
||||
@ -31,10 +29,12 @@ namespace DamageAssesment.Api.Questions.Controllers
|
||||
return NoContent();
|
||||
}
|
||||
//Get questions based on question id
|
||||
[HttpGet("Questions/{id}")]
|
||||
public async Task<IActionResult> GetQuestionAsync(int id)
|
||||
[Route("{Language}/Questions/{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)
|
||||
{
|
||||
return Ok(result.Question);
|
||||
@ -42,10 +42,11 @@ namespace DamageAssesment.Api.Questions.Controllers
|
||||
return NotFound();
|
||||
}
|
||||
//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)
|
||||
{
|
||||
if (string.IsNullOrEmpty(Language)) Language = "en";
|
||||
var result = await this.questionsProvider.GetSurveyQuestionAsync(surveyId, Language);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
|
@ -4,8 +4,8 @@ namespace DamageAssesment.Api.Questions.Interfaces
|
||||
{
|
||||
public interface IQuestionsProvider : IQuestionTypesProvider
|
||||
{
|
||||
Task<(bool IsSuccess, Models.Question Question, string ErrorMessage)> GetQuestionAsync(int Id);
|
||||
Task<(bool IsSuccess, IEnumerable<Models.Question> Questions, string ErrorMessage)> GetQuestionsAsync();
|
||||
Task<(bool IsSuccess, Models.Question Question, string ErrorMessage)> GetQuestionAsync(int Id, string Language);
|
||||
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, Models.Question Question, string ErrorMessage)> PostQuestionAsync(Models.Question Question);
|
||||
Task<(bool IsSuccess, Models.Question Question, string ErrorMessage)> UpdateQuestionAsync(Models.Question Question);
|
||||
|
@ -31,7 +31,7 @@ namespace DamageAssesment.Api.Questions.Providers
|
||||
|
||||
if (!questionDbContext.QuestionsTranslations.Any())
|
||||
{
|
||||
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() {Id=1, QuestionId = 1, QuestionText = "Can You Open ?",Language="en" });
|
||||
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 1, QuestionId = 1, QuestionText = "Can You Open ?", Language = "en" });
|
||||
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 2, QuestionId = 1, QuestionText = "Peux-tu ouvrir ?", Language = "fr" });
|
||||
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 3, QuestionId = 2, QuestionText = "Are the grounds flodded ?", Language = "en" });
|
||||
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 4, QuestionId = 2, QuestionText = "Les terrains sont-ils inondés ?", Language = "fr" });
|
||||
@ -44,7 +44,7 @@ namespace DamageAssesment.Api.Questions.Providers
|
||||
}
|
||||
if (!questionDbContext.Questions.Any())
|
||||
{
|
||||
questionDbContext.Questions.Add(new Db.Question() { Id = 1, QuestionTypeId = 2, SurveyId = 1, QuestionNumber = 1, IsRequired = true, Comment = false, Key = true, QuestionGroup = "group1",CategoryId=1 });
|
||||
questionDbContext.Questions.Add(new Db.Question() { Id = 1, QuestionTypeId = 2, SurveyId = 1, QuestionNumber = 1, IsRequired = true, Comment = false, Key = true, QuestionGroup = "group1", CategoryId = 1 });
|
||||
questionDbContext.Questions.Add(new Db.Question() { Id = 2, QuestionTypeId = 1, SurveyId = 1, QuestionNumber = 2, IsRequired = false, Comment = true, Key = false, QuestionGroup = "group1", CategoryId = 1 });
|
||||
questionDbContext.Questions.Add(new Db.Question() { Id = 3, QuestionTypeId = 1, SurveyId = 1, QuestionNumber = 3, IsRequired = true, Comment = false, Key = true, QuestionGroup = "group1", CategoryId = 2 });
|
||||
questionDbContext.SaveChanges();
|
||||
@ -61,7 +61,7 @@ namespace DamageAssesment.Api.Questions.Providers
|
||||
|
||||
if (!questionDbContext.QuestionCategories.Any())
|
||||
{
|
||||
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 1, CategoryName = "Category 1", CategoryImage="img1" });
|
||||
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 1, CategoryName = "Category 1", CategoryImage = "img1" });
|
||||
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 2, CategoryName = "Category 2", CategoryImage = "img1" });
|
||||
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 3, CategoryName = "Category 3", CategoryImage = "img1" });
|
||||
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 4, CategoryName = "Category 4", CategoryImage = "img1" });
|
||||
@ -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
|
||||
{
|
||||
@ -82,10 +82,20 @@ namespace DamageAssesment.Api.Questions.Providers
|
||||
|
||||
//logger?.LogInformation($"{question} customer(s) found");
|
||||
var result = mapper.Map<IEnumerable<Db.Question>, IEnumerable<Models.Question>>(questions);
|
||||
|
||||
|
||||
foreach (var question in result)
|
||||
{
|
||||
question.Questions=mapper.Map<List<Db.QuestionsTranslation>,List<Models.QuestionsTranslation>>(
|
||||
questionDbContext.QuestionsTranslations.Where(a=>a.QuestionId==question.Id).ToList());
|
||||
if (string.IsNullOrEmpty(Language))
|
||||
{
|
||||
question.Questions = mapper.Map<List<Db.QuestionsTranslation>, List<Models.QuestionsTranslation>>(
|
||||
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);
|
||||
}
|
||||
@ -97,7 +107,7 @@ namespace DamageAssesment.Api.Questions.Providers
|
||||
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
|
||||
{
|
||||
@ -107,8 +117,17 @@ namespace DamageAssesment.Api.Questions.Providers
|
||||
{
|
||||
logger?.LogInformation($"{question} customer(s) found");
|
||||
var result = mapper.Map<Db.Question, Models.Question>(question);
|
||||
|
||||
if (string.IsNullOrEmpty(Language))
|
||||
{
|
||||
result.Questions = mapper.Map<List<Db.QuestionsTranslation>, List<Models.QuestionsTranslation>>(
|
||||
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 (false, null, "Not found");
|
||||
@ -119,12 +138,23 @@ namespace DamageAssesment.Api.Questions.Providers
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
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 && a.Language== Language).ToList());
|
||||
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)
|
||||
{
|
||||
item.Questions = mapper.Map<List<Db.QuestionsTranslation>, List<Models.QuestionsTranslation>>(
|
||||
questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == item.Id && a.Language == Language).ToList());
|
||||
}
|
||||
}
|
||||
return questions;
|
||||
}
|
||||
@ -133,12 +163,12 @@ namespace DamageAssesment.Api.Questions.Providers
|
||||
try
|
||||
{
|
||||
logger?.LogInformation("Query Question");
|
||||
var questions = await questionDbContext.Questions.Include("QuestionType").Where(a=>a.SurveyId==SurveyId).AsNoTracking().ToListAsync();
|
||||
var questions = await questionDbContext.Questions.Include("QuestionType").Where(a => a.SurveyId == SurveyId).AsNoTracking().ToListAsync();
|
||||
if (questions != null)
|
||||
{
|
||||
List<SurveyQuestions> surveyQuestionsList = new List<SurveyQuestions>();
|
||||
List<int> CategoryIds=questions.Select(a=>a.CategoryId).Distinct().ToList();
|
||||
var questioncategories = await questionDbContext.QuestionCategories.Where(a =>CategoryIds.Contains(a.Id)).ToListAsync();
|
||||
List<int> CategoryIds = questions.Select(a => a.CategoryId).Distinct().ToList();
|
||||
var questioncategories = await questionDbContext.QuestionCategories.Where(a => CategoryIds.Contains(a.Id)).ToListAsync();
|
||||
//logger?.LogInformation($"{question} customer(s) found");
|
||||
foreach (var item in questioncategories)
|
||||
{
|
||||
@ -169,7 +199,7 @@ namespace DamageAssesment.Api.Questions.Providers
|
||||
logger?.LogInformation("Query Question");
|
||||
var dbquestion = mapper.Map<Models.Question, Db.Question>(Question);
|
||||
var dbquestiontranslation = mapper.Map<List<Models.QuestionsTranslation>, List<Db.QuestionsTranslation>>(Question.Questions);
|
||||
dbquestion.QuestionTypeId=questionDbContext.QuestionTypes.Where(a=>a.TypeText==Question.TypeText).Select(a=>a.Id).FirstOrDefault();
|
||||
dbquestion.QuestionTypeId = questionDbContext.QuestionTypes.Where(a => a.TypeText == Question.TypeText).Select(a => a.Id).FirstOrDefault();
|
||||
questionDbContext.Questions.Add(dbquestion);
|
||||
dbquestiontranslation.ForEach(i => i.QuestionId = dbquestion.Id);
|
||||
questionDbContext.QuestionsTranslations.AddRange(dbquestiontranslation);
|
||||
@ -192,7 +222,7 @@ namespace DamageAssesment.Api.Questions.Providers
|
||||
dbquestion.QuestionTypeId = questionDbContext.QuestionTypes.Where(a => a.TypeText == Question.TypeText).Select(a => a.Id).FirstOrDefault();
|
||||
questionDbContext.Entry(dbquestion).State = EntityState.Modified;
|
||||
var oldquestions = questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == dbquestion.Id).ToList();
|
||||
if(oldquestions!=null)
|
||||
if (oldquestions != null)
|
||||
questionDbContext.QuestionsTranslations.RemoveRange(oldquestions);
|
||||
dbquestiontranslation.ForEach(i => i.QuestionId = dbquestion.Id);
|
||||
questionDbContext.QuestionsTranslations.AddRange(dbquestiontranslation);
|
||||
@ -284,7 +314,7 @@ namespace DamageAssesment.Api.Questions.Providers
|
||||
// Question.QuestionType = GetQuestionType(Question.QuestionTypeId);
|
||||
questionDbContext.QuestionCategories.Add(dbQuestionCategory);
|
||||
questionDbContext.SaveChanges();
|
||||
QuestionCategory.Id=dbQuestionCategory.Id;
|
||||
QuestionCategory.Id = dbQuestionCategory.Id;
|
||||
return (true, QuestionCategory, null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
@ -1,16 +1,7 @@
|
||||
|
||||
using AutoMapper;
|
||||
using DamageAssesment.Api.Questions.Controllers;
|
||||
using DamageAssesment.Api.Questions.Db;
|
||||
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.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
||||
using Moq;
|
||||
using Newtonsoft.Json;
|
||||
using Xunit;
|
||||
|
||||
namespace DamageAssesment.Api.Questions.Test
|
||||
@ -22,10 +13,10 @@ namespace DamageAssesment.Api.Questions.Test
|
||||
{
|
||||
var mockQuestionService = new Mock<IQuestionsProvider>();
|
||||
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 result = (OkObjectResult)await QuestionProvider.GetQuestionsAsync();
|
||||
var result = (OkObjectResult)await QuestionProvider.GetQuestionsAsync(null);
|
||||
|
||||
Assert.Equal(200, result.StatusCode);
|
||||
}
|
||||
@ -35,10 +26,10 @@ namespace DamageAssesment.Api.Questions.Test
|
||||
{
|
||||
var mockQuestionService = new Mock<IQuestionsProvider>();
|
||||
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 result = (NoContentResult)await QuestionProvider.GetQuestionsAsync();
|
||||
var result = (NoContentResult)await QuestionProvider.GetQuestionsAsync(null);
|
||||
|
||||
Assert.Equal(204, result.StatusCode);
|
||||
}
|
||||
@ -48,10 +39,10 @@ namespace DamageAssesment.Api.Questions.Test
|
||||
{
|
||||
var mockQuestionService = new Mock<IQuestionsProvider>();
|
||||
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 result = (OkObjectResult)await QuestionProvider.GetQuestionAsync(1);
|
||||
var result = (OkObjectResult)await QuestionProvider.GetQuestionAsync(1,null);
|
||||
|
||||
Assert.Equal(200, result.StatusCode);
|
||||
}
|
||||
@ -61,10 +52,10 @@ namespace DamageAssesment.Api.Questions.Test
|
||||
{
|
||||
var mockQuestionService = new Mock<IQuestionsProvider>();
|
||||
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 result = (NotFoundResult)await QuestionProvider.GetQuestionAsync(99);
|
||||
var result = (NotFoundResult)await QuestionProvider.GetQuestionAsync(99,null);
|
||||
Assert.Equal(404, result.StatusCode);
|
||||
}
|
||||
|
||||
|
@ -4,25 +4,11 @@ namespace DamageAssesment.Api.SurveyResponses.Models
|
||||
{
|
||||
public class Survey
|
||||
{
|
||||
[Key]
|
||||
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 DateTime? StartDate { get; set; }
|
||||
|
||||
public DateTime? EndDate { get; set; }
|
||||
|
||||
//public DateTime CreatedDate { get; set; }
|
||||
|
||||
//[StringLength(6)]
|
||||
//public string EmployeeID { get; set; }
|
||||
|
||||
public DateTime CreatedDate { get; set; }
|
||||
public IEnumerable<SurveyTranslation> Titles { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,13 @@
|
||||
namespace DamageAssesment.Api.SurveyResponses.Models
|
||||
{
|
||||
public class SurveyTranslation
|
||||
{
|
||||
public string Title { get; set; }
|
||||
public string Language { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -15,7 +15,7 @@ namespace DamageAssesment.Api.Survey.Test
|
||||
|
||||
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);
|
||||
}
|
||||
@ -45,7 +45,7 @@ namespace DamageAssesment.Api.Survey.Test
|
||||
|
||||
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) };
|
||||
|
||||
}
|
||||
|
||||
|
@ -14,10 +14,10 @@ namespace DamageAssesment.Api.Surveys.Test
|
||||
{
|
||||
var mockSurveyService = new Mock<ISurveyProvider>();
|
||||
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 result = (OkObjectResult) await surveyProvider.GetSurveysAsync();
|
||||
var result = (OkObjectResult) await surveyProvider.GetSurveysAsync(null);
|
||||
|
||||
Assert.Equal(200, result.StatusCode);
|
||||
}
|
||||
@ -27,10 +27,10 @@ namespace DamageAssesment.Api.Surveys.Test
|
||||
{
|
||||
var mockSurveyService = new Mock<ISurveyProvider>();
|
||||
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 result = (NoContentResult)await surveyProvider.GetSurveysAsync();
|
||||
var result = (NoContentResult)await surveyProvider.GetSurveysAsync(null);
|
||||
|
||||
Assert.Equal(204, result.StatusCode);
|
||||
}
|
||||
@ -40,10 +40,10 @@ namespace DamageAssesment.Api.Surveys.Test
|
||||
{
|
||||
var mockSurveyService = new Mock<ISurveyProvider>();
|
||||
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 result = (OkObjectResult)await surveyProvider.GetSurveysAsync(1);
|
||||
var result = (OkObjectResult)await surveyProvider.GetSurveysAsync(1,null);
|
||||
|
||||
Assert.Equal(200, result.StatusCode);
|
||||
}
|
||||
@ -53,10 +53,10 @@ namespace DamageAssesment.Api.Surveys.Test
|
||||
{
|
||||
var mockSurveyService = new Mock<ISurveyProvider>();
|
||||
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 result = (NotFoundResult)await surveyProvider.GetSurveysAsync(99);
|
||||
var result = (NotFoundResult)await surveyProvider.GetSurveysAsync(99,null);
|
||||
Assert.Equal(404, result.StatusCode);
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace DamageAssesment.Api.Surveys.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[Route("api")]
|
||||
[ApiController]
|
||||
public class SurveysController : ControllerBase
|
||||
{
|
||||
@ -16,10 +16,13 @@ namespace DamageAssesment.Api.Surveys.Controllers
|
||||
this.surveyProvider = surveyProvider;
|
||||
}
|
||||
|
||||
|
||||
[Route("Surveys")]
|
||||
[Route("{Language}/Surveys")]
|
||||
[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)
|
||||
{
|
||||
return Ok(result.Surveys);
|
||||
@ -27,10 +30,12 @@ namespace DamageAssesment.Api.Surveys.Controllers
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpGet("{Id}")]
|
||||
public async Task<ActionResult> GetSurveysAsync(int Id)
|
||||
[Route("Surveys/{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)
|
||||
{
|
||||
return Ok(result.Surveys);
|
||||
@ -38,7 +43,7 @@ namespace DamageAssesment.Api.Surveys.Controllers
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[HttpPost("Surveys")]
|
||||
public async Task<ActionResult> PostSurveysAsync(Models.Survey survey)
|
||||
{
|
||||
var result = await this.surveyProvider.PostSurveyAsync(survey);
|
||||
@ -49,10 +54,10 @@ namespace DamageAssesment.Api.Surveys.Controllers
|
||||
return BadRequest(result.ErrorMessage);
|
||||
}
|
||||
|
||||
[HttpPut("{Id}")]
|
||||
[HttpPut("Surveys/{Id}")]
|
||||
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);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.Survey);
|
||||
@ -63,7 +68,7 @@ namespace DamageAssesment.Api.Surveys.Controllers
|
||||
return BadRequest(result.ErrorMessage);
|
||||
}
|
||||
|
||||
[HttpDelete("{Id}")]
|
||||
[HttpDelete("Surveys/{Id}")]
|
||||
public async Task<ActionResult> DeleteSurveysAsync(int Id)
|
||||
{
|
||||
var result = await this.surveyProvider.DeleteSurveyAsync(Id);
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace DamageAssesment.Api.Surveys.Db
|
||||
{
|
||||
@ -6,11 +7,6 @@ namespace DamageAssesment.Api.Surveys.Db
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
|
||||
[StringLength(100)]
|
||||
[Required]
|
||||
public string Title { get; set; }
|
||||
|
||||
//[StringLength(1000)]
|
||||
//public string Description { get; set; }
|
||||
|
||||
@ -20,9 +16,11 @@ namespace DamageAssesment.Api.Surveys.Db
|
||||
|
||||
public DateTime? EndDate { get; set; }
|
||||
|
||||
//public DateTime CreatedDate { get; set; }
|
||||
|
||||
//[StringLength(6)]
|
||||
//public string EmployeeID { get; set; }
|
||||
public DateTime CreatedDate { get; set; } = DateTime.Now;
|
||||
/*
|
||||
[StringLength(10)]
|
||||
[ForeignKey("Employee")]
|
||||
public string EmployeeId { get; set; }
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -9,6 +9,6 @@ namespace DamageAssesment.Api.Surveys.Db
|
||||
|
||||
}
|
||||
public DbSet<Db.Survey> Surveys { get; set; }
|
||||
|
||||
public DbSet<Db.SurveyTranslation> SurveysTranslation { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,8 @@
|
||||
{
|
||||
public interface ISurveyProvider
|
||||
{
|
||||
Task<(bool IsSuccess, IEnumerable< Models.Survey> Surveys, string ErrorMessage)> GetSurveysAsync();
|
||||
Task<(bool IsSuccess, Models.Survey Surveys, string ErrorMessage)> GetSurveysAsync(int Id);
|
||||
Task<(bool IsSuccess, IEnumerable< Models.Survey> Surveys, string ErrorMessage)> GetSurveysAsync(string Language);
|
||||
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)> PutSurveyAsync(int Id,Models.Survey Survey);
|
||||
Task<(bool IsSuccess, Models.Survey Survey, string ErrorMessage)> DeleteSurveyAsync(int Id);
|
||||
|
@ -4,25 +4,11 @@ namespace DamageAssesment.Api.Surveys.Models
|
||||
{
|
||||
public class Survey
|
||||
{
|
||||
[Key]
|
||||
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 DateTime? StartDate { get; set; }
|
||||
|
||||
public DateTime? EndDate { get; set; }
|
||||
|
||||
//public DateTime CreatedDate { get; set; }
|
||||
|
||||
//[StringLength(6)]
|
||||
//public string EmployeeID { get; set; }
|
||||
|
||||
public DateTime CreatedDate { get; set; }
|
||||
public IEnumerable<SurveyTranslation> Titles { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,13 @@
|
||||
namespace DamageAssesment.Api.Surveys.Models
|
||||
{
|
||||
public class SurveyTranslation
|
||||
{
|
||||
public string Title { get; set; }
|
||||
public string Language { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -1,7 +1,9 @@
|
||||
using AutoMapper;
|
||||
using DamageAssesment.Api.Surveys.Db;
|
||||
using DamageAssesment.Api.Surveys.Interfaces;
|
||||
using DamageAssesment.Api.Surveys.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace DamageAssesment.Api.Surveys.Providers
|
||||
{
|
||||
@ -23,24 +25,88 @@ namespace DamageAssesment.Api.Surveys.Providers
|
||||
{
|
||||
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 = 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 = 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 = 1, IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90), CreatedDate = DateTime.Now });
|
||||
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, 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();
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
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 (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");
|
||||
var result = mapper.Map<IEnumerable<Db.Survey>, IEnumerable<Models.Survey>>(surveys);
|
||||
return (true, result, null);
|
||||
return (true, surveysList, null);
|
||||
}
|
||||
return (false, null, "Not found");
|
||||
}
|
||||
@ -50,16 +116,55 @@ namespace DamageAssesment.Api.Surveys.Providers
|
||||
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
|
||||
{
|
||||
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)
|
||||
{
|
||||
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");
|
||||
var result = mapper.Map<Db.Survey, Models.Survey>(survey);
|
||||
return (true, result, null);
|
||||
}
|
||||
return (false, null, "Not found");
|
||||
@ -78,8 +183,16 @@ namespace DamageAssesment.Api.Surveys.Providers
|
||||
if (survey != null)
|
||||
{
|
||||
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();
|
||||
return (true, survey, "Successful");
|
||||
}
|
||||
@ -106,12 +219,40 @@ namespace DamageAssesment.Api.Surveys.Providers
|
||||
|
||||
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.StartDate = survey.StartDate;
|
||||
_survey.EndDate = survey.EndDate;
|
||||
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
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user