diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Controllers/QuestionsController.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Controllers/QuestionsController.cs index ae98924..19c32bc 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Controllers/QuestionsController.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Controllers/QuestionsController.cs @@ -3,7 +3,6 @@ using Microsoft.AspNetCore.Mvc; namespace DamageAssesment.Api.Questions.Controllers { - [Route("api")] [ApiController] public class QuestionsController : ControllerBase { @@ -21,12 +20,12 @@ namespace DamageAssesment.Api.Questions.Controllers /// // get all questions - [Route("{Language}/Questions")] [Route("Questions")] + [Route("Questions/{language:alpha}")] [HttpGet] - public async Task GetQuestionsAsync(string? Language) + public async Task GetQuestionsAsync(string? language) { - var result = await this.questionsProvider.GetQuestionsAsync(Language); + var result = await this.questionsProvider.GetQuestionsAsync(language); if (result.IsSuccess) { return Ok(result.Questions); @@ -38,12 +37,12 @@ namespace DamageAssesment.Api.Questions.Controllers /// /// GET request for retrieving a question by ID. /// - [Route("{Language}/Questions/{id}")] - [Route("Questions/{id}")] + [Route("Questions/{id}/{language:alpha}")] + [Route("Questions/{id:int}")] [HttpGet] - public async Task GetQuestionAsync(int id, string? Language) + public async Task GetQuestionByIdAsync(string? language,int id) { - var result = await this.questionsProvider.GetQuestionAsync(id,Language); + var result = await this.questionsProvider.GetQuestionAsync(id, language); if (result.IsSuccess) { return Ok(result.Question); @@ -56,12 +55,12 @@ namespace DamageAssesment.Api.Questions.Controllers /// GET request for retrieving survey questions based on a survey ID. /// Uri: {Optional language}/GetSurveyQuestions/{surveyId} :Default returns question in all languages /// - [Route("{Language}/GetSurveyQuestions/{surveyId}")] - [Route("GetSurveyQuestions/{surveyId}")] + [Route("Questions/BySurvey/{surveyId:int}")] + [Route("Questions/BySurvey/{surveyId:int}/{language:alpha}")] [HttpGet] - public async Task GetSurveyQuestions(int surveyId,string? Language) + public async Task GetSurveyQuestions(int surveyId,string? language) { - var result = await this.questionsProvider.GetSurveyQuestionAsync(surveyId, Language); + var result = await this.questionsProvider.GetSurveyQuestionAsync(surveyId, language); if (result.IsSuccess) { return Ok(result.SurveyQuestions); @@ -126,10 +125,11 @@ namespace DamageAssesment.Api.Questions.Controllers /// GET request for retrieving question categories. /// - [HttpGet("QuestionCategories")] - public async Task GetQuestionCategoriesAsync() + [HttpGet("Questions/Categories")] + [HttpGet("Questions/Categories/{language:alpha}")] + public async Task GetQuestionCategoriesAsync(string? language) { - var result = await this.questionsProvider.GetQuestionCategoriesAsync(); + var result = await this.questionsProvider.GetQuestionCategoriesAsync(language); if (result.IsSuccess) { return Ok(result.QuestionCategories); @@ -140,10 +140,11 @@ namespace DamageAssesment.Api.Questions.Controllers /// GET request for retrieving a question category by ID. /// - [HttpGet("QuestionCategories/{id}")] - public async Task GetQuestionCategoryAsync(int id) + [HttpGet("Questions/Categories/{id:int}")] + [HttpGet("Questions/Categories/{id:int}/{language:alpha}")] + public async Task GetQuestionCategoryAsync(int id,string? language) { - var result = await this.questionsProvider.GetQuestionCategoryAsync(id); + var result = await this.questionsProvider.GetQuestionCategoryAsync(id, language); if (result.IsSuccess) { return Ok(result.QuestionCategory); @@ -156,7 +157,7 @@ namespace DamageAssesment.Api.Questions.Controllers /// PUT request for updating a question category. /// - [HttpPut("QuestionCategories")] + [HttpPut("Questions/Categories")] public async Task UpdateQuestionCategory(Models.QuestionCategory questionCategory) { if (questionCategory != null) @@ -177,7 +178,7 @@ namespace DamageAssesment.Api.Questions.Controllers /// POST request for creating a new question category. /// - [HttpPost("QuestionCategories")] + [HttpPost("Questions/Categories")] public async Task CreateQuestionCategory(Models.QuestionCategory questionCategory) { if (questionCategory != null) @@ -195,7 +196,7 @@ namespace DamageAssesment.Api.Questions.Controllers /// DELETE request for deleting a question category based on ID. /// - [HttpDelete("QuestionCategories/{id}")] + [HttpDelete("Questions/Categories/{id}")] public async Task DeleteQuestionCategory(int id) { var result = await this.questionsProvider.DeleteQuestionCategoryAsync(id); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Db/CategoryTranslation.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Db/CategoryTranslation.cs new file mode 100644 index 0000000..b6e21fa --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Db/CategoryTranslation.cs @@ -0,0 +1,15 @@ +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +namespace DamageAssesment.Api.Questions.Db +{ + public class CategoryTranslation + { + [Key] + public int Id { get; set; } + [ForeignKey("QuestionCategory")] + public int CategoryId { get; set; } + public string Title { get; set; } + public string Language { get; set; } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Db/QuestionCategory.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Db/QuestionCategory.cs index 06fc296..117f770 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Db/QuestionCategory.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Db/QuestionCategory.cs @@ -7,8 +7,8 @@ namespace DamageAssesment.Api.Questions.Db { [Key] public int Id { get; set; } - public string CategoryName { get; set; } - public string CategoryImage { get; set; } + public string IconName { get; set; } + public string IconLibrary { get; set; } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Db/QuestionDbContext.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Db/QuestionDbContext.cs index a62485e..01e3c3d 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Db/QuestionDbContext.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Db/QuestionDbContext.cs @@ -10,6 +10,7 @@ namespace DamageAssesment.Api.Questions.Db public DbSet QuestionTypes { get; set; } public DbSet QuestionsTranslations { get; set; } public DbSet QuestionCategories { get; set; } + public DbSet CategoryTranslations { get; set; } public QuestionDbContext(DbContextOptions options) : base(options) { @@ -29,6 +30,9 @@ namespace DamageAssesment.Api.Questions.Db modelBuilder.Entity() .Property(item => item.Id) .ValueGeneratedOnAdd(); + modelBuilder.Entity() + .Property(item => item.Id) + .ValueGeneratedOnAdd(); } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Interfaces/IQuestionsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Interfaces/IQuestionsProvider.cs index 58def79..59a805b 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Interfaces/IQuestionsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Interfaces/IQuestionsProvider.cs @@ -4,19 +4,19 @@ namespace DamageAssesment.Api.Questions.Interfaces { public interface IQuestionsProvider : IQuestionTypesProvider { - Task<(bool IsSuccess, Models.Question Question, string ErrorMessage)> GetQuestionAsync(int Id, string Language); - Task<(bool IsSuccess, IEnumerable Questions, string ErrorMessage)> GetQuestionsAsync(string Language); - Task<(bool IsSuccess, List 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); - Task<(bool IsSuccess, Models.Question Question, string ErrorMessage)> DeleteQuestionAsync(int Id); + Task<(bool IsSuccess, Models.MultiLanQuestion Question, string ErrorMessage)> GetQuestionAsync(int id, string language); + Task<(bool IsSuccess, IEnumerable Questions, string ErrorMessage)> GetQuestionsAsync(string language); + Task<(bool IsSuccess, List SurveyQuestions, string ErrorMessage)> GetSurveyQuestionAsync(int surveyId,string language); + Task<(bool IsSuccess, Models.MultiLanQuestion Question, string ErrorMessage)> PostQuestionAsync(Models.Question Question); + Task<(bool IsSuccess, Models.MultiLanQuestion Question, string ErrorMessage)> UpdateQuestionAsync(Models.Question Question); + Task<(bool IsSuccess, Models.MultiLanQuestion Question, string ErrorMessage)> DeleteQuestionAsync(int id); - Task<(bool IsSuccess, IEnumerable QuestionCategories, string ErrorMessage)> GetQuestionCategoriesAsync(); - Task<(bool IsSuccess, Models.QuestionCategory QuestionCategory, string ErrorMessage)> GetQuestionCategoryAsync(int Id); - Task<(bool IsSuccess, Models.QuestionCategory QuestionCategory, string ErrorMessage)> PostQuestionCategoryAsync(Models.QuestionCategory QuestionCategory); - Task<(bool IsSuccess, Models.QuestionCategory QuestionCategory, string ErrorMessage)> UpdateQuestionCategoryAsync(Models.QuestionCategory QuestionCategory); - Task<(bool IsSuccess, Models.QuestionCategory QuestionCategory, string ErrorMessage)> DeleteQuestionCategoryAsync(int Id); + Task<(bool IsSuccess, IEnumerable QuestionCategories, string ErrorMessage)> GetQuestionCategoriesAsync(string? language); + Task<(bool IsSuccess, Models.MultiLanQuestionCategory QuestionCategory, string ErrorMessage)> GetQuestionCategoryAsync(int id, string? language); + Task<(bool IsSuccess, Models.MultiLanQuestionCategory QuestionCategory, string ErrorMessage)> PostQuestionCategoryAsync(Models.QuestionCategory QuestionCategory); + Task<(bool IsSuccess, Models.MultiLanQuestionCategory QuestionCategory, string ErrorMessage)> UpdateQuestionCategoryAsync(Models.QuestionCategory QuestionCategory); + Task<(bool IsSuccess, Models.MultiLanQuestionCategory QuestionCategory, string ErrorMessage)> DeleteQuestionCategoryAsync(int id); void SeedData(); } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Models/CategoryTranslation.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Models/CategoryTranslation.cs new file mode 100644 index 0000000..0c60d6b --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Models/CategoryTranslation.cs @@ -0,0 +1,8 @@ +namespace DamageAssesment.Api.Questions.Models +{ + public class CategoryTranslation + { + public string Title { get; set; } + public string Language { get; set; } + } +} diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Models/Question.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Models/Question.cs index b694f5b..7900097 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Models/Question.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Models/Question.cs @@ -2,11 +2,17 @@ namespace DamageAssesment.Api.Questions.Models { - public class Question + public class MultiLanQuestion: BaseQuestion + { + public MultiLanguage Questions { get; set; } + } + public class Question: BaseQuestion + { + public List Questions { get; set; } + } + public class BaseQuestion { public int Id { get; set; } - public List Questions { get; set; } - //public int QuestionTypeID { get; set; } public string TypeText { get; set; } = string.Empty; diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Models/QuestionCategory.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Models/QuestionCategory.cs index 78a6b52..7c8ccc7 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Models/QuestionCategory.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Models/QuestionCategory.cs @@ -1,9 +1,17 @@ namespace DamageAssesment.Api.Questions.Models { - public class QuestionCategory + public class MultiLanQuestionCategory : BaseQuestionCategory + { + public object Titles { get; set; } + } + public class QuestionCategory : BaseQuestionCategory + { + public List Categories { get; set; } + } + public class BaseQuestionCategory { public int Id { get; set; } - public string CategoryName { get; set; } - public string CategoryImage { get; set; } + public string IconName { get; set; } + public string IconLibrary { get; set; } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Models/QuestionsTranslation.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Models/QuestionsTranslation.cs index 6df8219..af39cdd 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Models/QuestionsTranslation.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Models/QuestionsTranslation.cs @@ -5,4 +5,8 @@ public string QuestionText { get; set; } public string Language { get; set; } = "En"; } + public class MultiLanguage + { + public object questionText { get; set; } + } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Models/SurveyQuestion.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Models/SurveyQuestion.cs index 9736510..a3a5513 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Models/SurveyQuestion.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Models/SurveyQuestion.cs @@ -3,8 +3,8 @@ public class SurveyQuestions { public int CategoryId { get; set; } - public string CategoryName { get; set; } - public string CategoryImage { get; set; } - public List Questions { get; set; } + public string IconName { get; set; } + public string IconLibrary { get; set; } + public List Questions { get; set; } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Profiles/QuestionProfile.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Profiles/QuestionProfile.cs index 015fe0c..5c0f36e 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Profiles/QuestionProfile.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Profiles/QuestionProfile.cs @@ -8,11 +8,15 @@ namespace DamageAssesment.Api.Questions.Profiles { CreateMap().ForMember(dest => dest.TypeText, opt => opt.MapFrom(src => src.QuestionType.TypeText)); + CreateMap().ForMember(dest => dest.TypeText, + opt => opt.MapFrom(src => src.QuestionType.TypeText)); CreateMap(); - CreateMap(); + CreateMap(); CreateMap(); CreateMap(); CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs index 7ec55fa..26aa866 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs @@ -5,6 +5,8 @@ using DamageAssesment.Api.Questions.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Internal; +using System.Collections; +using System.Collections.Generic; namespace DamageAssesment.Api.Questions.Providers { @@ -57,16 +59,89 @@ namespace DamageAssesment.Api.Questions.Providers if (!questionDbContext.QuestionCategories.Any()) { - questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 1, CategoryName = "Flooding", CategoryImage= "https://example.com/images/img1.png" }); - questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 2, CategoryName = "Electrical", CategoryImage = "https://example.com/images/img2.png" }); - questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 3, CategoryName = "Structural", CategoryImage = "https://example.com/images/img3.png" }); - questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 4, CategoryName = "Utility", CategoryImage = "https://example.com/images/img4.png" }); - questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 5, CategoryName = "Debris", CategoryImage = "https://example.com/images/img5.png" }); + questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 1, IconName = "Flooding", IconLibrary= "https://example.com/images/img1.png" }); + questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 2, IconName = "Electrical", IconLibrary = "https://example.com/images/img2.png" }); + questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 3, IconName = "Structural", IconLibrary = "https://example.com/images/img3.png" }); + questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 4, IconName = "Utility", IconLibrary = "https://example.com/images/img4.png" }); + questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 5, IconName = "Debris", IconLibrary = "https://example.com/images/img5.png" }); + questionDbContext.SaveChanges(); + } + + if (!questionDbContext.CategoryTranslations.Any()) + { + + questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { Id = 1, CategoryId = 1, Title = "Flooding", Language = "en" }); + questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { Id = 2, CategoryId = 2, Title = "Electrical", Language = "en" }); + questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { Id = 3, CategoryId = 3, Title = "Structural", Language = "en" }); + questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { Id = 4, CategoryId = 4, Title = "Utility", Language = "en" }); + questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { Id = 5, CategoryId = 5, Title = "Debris", Language = "en" }); + questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { Id = 6, CategoryId = 1, Title = "Inondation", Language = "fr" }); + questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { Id = 7, CategoryId = 2, Title = "Électrique", Language = "fr" }); + questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { Id = 8, CategoryId = 3, Title = "De construction", Language = "fr" }); + questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { Id = 9, CategoryId = 4, Title = "Utilitaire", Language = "fr" }); + questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { Id = 10, CategoryId = 5, Title = "Débris", Language = "fr" }); + questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { Id = 11, CategoryId = 1, Title = "Inundación", Language = "es" }); + questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { Id = 12, CategoryId = 2, Title = "Eléctrica", Language = "es" }); + questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { Id = 13, CategoryId = 3, Title = "Estructural", Language = "es" }); + questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { Id = 14, CategoryId = 4, Title = "Utilidad", Language = "es" }); + questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { Id = 15, CategoryId = 5, Title = "Escombros", Language = "es" }); questionDbContext.SaveChanges(); } } - public async Task<(bool IsSuccess, IEnumerable Questions, string ErrorMessage)> GetQuestionsAsync(string Language) + public List GetCategoryTranslations(int id, string? language) + { + List categoryTranslations = new List(); + if (string.IsNullOrEmpty(language)) + { + categoryTranslations = mapper.Map, List>( + questionDbContext.CategoryTranslations.AsNoTracking().Where(a => a.CategoryId == id).ToList()); + } + else + { + categoryTranslations = mapper.Map, List>( + questionDbContext.CategoryTranslations.AsNoTracking().Where(a => a.CategoryId == id && a.Language == language).ToList()); + } + return categoryTranslations; + } + public object CreateCategoryMultiLanguageObject(List categoryTranslations) + { + object MultiLanguage = new object(); + Dictionary dict = new Dictionary(); + foreach (Models.CategoryTranslation item in categoryTranslations) + { + dict.Add(item.Language, item.Title); + } + MultiLanguage = dict; + return MultiLanguage; + } + public List GetQuestionsTranslations(int id, string? language) + { + List QuestionTranslations; + if (string.IsNullOrEmpty(language)) + { + QuestionTranslations = mapper.Map, List>( + questionDbContext.QuestionsTranslations.AsNoTracking().Where(a => a.QuestionId == id).ToList()); + } + else + { + QuestionTranslations = mapper.Map, List>( + questionDbContext.QuestionsTranslations.AsNoTracking().Where(a => a.QuestionId == id && a.Language == language).ToList()); + } + return QuestionTranslations; + } + public MultiLanguage CreateMultiLanguageObject(List questions) + { + MultiLanguage MultiLanguage = new MultiLanguage(); + Dictionary dict = new Dictionary(); + foreach (Models.QuestionsTranslation item in questions) + { + dict.Add(item.Language, item.QuestionText); + } + MultiLanguage.questionText = dict; + return MultiLanguage; + } + public async Task<(bool IsSuccess, IEnumerable Questions, string ErrorMessage)> GetQuestionsAsync(string language) { try { @@ -76,21 +151,10 @@ namespace DamageAssesment.Api.Questions.Providers { //logger?.LogInformation($"{question} customer(s) found"); - var result = mapper.Map, IEnumerable>(questions); - - + var result = mapper.Map, IEnumerable>(questions); foreach (var question in result) { - if (string.IsNullOrEmpty(Language)) - { - question.Questions = mapper.Map, List>( - questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == question.Id).ToList()); - } - else - { - question.Questions = mapper.Map, List>( - questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == question.Id && a.Language == Language).ToList()); - } + question.Questions = CreateMultiLanguageObject(GetQuestionsTranslations(question.Id, language)); } return (true, result, null); } @@ -102,27 +166,17 @@ namespace DamageAssesment.Api.Questions.Providers return (false, null, ex.Message); } } - public async Task<(bool IsSuccess, Models.Question Question, string ErrorMessage)> GetQuestionAsync(int Id, string Language) + public async Task<(bool IsSuccess, Models.MultiLanQuestion Question, string ErrorMessage)> GetQuestionAsync(int id, string language) { try { logger?.LogInformation("Query Question"); - var question = await questionDbContext.Questions.Include("QuestionType").AsNoTracking().FirstOrDefaultAsync(q => q.Id == Id); + var question = await questionDbContext.Questions.Include("QuestionType").AsNoTracking().FirstOrDefaultAsync(q => q.Id == id); if (question != null) { logger?.LogInformation($"{question} customer(s) found"); - var result = mapper.Map(question); - - if (string.IsNullOrEmpty(Language)) - { - result.Questions = mapper.Map, List>( - questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == result.Id).ToList()); - } - else - { - result.Questions = mapper.Map, List>( - questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == result.Id && a.Language == Language).ToList()); - } + var result = mapper.Map(question); + result.Questions = CreateMultiLanguageObject(GetQuestionsTranslations(id, language)); return (true, result, null); } return (false, null, "Not found"); @@ -133,27 +187,15 @@ namespace DamageAssesment.Api.Questions.Providers return (false, null, ex.Message); } } - public List GetSurveyQuestion(List questions, string Language) + public List GetSurveyQuestion(List questions, string language) { - if (string.IsNullOrEmpty(Language)) + foreach (var item in questions) { - foreach (var item in questions) - { - item.Questions = mapper.Map, List>( - questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == item.Id).ToList()); - } - } - else - { - foreach (var item in questions) - { - item.Questions = mapper.Map, List>( - questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == item.Id && a.Language == Language).ToList()); - } + item.Questions = CreateMultiLanguageObject(GetQuestionsTranslations(item.Id, language)); } return questions; } - public async Task<(bool IsSuccess, List SurveyQuestions, string ErrorMessage)> GetSurveyQuestionAsync(int SurveyId, string Language) + public async Task<(bool IsSuccess, List SurveyQuestions, string ErrorMessage)> GetSurveyQuestionAsync(int SurveyId, string language) { try { @@ -170,9 +212,9 @@ namespace DamageAssesment.Api.Questions.Providers surveyQuestionsList.Add(new SurveyQuestions() { CategoryId = item.Id, - CategoryImage = item.CategoryImage, - CategoryName = item.CategoryName, - Questions = GetSurveyQuestion(mapper.Map, List>(questions.Where(a => a.CategoryId == item.Id).ToList()), Language) + IconLibrary = item.IconLibrary, + IconName = item.IconName, + Questions = GetSurveyQuestion(mapper.Map, List>(questions.Where(a => a.CategoryId == item.Id).ToList()), language) }); } @@ -187,7 +229,7 @@ namespace DamageAssesment.Api.Questions.Providers return (false, null, ex.Message); } } - public async Task<(bool IsSuccess, Models.Question Question, string ErrorMessage)> PostQuestionAsync(Models.Question Question) + public async Task<(bool IsSuccess, Models.MultiLanQuestion Question, string ErrorMessage)> PostQuestionAsync(Models.Question Question) { try { @@ -200,7 +242,9 @@ namespace DamageAssesment.Api.Questions.Providers questionDbContext.QuestionsTranslations.AddRange(dbquestiontranslation); questionDbContext.SaveChanges(); Question.Id = dbquestion.Id; - return (true, Question, null); + var result = mapper.Map(dbquestion); + result.Questions = CreateMultiLanguageObject(GetQuestionsTranslations(result.Id,"")); + return (true, result, null); } catch (Exception ex) { @@ -208,7 +252,7 @@ namespace DamageAssesment.Api.Questions.Providers return (false, null, ex.Message); } } - public async Task<(bool IsSuccess, Models.Question Question, string ErrorMessage)> UpdateQuestionAsync(Models.Question Question) + public async Task<(bool IsSuccess, Models.MultiLanQuestion Question, string ErrorMessage)> UpdateQuestionAsync(Models.Question Question) { try { @@ -222,7 +266,9 @@ namespace DamageAssesment.Api.Questions.Providers dbquestiontranslation.ForEach(i => i.QuestionId = dbquestion.Id); questionDbContext.QuestionsTranslations.AddRange(dbquestiontranslation); questionDbContext.SaveChanges(); - return (true, Question, null); + var result = mapper.Map(dbquestion); + result.Questions = CreateMultiLanguageObject(GetQuestionsTranslations(result.Id, "")); + return (true, result, null); } catch (Exception ex) { @@ -231,21 +277,23 @@ namespace DamageAssesment.Api.Questions.Providers return (false, null, ex.Message); } } - public async Task<(bool IsSuccess, Models.Question Question, string ErrorMessage)> DeleteQuestionAsync(int Id) + public async Task<(bool IsSuccess, Models.MultiLanQuestion Question, string ErrorMessage)> DeleteQuestionAsync(int id) { try { - var question = await questionDbContext.Questions.Where(x => x.Id == Id).FirstOrDefaultAsync(); + var question = await questionDbContext.Questions.Where(x => x.Id == id).FirstOrDefaultAsync(); if (question != null) { + var result = mapper.Map(question); + result.Questions = CreateMultiLanguageObject(GetQuestionsTranslations(result.Id, "")); questionDbContext.Questions.Remove(question); questionDbContext.SaveChanges(); - return (true, mapper.Map(question), $"QuestionID {Id} deleted Successfuly"); + return (true, result, $"QuestionID {id} deleted Successfuly"); } else { - logger?.LogInformation($"QuestionID: {Id} Not found"); + logger?.LogInformation($"QuestionID: {id} Not found"); return (false, null, "Not Found"); } } @@ -258,7 +306,7 @@ namespace DamageAssesment.Api.Questions.Providers //Question Category Logic - public async Task<(bool IsSuccess, IEnumerable QuestionCategories, string ErrorMessage)> GetQuestionCategoriesAsync() + public async Task<(bool IsSuccess, IEnumerable QuestionCategories, string ErrorMessage)> GetQuestionCategoriesAsync(string? language) { try { @@ -267,7 +315,11 @@ namespace DamageAssesment.Api.Questions.Providers if (questionCategories != null) { //logger?.LogInformation($"{question} customer(s) found"); - var result = mapper.Map, IEnumerable>(questionCategories); + var result = mapper.Map, IEnumerable>(questionCategories); + foreach (var category in result) + { + category.Titles = CreateCategoryMultiLanguageObject(GetCategoryTranslations(category.Id, language)); + } return (true, result, null); } return (false, null, "Not found"); @@ -278,16 +330,17 @@ namespace DamageAssesment.Api.Questions.Providers return (false, null, ex.Message); } } - public async Task<(bool IsSuccess, Models.QuestionCategory QuestionCategory, string ErrorMessage)> GetQuestionCategoryAsync(int Id) + public async Task<(bool IsSuccess, Models.MultiLanQuestionCategory QuestionCategory, string ErrorMessage)> GetQuestionCategoryAsync(int id, string? language) { try { logger?.LogInformation("Query Question"); - var questioncategory = await questionDbContext.QuestionCategories.AsNoTracking().FirstOrDefaultAsync(q => q.Id == Id); + var questioncategory = await questionDbContext.QuestionCategories.AsNoTracking().FirstOrDefaultAsync(q => q.Id == id); if (questioncategory != null) { logger?.LogInformation($"{questioncategory} customer(s) found"); - var result = mapper.Map(questioncategory); + var result = mapper.Map(questioncategory); + result.Titles = CreateCategoryMultiLanguageObject(GetCategoryTranslations(result.Id, language)); return (true, result, null); } return (false, null, "Not found"); @@ -298,17 +351,23 @@ namespace DamageAssesment.Api.Questions.Providers return (false, null, ex.Message); } } - public async Task<(bool IsSuccess, Models.QuestionCategory QuestionCategory, string ErrorMessage)> PostQuestionCategoryAsync(Models.QuestionCategory QuestionCategory) + public async Task<(bool IsSuccess, Models.MultiLanQuestionCategory QuestionCategory, string ErrorMessage)> PostQuestionCategoryAsync(Models.QuestionCategory QuestionCategory) { try { logger?.LogInformation("Query Question"); var dbQuestionCategory = mapper.Map(QuestionCategory); + var dbCategorytranslations = mapper.Map, List>(QuestionCategory.Categories); // Question.QuestionType = GetQuestionType(Question.QuestionTypeId); questionDbContext.QuestionCategories.Add(dbQuestionCategory); questionDbContext.SaveChanges(); QuestionCategory.Id = dbQuestionCategory.Id; - return (true, QuestionCategory, null); + dbCategorytranslations.ForEach(i => i.CategoryId = QuestionCategory.Id); + questionDbContext.CategoryTranslations.AddRange(dbCategorytranslations); + questionDbContext.SaveChanges(); + var result = mapper.Map(dbQuestionCategory); + result.Titles = CreateCategoryMultiLanguageObject(GetCategoryTranslations(result.Id, "")); + return (true, result, null); } catch (Exception ex) { @@ -316,15 +375,23 @@ namespace DamageAssesment.Api.Questions.Providers return (false, null, ex.Message); } } - public async Task<(bool IsSuccess, Models.QuestionCategory QuestionCategory, string ErrorMessage)> UpdateQuestionCategoryAsync(Models.QuestionCategory QuestionCategory) + public async Task<(bool IsSuccess, Models.MultiLanQuestionCategory QuestionCategory, string ErrorMessage)> UpdateQuestionCategoryAsync(Models.QuestionCategory QuestionCategory) { try { var dbQuestionCategory = mapper.Map(QuestionCategory); + var dbCategorytranslations = mapper.Map, List>(QuestionCategory.Categories); questionDbContext.Entry(dbQuestionCategory).State = EntityState.Modified; - + QuestionCategory.Id = dbQuestionCategory.Id; + var oldcategories = questionDbContext.CategoryTranslations.Where(a => a.CategoryId == dbQuestionCategory.Id).ToList(); + if (oldcategories != null) + questionDbContext.CategoryTranslations.RemoveRange(oldcategories); + dbCategorytranslations.ForEach(i => i.CategoryId = QuestionCategory.Id); + questionDbContext.CategoryTranslations.AddRange(dbCategorytranslations); questionDbContext.SaveChanges(); - return (true, QuestionCategory, null); + var result = mapper.Map(dbQuestionCategory); + result.Titles = CreateCategoryMultiLanguageObject(GetCategoryTranslations(result.Id, "")); + return (true, result, null); } catch (Exception ex) { @@ -333,18 +400,20 @@ namespace DamageAssesment.Api.Questions.Providers return (false, null, ex.Message); } } - public async Task<(bool IsSuccess, Models.QuestionCategory QuestionCategory, string ErrorMessage)> DeleteQuestionCategoryAsync(int Id) + public async Task<(bool IsSuccess, Models.MultiLanQuestionCategory QuestionCategory, string ErrorMessage)> DeleteQuestionCategoryAsync(int Id) { try { var questioncategory = await questionDbContext.QuestionCategories.Where(x => x.Id == Id).FirstOrDefaultAsync(); if (questioncategory != null) { + var result = mapper.Map(questioncategory); + result.Titles = CreateCategoryMultiLanguageObject(GetCategoryTranslations(result.Id, "")); var question = await questionDbContext.Questions.Where(x => x.Id == Id).ToListAsync(); questionDbContext.Questions.RemoveRange(question); questionDbContext.QuestionCategories.Remove(questioncategory); questionDbContext.SaveChanges(); - return (true, mapper.Map(questioncategory), $"QuestionID {Id} deleted Successfuly"); + return (true, result, $"QuestionID {Id} deleted Successfuly"); } else { diff --git a/DamageAssesmentApi/DamageAssesment.Api.QuestionsTest/CategoryMockData.cs b/DamageAssesmentApi/DamageAssesment.Api.QuestionsTest/CategoryMockData.cs index cf0bb04..e09a7c0 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.QuestionsTest/CategoryMockData.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.QuestionsTest/CategoryMockData.cs @@ -8,42 +8,42 @@ namespace DamageAssesment.Api.Questions.Test { public class CategoryMockData { - public static async Task<(bool, IEnumerable, string)> getOkResponse() + public static async Task<(bool, IEnumerable, string)> getOkResponse() { - IEnumerable list = new List(); + IEnumerable list = new List(); for (int i = 0; i < 10; i++) { - list.Append(new Questions.Models.QuestionCategory { Id = i, CategoryImage = "img"+i,CategoryName="Category "+i }); + list.Append(new Questions.Models.MultiLanQuestionCategory { Id = i, IconLibrary = "img"+i,IconName="Category "+i }); } return (true, list, null); } - public static async Task<(bool, Questions.Models.QuestionCategory, string)> getOkResponse(int Id) + public static async Task<(bool, Questions.Models.MultiLanQuestionCategory, string)> getOkResponse(int Id) { var Questions = await getOkResponse(); var Question = Questions.Item2.FirstOrDefault(s => s.Id == Id); return (true, Question, null); } - public static async Task<(bool, Questions.Models.QuestionCategory, string)> getBadRequestResponse() + public static async Task<(bool, Questions.Models.MultiLanQuestionCategory, string)> getBadRequestResponse() { return (false, null, "Bad Request"); } - public static async Task<(bool, Questions.Models.QuestionCategory, string)> getNotFoundResponse() + public static async Task<(bool, Questions.Models.MultiLanQuestionCategory, string)> getNotFoundResponse() { return (false, null, "Not Found"); } - public static async Task<(bool, IEnumerable, string)> getNoContentResponse() + public static async Task<(bool, IEnumerable, string)> getNoContentResponse() { - IEnumerable list = new List(); + IEnumerable list = new List(); return (false, list, null); } public static async Task getInputQuestionCategoryData() { - return new Questions.Models.QuestionCategory { Id = 1, CategoryName = "Category 1",CategoryImage="img 1" }; + return new Questions.Models.QuestionCategory { Id = 1, IconName = "Category 1",IconLibrary="img 1" }; } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.QuestionsTest/MockData.cs b/DamageAssesmentApi/DamageAssesment.Api.QuestionsTest/MockData.cs index 1991128..363ed98 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.QuestionsTest/MockData.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.QuestionsTest/MockData.cs @@ -5,13 +5,13 @@ namespace DamageAssesment.Api.Questions.Test public class MockData { - public static async Task<(bool, IEnumerable, string)> getOkResponse() + public static async Task<(bool, IEnumerable, string)> getOkResponse() { - IEnumerable list = new List(); + IEnumerable list = new List(); for (int i = 0; i < 10; i++) { - list.Append(new Questions.Models.Question { Id = i, TypeText = "Text" + i, SurveyId = 1, QuestionNumber = 1, IsRequired = true, Comment = false, Key = true, CategoryId=i }); + list.Append(new Questions.Models.MultiLanQuestion { Id = i, TypeText = "Text" + i, SurveyId = 1, QuestionNumber = 1, IsRequired = true, Comment = false, Key = true, CategoryId=i }); } return (true, list, null); } @@ -23,38 +23,38 @@ namespace DamageAssesment.Api.Questions.Test for (int i = 0; i < 10; i++) { - List question = new List(); - question.Add(new Models.Question { Id = i, TypeText = "Text" + i, SurveyId = 1, QuestionNumber = 1, IsRequired = true, Comment = false, Key = true, CategoryId = i }); + List question = new List(); + question.Add(new Models.MultiLanQuestion { Id = i, TypeText = "Text" + i, SurveyId = 1, QuestionNumber = 1, IsRequired = true, Comment = false, Key = true, CategoryId = i }); list.Append(new Questions.Models.SurveyQuestions { CategoryId = i, - CategoryImage = "img" + i, - CategoryName = "Category " + i, + IconLibrary = "img" + i, + IconName = "Category " + i, Questions = question }); } return (true, list, null); } - public static async Task<(bool, Questions.Models.Question, string)> getOkResponse(int Id) + public static async Task<(bool, Questions.Models.MultiLanQuestion, string)> getOkResponse(int Id) { var Questions = await getOkResponse(); var Question = Questions.Item2.FirstOrDefault(s => s.Id == Id); return (true, Question, null); } - public static async Task<(bool, Questions.Models.Question, string)> getBadRequestResponse() + public static async Task<(bool, Questions.Models.MultiLanQuestion, string)> getBadRequestResponse() { return (false, null, "Bad Request"); } - public static async Task<(bool, Questions.Models.Question, string)> getNotFoundResponse() + public static async Task<(bool, Questions.Models.MultiLanQuestion, string)> getNotFoundResponse() { return (false, null, "Not Found"); } - public static async Task<(bool, IEnumerable, string)> getNoContentResponse() + public static async Task<(bool, IEnumerable, string)> getNoContentResponse() { - IEnumerable list = new List(); + IEnumerable list = new List(); return (false, list, null); } public static async Task<(bool, List, string)> getNoSurveyContentResponse() diff --git a/DamageAssesmentApi/DamageAssesment.Api.QuestionsTest/QuestionsServiceTest.cs b/DamageAssesmentApi/DamageAssesment.Api.QuestionsTest/QuestionsServiceTest.cs index 17797c6..bda9db2 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.QuestionsTest/QuestionsServiceTest.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.QuestionsTest/QuestionsServiceTest.cs @@ -42,7 +42,7 @@ namespace DamageAssesment.Api.Questions.Test mockQuestionService.Setup(service => service.GetQuestionAsync(1,null)).ReturnsAsync(mockResponse); var QuestionProvider = new QuestionsController(mockQuestionService.Object); - var result = (OkObjectResult)await QuestionProvider.GetQuestionAsync(1,null); + var result = (OkObjectResult)await QuestionProvider.GetQuestionByIdAsync(1,null); Assert.Equal(200, result.StatusCode); } @@ -55,7 +55,7 @@ namespace DamageAssesment.Api.Questions.Test mockQuestionService.Setup(service => service.GetQuestionAsync(99,null)).ReturnsAsync(mockResponse); var QuestionProvider = new QuestionsController(mockQuestionService.Object); - var result = (NotFoundResult)await QuestionProvider.GetQuestionAsync(99,null); + var result = (NotFoundResult)await QuestionProvider.GetQuestionByIdAsync(99,null); Assert.Equal(404, result.StatusCode); } @@ -188,10 +188,10 @@ namespace DamageAssesment.Api.Questions.Test { var mockQuestionService = new Mock(); var mockResponse = await CategoryMockData.getOkResponse(); - mockQuestionService.Setup(service => service.GetQuestionCategoriesAsync()).ReturnsAsync(mockResponse); + mockQuestionService.Setup(service => service.GetQuestionCategoriesAsync("en")).ReturnsAsync(mockResponse); var QuestionProvider = new QuestionsController(mockQuestionService.Object); - var result = (OkObjectResult)await QuestionProvider.GetQuestionCategoriesAsync(); + var result = (OkObjectResult)await QuestionProvider.GetQuestionCategoriesAsync("en"); Assert.Equal(200, result.StatusCode); } @@ -201,10 +201,10 @@ namespace DamageAssesment.Api.Questions.Test { var mockQuestionService = new Mock(); var mockResponse = await CategoryMockData.getNoContentResponse(); - mockQuestionService.Setup(service => service.GetQuestionCategoriesAsync()).ReturnsAsync(mockResponse); + mockQuestionService.Setup(service => service.GetQuestionCategoriesAsync("en")).ReturnsAsync(mockResponse); var QuestionProvider = new QuestionsController(mockQuestionService.Object); - var result = (NoContentResult)await QuestionProvider.GetQuestionCategoriesAsync(); + var result = (NoContentResult)await QuestionProvider.GetQuestionCategoriesAsync("en"); Assert.Equal(204, result.StatusCode); } @@ -214,10 +214,10 @@ namespace DamageAssesment.Api.Questions.Test { var mockQuestionService = new Mock(); var mockResponse = await CategoryMockData.getOkResponse(1); - mockQuestionService.Setup(service => service.GetQuestionCategoryAsync(1)).ReturnsAsync(mockResponse); + mockQuestionService.Setup(service => service.GetQuestionCategoryAsync(1, "en")).ReturnsAsync(mockResponse); var QuestionProvider = new QuestionsController(mockQuestionService.Object); - var result = (OkObjectResult)await QuestionProvider.GetQuestionCategoryAsync(1); + var result = (OkObjectResult)await QuestionProvider.GetQuestionCategoryAsync(1, "en"); Assert.Equal(200, result.StatusCode); } @@ -227,10 +227,10 @@ namespace DamageAssesment.Api.Questions.Test { var mockQuestionService = new Mock(); var mockResponse = await CategoryMockData.getNotFoundResponse(); - mockQuestionService.Setup(service => service.GetQuestionCategoryAsync(99)).ReturnsAsync(mockResponse); + mockQuestionService.Setup(service => service.GetQuestionCategoryAsync(99, "en")).ReturnsAsync(mockResponse); var QuestionProvider = new QuestionsController(mockQuestionService.Object); - var result = (NotFoundResult)await QuestionProvider.GetQuestionCategoryAsync(99); + var result = (NotFoundResult)await QuestionProvider.GetQuestionCategoryAsync(99, "en"); Assert.Equal(404, result.StatusCode); } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys.Test/MockData.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys.Test/MockData.cs index 01972e3..fa4928d 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys.Test/MockData.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys.Test/MockData.cs @@ -9,37 +9,37 @@ namespace DamageAssesment.Api.Survey.Test { public class MockData { - public static async Task<(bool, IEnumerable, string)> getOkResponse() + public static async Task<(bool, IEnumerable, string)> getOkResponse() { - IEnumerable list = new List(); + IEnumerable list = new List(); for (int i = 0; i < 10; i++) { - list.Append(new Surveys.Models.Survey { Id = i, /*Title = "Survey Title - " + i */}); + list.Append(new Surveys.Models.MultiLanSurvey { Id = i, /*Title = "Survey Title - " + i */}); } return (true, list, null); } - public static async Task<(bool, Surveys.Models.Survey, string)> getOkResponse( int Id) + public static async Task<(bool, Surveys.Models.MultiLanSurvey, string)> getOkResponse( int Id) { var surveys = await getOkResponse(); var survey = surveys.Item2.FirstOrDefault(s => s.Id == Id); return (true, survey, null); } - public static async Task<(bool, Surveys.Models.Survey, string)> getBadRequestResponse() + public static async Task<(bool, Surveys.Models.MultiLanSurvey, string)> getBadRequestResponse() { return (false, null,"Bad Request"); } - public static async Task<(bool, Surveys.Models.Survey, string)> getNotFoundResponse() + public static async Task<(bool, Surveys.Models.MultiLanSurvey, string)> getNotFoundResponse() { return (false, null, "Not Found"); } - public static async Task<(bool, IEnumerable, string)> getNoContentResponse() + public static async Task<(bool, IEnumerable, string)> getNoContentResponse() { - IEnumerable list = new List(); + IEnumerable list = new List(); return (false, list, null); } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Controllers/SurveysController.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Controllers/SurveysController.cs index e360e53..7853716 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Controllers/SurveysController.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Controllers/SurveysController.cs @@ -3,7 +3,6 @@ using Microsoft.AspNetCore.Mvc; namespace DamageAssesment.Api.Surveys.Controllers { - [Route("api")] [ApiController] public class SurveysController : ControllerBase { @@ -18,11 +17,11 @@ namespace DamageAssesment.Api.Surveys.Controllers /// [Route("Surveys")] - [Route("{Language}/Surveys")] + [Route("Surveys/{language:alpha}")] [HttpGet] - public async Task GetSurveysAsync(string? Language) + public async Task GetSurveysAsync(string? language) { - var result = await this.surveyProvider.GetSurveysAsync(Language); + var result = await this.surveyProvider.GetSurveysAsync(language); if (result.IsSuccess) { return Ok(result.Surveys); @@ -33,12 +32,12 @@ namespace DamageAssesment.Api.Surveys.Controllers /// /// GET request for retrieving surveys by ID. /// - [Route("Surveys/{Id}")] - [Route("{Language}/Surveys/{Id}")] + [Route("Surveys/{id:int}")] + [Route("Surveys/{id:int}/{language:alpha}")] [HttpGet] - public async Task GetSurveysAsync(int Id, string? Language) + public async Task GetSurveysAsync(int id, string? language) { - var result = await this.surveyProvider.GetSurveysAsync(Id, Language); + var result = await this.surveyProvider.GetSurveysAsync(id, language); if (result.IsSuccess) { return Ok(result.Surveys); @@ -64,10 +63,10 @@ namespace DamageAssesment.Api.Surveys.Controllers /// - [HttpPut("Surveys/{Id}")] - public async Task PutSurveysAsync(int Id, Models.Survey survey) + [HttpPut("Surveys/{id}")] + public async Task 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); @@ -81,10 +80,10 @@ namespace DamageAssesment.Api.Surveys.Controllers /// /// DELETE request for deleting a survey by ID. /// - [HttpDelete("Surveys/{Id}")] - public async Task DeleteSurveysAsync(int Id) + [HttpDelete("Surveys/{id}")] + public async Task DeleteSurveysAsync(int id) { - var result = await this.surveyProvider.DeleteSurveyAsync(Id); + var result = await this.surveyProvider.DeleteSurveyAsync(id); if (result.IsSuccess) { return Ok(result.Survey); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Interfaces/ISurveyProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Interfaces/ISurveyProvider.cs index 46743d3..22b2910 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Interfaces/ISurveyProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Interfaces/ISurveyProvider.cs @@ -2,11 +2,11 @@ { public interface ISurveyProvider { - 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); + Task<(bool IsSuccess, IEnumerable< Models.MultiLanSurvey> Surveys, string ErrorMessage)> GetSurveysAsync(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); + Task<(bool IsSuccess, Models.MultiLanSurvey Survey, string ErrorMessage)> DeleteSurveyAsync(int id); void seedData(); } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Models/Survey.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Models/Survey.cs index 7fa898d..f79158b 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Models/Survey.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Models/Survey.cs @@ -2,13 +2,21 @@ namespace DamageAssesment.Api.Surveys.Models { - public class Survey + public class MultiLanSurvey : BaseSurvey + { + public object Titles { get; set; } + } + public class Survey : BaseSurvey + { + public IEnumerable Titles { get; set; } + + } + public class BaseSurvey { public int Id { get; set; } public bool IsEnabled { get; set; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public DateTime CreatedDate { get; set; } - public IEnumerable Titles { get; set; } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Profiles/SurveysProfile.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Profiles/SurveysProfile.cs index 2cb043c..7b65d99 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Profiles/SurveysProfile.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Profiles/SurveysProfile.cs @@ -3,8 +3,10 @@ public class SurveysProfile:AutoMapper.Profile { public SurveysProfile() { - CreateMap(); + CreateMap(); CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs index c756738..f843216 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs @@ -49,60 +49,57 @@ namespace DamageAssesment.Api.Surveys.Providers } } - public async Task<(bool IsSuccess, IEnumerable Surveys, string ErrorMessage)> GetSurveysAsync(string Language) + public IEnumerable GetSurveyTranslations(int id, IEnumerable SurveyTranslation,string? language) { - IEnumerable surveysList = null; + if (SurveyTranslation == null) + { + if (string.IsNullOrEmpty(language)) + { + SurveyTranslation = mapper.Map, IEnumerable>( + surveyDbContext.SurveysTranslation.Where(a => a.SurveyId == id).ToList()); + } + else + { + SurveyTranslation = mapper.Map, IEnumerable>( + surveyDbContext.SurveysTranslation.Where(a => a.SurveyId == id && a.Language == language).ToList()); + } + } + return SurveyTranslation; + } + public object CreateMultiLanguageObject(IEnumerable surveyTranslations) + { + object MultiLanguage = new object(); + Dictionary dict = new Dictionary(); + foreach (Models.SurveyTranslation item in surveyTranslations) + { + dict.Add(item.Language, item.Title); + } + MultiLanguage = dict; + return MultiLanguage; + } + public async Task<(bool IsSuccess, IEnumerable Surveys, string ErrorMessage)> GetSurveysAsync(string language) + { + IEnumerable surveysList = null; try { logger?.LogInformation("Gell all Surveys from DB"); var surveys = await surveyDbContext.Surveys.Where(s => s.IsEnabled == true).ToListAsync(); - var surveyTranslations = await surveyDbContext.SurveysTranslation.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 - } + surveysList = from s in surveys + select new + Models.MultiLanSurvey + { + Id = s.Id, + StartDate = s.StartDate, + EndDate = s.EndDate, + IsEnabled = s.IsEnabled, + CreatedDate = s.CreatedDate, + Titles = CreateMultiLanguageObject(GetSurveyTranslations(s.Id,null, 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"); return (true, surveysList, null); @@ -115,55 +112,27 @@ namespace DamageAssesment.Api.Surveys.Providers return (false, null, ex.Message); } } - public async Task<(bool IsSuccess, Models.Survey Surveys, string ErrorMessage)> GetSurveysAsync(int Id, string Language) + public async Task<(bool IsSuccess, Models.MultiLanSurvey Surveys, string ErrorMessage)> GetSurveysAsync(int id, string language) { try { logger?.LogInformation("Query Survey"); - var survey = await surveyDbContext.Surveys.SingleOrDefaultAsync(s => s.Id == Id && s.IsEnabled == true); + var survey = await surveyDbContext.Surveys.SingleOrDefaultAsync(s => s.Id == id && s.IsEnabled == true); if (survey != null) { - Models.Survey result = null; + Models.MultiLanSurvey result = null; var surveyTranslations = await surveyDbContext.SurveysTranslation.Where(s => s.SurveyId == survey.Id).ToListAsync(); - - if (string.IsNullOrEmpty(Language)) + result = new Models.MultiLanSurvey { - 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 - } + Id = survey.Id, + StartDate = survey.StartDate, + EndDate = survey.EndDate, + IsEnabled = survey.IsEnabled, + CreatedDate = survey.CreatedDate, + Titles = CreateMultiLanguageObject(GetSurveyTranslations(survey.Id,null, 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"); return (true, result, null); } return (false, null, "Not found"); @@ -175,7 +144,7 @@ namespace DamageAssesment.Api.Surveys.Providers } } - public async Task<(bool IsSuccess, Models.Survey Survey, string ErrorMessage)> PostSurveyAsync(Models.Survey survey) + public async Task<(bool IsSuccess, Models.MultiLanSurvey Survey, string ErrorMessage)> PostSurveyAsync(Models.Survey survey) { try { @@ -192,8 +161,9 @@ namespace DamageAssesment.Api.Surveys.Providers surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation {SurveyId = _survey.Id, Language = title.Language, Title = title.Title }); } await surveyDbContext.SaveChangesAsync(); - survey.Id = _survey.Id; - return (true,survey, "Successful"); + var result = mapper.Map(_survey); + result.Titles = CreateMultiLanguageObject(GetSurveyTranslations(_survey.Id, survey.Titles, "")); + return (true, result, "Successful"); } else { @@ -208,7 +178,7 @@ namespace DamageAssesment.Api.Surveys.Providers } } - public async Task<(bool IsSuccess, Models.Survey Survey, string ErrorMessage)> PutSurveyAsync(int Id, Models.Survey survey) + public async Task<(bool IsSuccess, Models.MultiLanSurvey Survey, string ErrorMessage)> PutSurveyAsync(int Id, Models.Survey survey) { try { @@ -236,21 +206,8 @@ namespace DamageAssesment.Api.Surveys.Providers 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 - } - - }; + var result = mapper.Map(_survey); + result.Titles = CreateMultiLanguageObject(GetSurveyTranslations(_survey.Id, survey.Titles, "")); return (true, result, "Successful"); } else @@ -272,7 +229,7 @@ namespace DamageAssesment.Api.Surveys.Providers } } - public async Task<(bool IsSuccess, Models.Survey Survey, string ErrorMessage)> DeleteSurveyAsync(int Id) + public async Task<(bool IsSuccess, Models.MultiLanSurvey Survey, string ErrorMessage)> DeleteSurveyAsync(int Id) { try { @@ -280,9 +237,11 @@ namespace DamageAssesment.Api.Surveys.Providers if (survey != null) { + var result = mapper.Map(survey); + result.Titles = CreateMultiLanguageObject(GetSurveyTranslations(survey.Id, null, "")); surveyDbContext.Surveys.Remove(survey); await surveyDbContext.SaveChangesAsync(); - return (true, mapper.Map(survey), $"Survey Id: {Id} deleted Successfuly"); + return (true, result, $"Survey Id: {Id} deleted Successfuly"); } else {