Merged new dev changes

This commit is contained in:
uppuv
2023-09-13 13:16:42 -04:00
137 changed files with 2247 additions and 1254 deletions

View File

@ -1,13 +1,8 @@
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
{
[Route("api")]
[ApiController]
public class QuestionsController : ControllerBase
{
@ -19,41 +14,63 @@ namespace DamageAssesment.Api.Questions.Controllers
this.questionsProvider = questionsProvider;
}
/// <summary>
/// GET request for retrieving questions.
/// </summary>
// get all questions
[HttpGet("Questions")]
public async Task<IActionResult> GetQuestionsAsync()
[Route("Questions")]
[Route("Questions/{language:alpha}")]
[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);
}
return NoContent();
}
//Get questions based on question id
[HttpGet("Questions/{id}")]
public async Task<IActionResult> GetQuestionAsync(int id)
/// <summary>
/// GET request for retrieving a question by ID.
/// </summary>
[Route("Questions/{id}/{language:alpha}")]
[Route("Questions/{id:int}")]
[HttpGet]
public async Task<IActionResult> GetQuestionByIdAsync(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);
}
return NotFound();
}
//get all questions based on survey id
[HttpGet("GetSurveyQuestions/{surveyId}")]
public async Task<IActionResult> GetSurveyQuestions(int surveyId,string? Language)
/// <summary>
/// GET request for retrieving survey questions based on a survey ID.
/// Uri: {Optional language}/GetSurveyQuestions/{surveyId} :Default returns question in all languages
/// </summary>
[Route("Questions/BySurvey/{surveyId:int}")]
[Route("Questions/BySurvey/{surveyId:int}/{language:alpha}")]
[HttpGet]
public async Task<IActionResult> GetSurveyQuestions(int surveyId,string? language)
{
if (string.IsNullOrEmpty(Language)) Language = "en";
var result = await this.questionsProvider.GetSurveyQuestionAsync(surveyId, Language);
var result = await this.questionsProvider.GetSurveyQuestionAsync(surveyId, language);
if (result.IsSuccess)
{
return Ok(result.SurveyQuestions);
}
return NotFound();
}
//update existing question
/// <summary>
/// PUT request for updating a question (multilingual).
/// </summary>
[HttpPut("Questions")]
public async Task<IActionResult> UpdateQuestion(Models.Question question)
{
@ -71,7 +88,10 @@ namespace DamageAssesment.Api.Questions.Controllers
}
return CreatedAtRoute("DefaultApi", new { id = question.Id }, question);
}
//save new question
/// <summary>
/// POST request for creating a new question (multilingual).
/// </summary>
[HttpPost("Questions")]
public async Task<IActionResult> CreateQuestion(Models.Question question)
{
@ -86,7 +106,10 @@ namespace DamageAssesment.Api.Questions.Controllers
}
return CreatedAtRoute("DefaultApi", new { id = question.Id }, question);
}
// delete existing question
/// <summary>
/// DELETE request for deleting a question based on ID.
/// </summary>
[HttpDelete("Questions/{id}")]
public async Task<IActionResult> DeleteQuestion(int id)
{
@ -98,23 +121,30 @@ namespace DamageAssesment.Api.Questions.Controllers
return NotFound();
}
/// <summary>
/// GET request for retrieving question categories.
/// </summary>
// get all questions
[HttpGet("QuestionCategories")]
public async Task<IActionResult> GetQuestionCategoriesAsync()
[HttpGet("Questions/Categories")]
[HttpGet("Questions/Categories/{language:alpha}")]
public async Task<IActionResult> GetQuestionCategoriesAsync(string? language)
{
var result = await this.questionsProvider.GetQuestionCategoriesAsync();
var result = await this.questionsProvider.GetQuestionCategoriesAsync(language);
if (result.IsSuccess)
{
return Ok(result.QuestionCategories);
}
return NoContent();
}
//Get questions based on question id
[HttpGet("QuestionCategories/{id}")]
public async Task<IActionResult> GetQuestionCategoryAsync(int id)
/// <summary>
/// GET request for retrieving a question category by ID.
/// </summary>
[HttpGet("Questions/Categories/{id:int}")]
[HttpGet("Questions/Categories/{id:int}/{language:alpha}")]
public async Task<IActionResult> 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);
@ -123,8 +153,11 @@ namespace DamageAssesment.Api.Questions.Controllers
}
//update existing question
[HttpPut("QuestionCategories")]
/// <summary>
/// PUT request for updating a question category.
/// </summary>
[HttpPut("Questions/Categories")]
public async Task<IActionResult> UpdateQuestionCategory(Models.QuestionCategory questionCategory)
{
if (questionCategory != null)
@ -141,8 +174,11 @@ namespace DamageAssesment.Api.Questions.Controllers
}
return CreatedAtRoute("DefaultApi", new { id = questionCategory.Id }, questionCategory);
}
//save new question
[HttpPost("QuestionCategories")]
/// <summary>
/// POST request for creating a new question category.
/// </summary>
[HttpPost("Questions/Categories")]
public async Task<IActionResult> CreateQuestionCategory(Models.QuestionCategory questionCategory)
{
if (questionCategory != null)
@ -156,8 +192,11 @@ namespace DamageAssesment.Api.Questions.Controllers
}
return CreatedAtRoute("DefaultApi", new { id = questionCategory.Id }, questionCategory);
}
// delete existing question
[HttpDelete("QuestionCategories/{id}")]
/// <summary>
/// DELETE request for deleting a question category based on ID.
/// </summary>
[HttpDelete("Questions/Categories/{id}")]
public async Task<IActionResult> DeleteQuestionCategory(int id)
{
var result = await this.questionsProvider.DeleteQuestionCategoryAsync(id);

View File

@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>
@ -19,6 +20,9 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.21" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.5" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>

View File

@ -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; }
}
}

View File

@ -20,7 +20,6 @@ namespace DamageAssesment.Api.Questions.Db
public bool Key { get; set; }
[ForeignKey("Survey")]
public int? SurveyId { get; set; }
public string QuestionGroup { get; set; }
[ForeignKey("QuestionCategory")]
public int CategoryId { get; set; }

View File

@ -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; }
}
}

View File

@ -20,12 +20,29 @@ namespace DamageAssesment.Api.Questions.Db
public DbSet<Db.QuestionType> QuestionTypes { get; set; }
public DbSet<Db.QuestionsTranslation> QuestionsTranslations { get; set; }
public DbSet<Db.QuestionCategory> QuestionCategories { get; set; }
//protected override void OnModelCreating(ModelBuilder modelBuilder)
//{
// modelBuilder.Entity<Question>()
// .HasOne(a => a.QuestionType)
// .WithOne(b => b.Question)
// .HasForeignKey<QuestionType>(b => b.QuestionTypeID);
//}
public DbSet<Db.CategoryTranslation> CategoryTranslations { get; set; }
public QuestionDbContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Question>()
.Property(item => item.Id)
.ValueGeneratedOnAdd();
modelBuilder.Entity<QuestionType>()
.Property(item => item.Id)
.ValueGeneratedOnAdd();
modelBuilder.Entity<QuestionsTranslation>()
.Property(item => item.Id)
.ValueGeneratedOnAdd();
modelBuilder.Entity<QuestionCategory>()
.Property(item => item.Id)
.ValueGeneratedOnAdd();
modelBuilder.Entity<CategoryTranslation>()
.Property(item => item.Id)
.ValueGeneratedOnAdd();
}
}
}

View File

@ -4,18 +4,19 @@ 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, 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);
Task<(bool IsSuccess, Models.Question Question, string ErrorMessage)> DeleteQuestionAsync(int Id);
Task<(bool IsSuccess, Models.MultiLanguage Question, string ErrorMessage)> GetQuestionAsync(int id, string language);
Task<(bool IsSuccess, IEnumerable<Models.MultiLanguage> Questions, string ErrorMessage)> GetQuestionsAsync(string language);
Task<(bool IsSuccess, List<SurveyQuestions> SurveyQuestions, string ErrorMessage)> GetSurveyQuestionAsync(int surveyId,string language);
Task<(bool IsSuccess, Models.MultiLanguage Question, string ErrorMessage)> PostQuestionAsync(Models.Question Question);
Task<(bool IsSuccess, Models.MultiLanguage Question, string ErrorMessage)> UpdateQuestionAsync(Models.Question Question);
Task<(bool IsSuccess, Models.MultiLanguage Question, string ErrorMessage)> DeleteQuestionAsync(int id);
Task<(bool IsSuccess, IEnumerable<Models.QuestionCategory> 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<Models.MultiLanQuestionCategory> 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();
}
}

View File

@ -0,0 +1,8 @@
namespace DamageAssesment.Api.Questions.Models
{
public class CategoryTranslation
{
public string Title { get; set; }
public string Language { get; set; }
}
}

View File

@ -1,24 +1,18 @@
using System.ComponentModel.DataAnnotations;
namespace DamageAssesment.Api.Questions.Models
namespace DamageAssesment.Api.Questions.Models
{
public class Question
public class Question: BaseQuestion
{
public List<QuestionsTranslation> Questions { get; set; }
}
public class BaseQuestion
{
public int Id { get; set; }
public List<QuestionsTranslation> Questions { get; set; }
//public int QuestionTypeID { get; set; }
public string TypeText { get; set; } = string.Empty;
public int QuestionNumber { get; set; }
public bool IsRequired { get; set; }
public bool Comment { get; set; }
public bool Key { get; set; }
public int? SurveyId { get; set; }
public string QuestionGroup { get; set; }
public int CategoryId { get; set; }
// public int? Survey_SurveyID { get; set; }
}
}

View File

@ -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<CategoryTranslation> 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; }
}
}

View File

@ -3,6 +3,10 @@
public class QuestionsTranslation
{
public string QuestionText { get; set; }
public string Language { get; set; } = "En";
public string Language { get; set; } = "en";
}
public class MultiLanguage : BaseQuestion
{
public Dictionary<string,string> Text { get; set; }
}
}

View File

@ -3,8 +3,8 @@
public class SurveyQuestions
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public string CategoryImage { get; set; }
public List<Question> Questions { get; set; }
public string IconName { get; set; }
public string IconLibrary { get; set; }
public List<MultiLanguage> QuestionsText { get; set; }
}
}

View File

@ -8,11 +8,15 @@ namespace DamageAssesment.Api.Questions.Profiles
{
CreateMap<Db.Question, Models.Question>().ForMember(dest => dest.TypeText,
opt => opt.MapFrom(src => src.QuestionType.TypeText));
CreateMap<Db.Question, Models.MultiLanguage>().ForMember(dest => dest.TypeText,
opt => opt.MapFrom(src => src.QuestionType.TypeText));
CreateMap<Models.QuestionCategory, Db.QuestionCategory>();
CreateMap<Db.QuestionCategory, Models.QuestionCategory>();
CreateMap<Db.QuestionCategory, Models.MultiLanQuestionCategory>();
CreateMap<Models.Question, Db.Question>();
CreateMap<Db.QuestionsTranslation, Models.QuestionsTranslation>();
CreateMap<Models.QuestionsTranslation, Db.QuestionsTranslation>();
CreateMap<Db.CategoryTranslation, Models.CategoryTranslation>();
CreateMap<Models.CategoryTranslation, Db.CategoryTranslation>();
}
}
}

View File

@ -2,7 +2,7 @@ using DamageAssesment.Api.Questions.Db;
using DamageAssesment.Api.Questions.Interfaces;
using DamageAssesment.Api.Questions.Providers;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
@ -15,9 +15,15 @@ builder.Services.AddControllers();
builder.Services.AddScoped<IQuestionsProvider, QuestionsProvider>();
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
//builder.Services.AddSwaggerGen();
builder.Services.AddSwaggerGen(c =>
{
// Include XML comments from your assembly
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
builder.Services.AddDbContext<QuestionDbContext>(option =>
{
option.UseSqlServer("QuestionConnection");
@ -29,6 +35,13 @@ if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
using (var serviceScope = app.Services.CreateScope())
{
var services = serviceScope.ServiceProvider;
var questionProvider = services.GetRequiredService<IQuestionsProvider>();
questionProvider.SeedData();
}
}
app.UseAuthorization();

View File

@ -2,9 +2,7 @@
using DamageAssesment.Api.Questions.Db;
using DamageAssesment.Api.Questions.Interfaces;
using DamageAssesment.Api.Questions.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
namespace DamageAssesment.Api.Questions.Providers
{
@ -24,18 +22,16 @@ namespace DamageAssesment.Api.Questions.Providers
SeedData();
}
private void SeedData()
public void SeedData()
{
if (!questionDbContext.QuestionCategories.Any())
{
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { CategoryName = "Electrical", CategoryImage = "img1" });
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { CategoryName = "Flooding", CategoryImage = "img1" });
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { CategoryName = "Structural", CategoryImage = "img1" });
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { CategoryName = "Utility", CategoryImage = "img1" });
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { CategoryName = "Debris", CategoryImage = "img1" });
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { IconName = "Electrical", IconLibrary = "img1" });
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { IconName = "Flooding", IconLibrary = "img1" });
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { IconName = "Structural", IconLibrary = "img1" });
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { IconName = "Utility", IconLibrary = "img1" });
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { IconName = "Debris", IconLibrary = "img1" });
questionDbContext.SaveChanges();
}
if (!questionDbContext.QuestionTypes.Any())
@ -47,9 +43,9 @@ namespace DamageAssesment.Api.Questions.Providers
}
if (!questionDbContext.Questions.Any())
{
questionDbContext.Questions.Add(new Db.Question() { QuestionTypeId = 2, SurveyId = 1, QuestionNumber = 1, IsRequired = true, Comment = false, Key = true, QuestionGroup = "group1",CategoryId=1 });
questionDbContext.Questions.Add(new Db.Question() { QuestionTypeId = 3, SurveyId = 1, QuestionNumber = 2, IsRequired = false, Comment = true, Key = false, QuestionGroup = "group1", CategoryId = 1 });
questionDbContext.Questions.Add(new Db.Question() { QuestionTypeId = 1, SurveyId = 1, QuestionNumber = 3, IsRequired = true, Comment = false, Key = true, QuestionGroup = "group1", CategoryId = 2 });
questionDbContext.Questions.Add(new Db.Question() { QuestionTypeId = 2, SurveyId = 1, QuestionNumber = 1, IsRequired = true, Comment = false, Key = true, CategoryId=1 });
questionDbContext.Questions.Add(new Db.Question() { QuestionTypeId = 3, SurveyId = 1, QuestionNumber = 2, IsRequired = false, Comment = true, Key = false, CategoryId = 1 });
questionDbContext.Questions.Add(new Db.Question() { QuestionTypeId = 1, SurveyId = 1, QuestionNumber = 3, IsRequired = true, Comment = false, Key = true, CategoryId = 2 });
questionDbContext.SaveChanges();
}
if (!questionDbContext.QuestionsTranslations.Any())
@ -66,9 +62,81 @@ namespace DamageAssesment.Api.Questions.Providers
questionDbContext.SaveChanges();
}
if (!questionDbContext.CategoryTranslations.Any())
{
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 1, Title = "Flooding", Language = "en" });
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 2, Title = "Electrical", Language = "en" });
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 3, Title = "Structural", Language = "en" });
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 4, Title = "Utility", Language = "en" });
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 5, Title = "Debris", Language = "en" });
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 1, Title = "Inondation", Language = "fr" });
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 2, Title = "Électrique", Language = "fr" });
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 3, Title = "De construction", Language = "fr" });
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 4, Title = "Utilitaire", Language = "fr" });
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 5, Title = "Débris", Language = "fr" });
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 1, Title = "Inundación", Language = "es" });
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 2, Title = "Eléctrica", Language = "es" });
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 3, Title = "Estructural", Language = "es" });
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 4, Title = "Utilidad", Language = "es" });
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 5, Title = "Escombros", Language = "es" });
questionDbContext.SaveChanges();
}
}
public async Task<(bool IsSuccess, IEnumerable<Models.Question> Questions, string ErrorMessage)> GetQuestionsAsync()
public List<Models.CategoryTranslation> GetCategoryTranslations(int id, string? language)
{
List<Models.CategoryTranslation> categoryTranslations = new List<Models.CategoryTranslation>();
if (string.IsNullOrEmpty(language))
{
categoryTranslations = mapper.Map<List<Db.CategoryTranslation>, List<Models.CategoryTranslation>>(
questionDbContext.CategoryTranslations.AsNoTracking().Where(a => a.CategoryId == id).ToList());
}
else
{
categoryTranslations = mapper.Map<List<Db.CategoryTranslation>, List<Models.CategoryTranslation>>(
questionDbContext.CategoryTranslations.AsNoTracking().Where(a => a.CategoryId == id && a.Language == language).ToList());
}
return categoryTranslations;
}
public object CreateCategoryMultiLanguageObject(List<Models.CategoryTranslation> categoryTranslations)
{
object MultiLanguage = new object();
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (Models.CategoryTranslation item in categoryTranslations)
{
dict.Add(item.Language, item.Title);
}
MultiLanguage = dict;
return MultiLanguage;
}
public List<Models.QuestionsTranslation> GetQuestionsTranslations(int id, string? language)
{
List<Models.QuestionsTranslation> QuestionTranslations;
if (string.IsNullOrEmpty(language))
{
QuestionTranslations = mapper.Map<List<Db.QuestionsTranslation>, List<Models.QuestionsTranslation>>(
questionDbContext.QuestionsTranslations.AsNoTracking().Where(a => a.QuestionId == id).ToList());
}
else
{
QuestionTranslations = mapper.Map<List<Db.QuestionsTranslation>, List<Models.QuestionsTranslation>>(
questionDbContext.QuestionsTranslations.AsNoTracking().Where(a => a.QuestionId == id && a.Language == language).ToList());
}
return QuestionTranslations;
}
public Dictionary<string, string> CreateMultiLanguageObject(List<Models.QuestionsTranslation> questions)
{
MultiLanguage MultiLanguage = new MultiLanguage();
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (Models.QuestionsTranslation item in questions)
{
dict.Add(item.Language, item.QuestionText);
}
//MultiLanguage.questionText = dict;
return dict;
}
public async Task<(bool IsSuccess, IEnumerable<Models.MultiLanguage> Questions, string ErrorMessage)> GetQuestionsAsync(string language)
{
try
{
@ -78,11 +146,10 @@ 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)
var result = mapper.Map<IEnumerable<Db.Question>, IEnumerable<Models.MultiLanguage>>(questions);
foreach (var item in result)
{
question.Questions=mapper.Map<List<Db.QuestionsTranslation>,List<Models.QuestionsTranslation>>(
questionDbContext.QuestionsTranslations.Where(a=>a.QuestionId==question.Id).ToList());
item.Text = CreateMultiLanguageObject(GetQuestionsTranslations(item.Id, language));
}
return (true, result, null);
}
@ -94,18 +161,17 @@ 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.MultiLanguage 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<Db.Question, Models.Question>(question);
result.Questions = mapper.Map<List<Db.QuestionsTranslation>, List<Models.QuestionsTranslation>>(
questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == result.Id).ToList());
var result = mapper.Map<Db.Question, Models.MultiLanguage>(question);
result.Text = CreateMultiLanguageObject(GetQuestionsTranslations(id, language));
return (true, result, null);
}
return (false, null, "Not found");
@ -116,35 +182,34 @@ namespace DamageAssesment.Api.Questions.Providers
return (false, null, ex.Message);
}
}
public List<Models.Question> GetSurveyQuestion(List<Models.Question> questions,string Language)
public List<Models.MultiLanguage> GetSurveyQuestion(List<Models.MultiLanguage> questions, string 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.Text = CreateMultiLanguageObject(GetQuestionsTranslations(item.Id, language));
}
return questions;
}
public async Task<(bool IsSuccess, List<SurveyQuestions> SurveyQuestions, string ErrorMessage)> GetSurveyQuestionAsync(int SurveyId, string Language)
public async Task<(bool IsSuccess, List<SurveyQuestions> SurveyQuestions, string ErrorMessage)> GetSurveyQuestionAsync(int SurveyId, string language)
{
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)
{
surveyQuestionsList.Add(new SurveyQuestions()
{
CategoryId = item.Id,
CategoryImage = item.CategoryImage,
CategoryName = item.CategoryName,
Questions = GetSurveyQuestion(mapper.Map<List<Db.Question>, List<Models.Question>>(questions.Where(a => a.CategoryId == item.Id).ToList()), Language)
IconLibrary = item.IconLibrary,
IconName = item.IconName,
QuestionsText = GetSurveyQuestion(mapper.Map<List<Db.Question>, List<Models.MultiLanguage>>(questions.Where(a => a.CategoryId == item.Id).ToList()), language)
});
}
@ -159,20 +224,22 @@ 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.MultiLanguage Question, string ErrorMessage)> PostQuestionAsync(Models.Question Question)
{
try
{
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);
questionDbContext.SaveChanges();
questionDbContext.SaveChanges();
Question.Id = dbquestion.Id;
return (true, Question, null);
var result = mapper.Map<Db.Question, Models.MultiLanguage>(dbquestion);
result.Text = CreateMultiLanguageObject(GetQuestionsTranslations(result.Id,""));
return (true, result, null);
}
catch (Exception ex)
{
@ -180,7 +247,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.MultiLanguage Question, string ErrorMessage)> UpdateQuestionAsync(Models.Question Question)
{
try
{
@ -189,12 +256,14 @@ 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);
questionDbContext.SaveChanges();
return (true, Question, null);
var result = mapper.Map<Db.Question, Models.MultiLanguage>(dbquestion);
result.Text = CreateMultiLanguageObject(GetQuestionsTranslations(result.Id, ""));
return (true, result, null);
}
catch (Exception ex)
{
@ -203,21 +272,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.MultiLanguage 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<Db.Question, Models.MultiLanguage>(question);
result.Text = CreateMultiLanguageObject(GetQuestionsTranslations(result.Id, ""));
questionDbContext.Questions.Remove(question);
questionDbContext.SaveChanges();
return (true, mapper.Map<Db.Question, Models.Question>(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");
}
}
@ -228,11 +299,9 @@ namespace DamageAssesment.Api.Questions.Providers
}
}
//Question Category Logic
public async Task<(bool IsSuccess, IEnumerable<Models.QuestionCategory> QuestionCategories, string ErrorMessage)> GetQuestionCategoriesAsync()
public async Task<(bool IsSuccess, IEnumerable<Models.MultiLanQuestionCategory> QuestionCategories, string ErrorMessage)> GetQuestionCategoriesAsync(string? language)
{
try
{
@ -241,7 +310,11 @@ namespace DamageAssesment.Api.Questions.Providers
if (questionCategories != null)
{
//logger?.LogInformation($"{question} customer(s) found");
var result = mapper.Map<IEnumerable<Db.QuestionCategory>, IEnumerable<Models.QuestionCategory>>(questionCategories);
var result = mapper.Map<IEnumerable<Db.QuestionCategory>, IEnumerable<Models.MultiLanQuestionCategory>>(questionCategories);
foreach (var category in result)
{
category.Titles = CreateCategoryMultiLanguageObject(GetCategoryTranslations(category.Id, language));
}
return (true, result, null);
}
return (false, null, "Not found");
@ -252,16 +325,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<Db.QuestionCategory, Models.QuestionCategory>(questioncategory);
var result = mapper.Map<Db.QuestionCategory, Models.MultiLanQuestionCategory>(questioncategory);
result.Titles = CreateCategoryMultiLanguageObject(GetCategoryTranslations(result.Id, language));
return (true, result, null);
}
return (false, null, "Not found");
@ -272,17 +346,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<Models.QuestionCategory, Db.QuestionCategory>(QuestionCategory);
var dbCategorytranslations = mapper.Map<List<Models.CategoryTranslation>, List<Db.CategoryTranslation>>(QuestionCategory.Categories);
// Question.QuestionType = GetQuestionType(Question.QuestionTypeId);
questionDbContext.QuestionCategories.Add(dbQuestionCategory);
questionDbContext.SaveChanges();
QuestionCategory.Id=dbQuestionCategory.Id;
return (true, QuestionCategory, null);
QuestionCategory.Id = dbQuestionCategory.Id;
dbCategorytranslations.ForEach(i => i.CategoryId = QuestionCategory.Id);
questionDbContext.CategoryTranslations.AddRange(dbCategorytranslations);
questionDbContext.SaveChanges();
var result = mapper.Map<Db.QuestionCategory, Models.MultiLanQuestionCategory>(dbQuestionCategory);
result.Titles = CreateCategoryMultiLanguageObject(GetCategoryTranslations(result.Id, ""));
return (true, result, null);
}
catch (Exception ex)
{
@ -290,15 +370,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<Models.QuestionCategory, Db.QuestionCategory>(QuestionCategory);
var dbCategorytranslations = mapper.Map<List<Models.CategoryTranslation>, List<Db.CategoryTranslation>>(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<Db.QuestionCategory, Models.MultiLanQuestionCategory>(dbQuestionCategory);
result.Titles = CreateCategoryMultiLanguageObject(GetCategoryTranslations(result.Id, ""));
return (true, result, null);
}
catch (Exception ex)
{
@ -307,18 +395,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<Db.QuestionCategory, Models.MultiLanQuestionCategory>(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<Db.QuestionCategory, Models.QuestionCategory>(questioncategory), $"QuestionID {Id} deleted Successfuly");
return (true, result, $"QuestionID {Id} deleted Successfuly");
}
else
{

View File

@ -1,4 +1,7 @@
{
"JwtSettings": {
"securitykey": "bWlhbWkgZGFkZSBzY2hvb2xzIHNlY3JldCBrZXk="
},
"Logging": {
"LogLevel": {
"Default": "Information",