480 lines
27 KiB
C#
480 lines
27 KiB
C#
using AutoMapper;
|
|
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;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
namespace DamageAssesment.Api.Questions.Providers
|
|
{
|
|
|
|
|
|
public class QuestionsProvider : IQuestionsProvider, IQuestionTypesProvider
|
|
{
|
|
private QuestionDbContext questionDbContext;
|
|
private ILogger<QuestionsProvider> logger;
|
|
private IMapper mapper;
|
|
|
|
public QuestionsProvider(QuestionDbContext questionDbContext, ILogger<QuestionsProvider> logger, IMapper mapper)
|
|
{
|
|
this.questionDbContext = questionDbContext;
|
|
this.logger = logger;
|
|
this.mapper = mapper;
|
|
SeedData();
|
|
}
|
|
|
|
public void SeedData()
|
|
{
|
|
|
|
if (!questionDbContext.QuestionsTranslations.Any())
|
|
{
|
|
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 1, QuestionId = 1, QuestionText = "Can You Open ?", Language = "en" });
|
|
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 2, QuestionId = 1, QuestionText = "Peux-tu ouvrir ?", Language = "fr" });
|
|
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 3, QuestionId = 2, QuestionText = "Are the grounds flodded ?", Language = "en" });
|
|
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 4, QuestionId = 2, QuestionText = "Les terrains sont-ils inondés ?", Language = "fr" });
|
|
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 5, QuestionId = 3, QuestionText = "Is the access blocked by flooding ?", Language = "en" });
|
|
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 6, QuestionId = 3, QuestionText = "L'accès est-il bloqué par les inondations ?", Language = "fr" });
|
|
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 7, QuestionId = 1, QuestionText = "Puedes abrir ?", Language = "es" });
|
|
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 8, QuestionId = 2, QuestionText = "¿Están inundados los terrenos?", Language = "es" });
|
|
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 9, QuestionId = 3, QuestionText = "¿El acceso está bloqueado por inundaciones?", Language = "es" });
|
|
questionDbContext.SaveChanges();
|
|
}
|
|
if (!questionDbContext.Questions.Any())
|
|
{
|
|
questionDbContext.Questions.Add(new Db.Question() { Id = 1, QuestionTypeId = 2, SurveyId = 1, QuestionNumber = 1, IsRequired = true, Comment = false, Key = true, CategoryId=1 });
|
|
questionDbContext.Questions.Add(new Db.Question() { Id = 2, QuestionTypeId = 1, SurveyId = 1, QuestionNumber = 2, IsRequired = false, Comment = true, Key = false, CategoryId = 1 });
|
|
questionDbContext.Questions.Add(new Db.Question() { Id = 3, QuestionTypeId = 1, SurveyId = 1, QuestionNumber = 3, IsRequired = true, Comment = false, Key = true, CategoryId = 2 });
|
|
questionDbContext.SaveChanges();
|
|
}
|
|
if (!questionDbContext.QuestionTypes.Any())
|
|
{
|
|
questionDbContext.QuestionTypes.Add(new Db.QuestionType() { Id = 1, TypeText = "RadioButton" });
|
|
questionDbContext.QuestionTypes.Add(new Db.QuestionType() { Id = 2, TypeText = "CheckBox" });
|
|
questionDbContext.QuestionTypes.Add(new Db.QuestionType() { Id = 3, TypeText = "TextBox" });
|
|
questionDbContext.SaveChanges();
|
|
}
|
|
|
|
if (!questionDbContext.QuestionCategories.Any())
|
|
{
|
|
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 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 MultiLanguage 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 MultiLanguage;
|
|
}
|
|
public async Task<(bool IsSuccess, IEnumerable<Models.MultiLanQuestion> Questions, string ErrorMessage)> GetQuestionsAsync(string language)
|
|
{
|
|
try
|
|
{
|
|
logger?.LogInformation("Query Question");
|
|
var questions = await questionDbContext.Questions.Include("QuestionType").AsNoTracking().ToListAsync();
|
|
if (questions != null)
|
|
{
|
|
|
|
//logger?.LogInformation($"{question} customer(s) found");
|
|
var result = mapper.Map<IEnumerable<Db.Question>, IEnumerable<Models.MultiLanQuestion>>(questions);
|
|
foreach (var question in result)
|
|
{
|
|
question.Questions = CreateMultiLanguageObject(GetQuestionsTranslations(question.Id, language));
|
|
}
|
|
return (true, result, null);
|
|
}
|
|
return (false, null, "Not found");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger?.LogError(ex.ToString());
|
|
return (false, null, ex.Message);
|
|
}
|
|
}
|
|
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);
|
|
if (question != null)
|
|
{
|
|
logger?.LogInformation($"{question} customer(s) found");
|
|
var result = mapper.Map<Db.Question, Models.MultiLanQuestion>(question);
|
|
result.Questions = CreateMultiLanguageObject(GetQuestionsTranslations(id, language));
|
|
return (true, result, null);
|
|
}
|
|
return (false, null, "Not found");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger?.LogError(ex.ToString());
|
|
return (false, null, ex.Message);
|
|
}
|
|
}
|
|
public List<Models.MultiLanQuestion> GetSurveyQuestion(List<Models.MultiLanQuestion> questions, string language)
|
|
{
|
|
foreach (var item in questions)
|
|
{
|
|
item.Questions = CreateMultiLanguageObject(GetQuestionsTranslations(item.Id, language));
|
|
}
|
|
return questions;
|
|
}
|
|
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();
|
|
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();
|
|
//logger?.LogInformation($"{question} customer(s) found");
|
|
foreach (var item in questioncategories)
|
|
{
|
|
surveyQuestionsList.Add(new SurveyQuestions()
|
|
{
|
|
CategoryId = item.Id,
|
|
IconLibrary = item.IconLibrary,
|
|
IconName = item.IconName,
|
|
Questions = GetSurveyQuestion(mapper.Map<List<Db.Question>, List<Models.MultiLanQuestion>>(questions.Where(a => a.CategoryId == item.Id).ToList()), language)
|
|
});
|
|
}
|
|
|
|
//var result = mapper.Map<IEnumerable<Db.Question>, IEnumerable<Models.Question>>(questions);
|
|
return (true, surveyQuestionsList, null);
|
|
}
|
|
return (false, null, "Not found");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger?.LogError(ex.ToString());
|
|
return (false, null, ex.Message);
|
|
}
|
|
}
|
|
public async Task<(bool IsSuccess, Models.MultiLanQuestion 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();
|
|
questionDbContext.Questions.Add(dbquestion);
|
|
dbquestiontranslation.ForEach(i => i.QuestionId = dbquestion.Id);
|
|
questionDbContext.QuestionsTranslations.AddRange(dbquestiontranslation);
|
|
questionDbContext.SaveChanges();
|
|
Question.Id = dbquestion.Id;
|
|
var result = mapper.Map<Db.Question, Models.MultiLanQuestion>(dbquestion);
|
|
result.Questions = CreateMultiLanguageObject(GetQuestionsTranslations(result.Id,""));
|
|
return (true, result, null);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger?.LogError(ex.ToString());
|
|
return (false, null, ex.Message);
|
|
}
|
|
}
|
|
public async Task<(bool IsSuccess, Models.MultiLanQuestion Question, string ErrorMessage)> UpdateQuestionAsync(Models.Question Question)
|
|
{
|
|
try
|
|
{
|
|
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();
|
|
questionDbContext.Entry(dbquestion).State = EntityState.Modified;
|
|
var oldquestions = questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == dbquestion.Id).ToList();
|
|
if (oldquestions != null)
|
|
questionDbContext.QuestionsTranslations.RemoveRange(oldquestions);
|
|
dbquestiontranslation.ForEach(i => i.QuestionId = dbquestion.Id);
|
|
questionDbContext.QuestionsTranslations.AddRange(dbquestiontranslation);
|
|
questionDbContext.SaveChanges();
|
|
var result = mapper.Map<Db.Question, Models.MultiLanQuestion>(dbquestion);
|
|
result.Questions = CreateMultiLanguageObject(GetQuestionsTranslations(result.Id, ""));
|
|
return (true, result, null);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
logger?.LogError(ex.ToString());
|
|
return (false, null, ex.Message);
|
|
}
|
|
}
|
|
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();
|
|
|
|
if (question != null)
|
|
{
|
|
var result = mapper.Map<Db.Question, Models.MultiLanQuestion>(question);
|
|
result.Questions = CreateMultiLanguageObject(GetQuestionsTranslations(result.Id, ""));
|
|
questionDbContext.Questions.Remove(question);
|
|
questionDbContext.SaveChanges();
|
|
return (true, result, $"QuestionID {id} deleted Successfuly");
|
|
}
|
|
else
|
|
{
|
|
logger?.LogInformation($"QuestionID: {id} Not found");
|
|
return (false, null, "Not Found");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger?.LogError(ex.ToString());
|
|
return (false, null, ex.Message);
|
|
}
|
|
}
|
|
|
|
//Question Category Logic
|
|
|
|
public async Task<(bool IsSuccess, IEnumerable<Models.MultiLanQuestionCategory> QuestionCategories, string ErrorMessage)> GetQuestionCategoriesAsync(string? language)
|
|
{
|
|
try
|
|
{
|
|
logger?.LogInformation("Query Question");
|
|
var questionCategories = await questionDbContext.QuestionCategories.AsNoTracking().ToListAsync();
|
|
if (questionCategories != null)
|
|
{
|
|
//logger?.LogInformation($"{question} customer(s) found");
|
|
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");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger?.LogError(ex.ToString());
|
|
return (false, null, ex.Message);
|
|
}
|
|
}
|
|
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);
|
|
if (questioncategory != null)
|
|
{
|
|
logger?.LogInformation($"{questioncategory} customer(s) found");
|
|
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");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger?.LogError(ex.ToString());
|
|
return (false, null, ex.Message);
|
|
}
|
|
}
|
|
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;
|
|
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)
|
|
{
|
|
logger?.LogError(ex.ToString());
|
|
return (false, null, ex.Message);
|
|
}
|
|
}
|
|
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();
|
|
var result = mapper.Map<Db.QuestionCategory, Models.MultiLanQuestionCategory>(dbQuestionCategory);
|
|
result.Titles = CreateCategoryMultiLanguageObject(GetCategoryTranslations(result.Id, ""));
|
|
return (true, result, null);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
logger?.LogError(ex.ToString());
|
|
return (false, null, ex.Message);
|
|
}
|
|
}
|
|
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, result, $"QuestionID {Id} deleted Successfuly");
|
|
}
|
|
else
|
|
{
|
|
logger?.LogInformation($"QuestionID: {Id} Not found");
|
|
return (false, null, "Not Found");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger?.LogError(ex.ToString());
|
|
return (false, null, ex.Message);
|
|
}
|
|
}
|
|
private bool QuestionExists(int id)
|
|
{
|
|
return questionDbContext.Questions.AsNoTracking().Count(e => e.Id == id) > 0;
|
|
}
|
|
private QuestionType GetQuestionType(int Id)
|
|
{
|
|
return questionDbContext.QuestionTypes.Where(a => a.Id == Id).FirstOrDefault();
|
|
}
|
|
|
|
public async Task<(bool IsSuccess, QuestionType QuestionType, string ErrorMessage)> GetQuestionTypeAsync(int Id)
|
|
{
|
|
try
|
|
{
|
|
logger?.LogInformation("Query Question");
|
|
var questiontype = await questionDbContext.QuestionTypes.FirstOrDefaultAsync(q => q.Id == Id);
|
|
if (questiontype != null)
|
|
{
|
|
logger?.LogInformation($"{questiontype} customer(s) found");
|
|
return (true, questiontype, null);
|
|
}
|
|
return (false, null, "Not found");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger?.LogError(ex.ToString());
|
|
return (false, null, ex.Message);
|
|
}
|
|
}
|
|
|
|
public async Task<(bool IsSuccess, IEnumerable<QuestionType> QuestionTypes, string ErrorMessage)> GetQuestionTypesAsync()
|
|
{
|
|
try
|
|
{
|
|
logger?.LogInformation("Query Question");
|
|
var questionTypes = await questionDbContext.QuestionTypes.ToListAsync();
|
|
if (questionTypes != null)
|
|
{
|
|
//logger?.LogInformation($"{question} customer(s) found");
|
|
return (true, questionTypes, null);
|
|
}
|
|
return (false, null, "Not found");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger?.LogError(ex.ToString());
|
|
return (false, null, ex.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|