Copy from old Repository

This commit is contained in:
Santhosh S
2023-08-15 23:52:30 -04:00
parent 93ef278429
commit 4160c2300b
160 changed files with 8796 additions and 19 deletions

View File

@ -0,0 +1,171 @@
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 Microsoft.AspNetCore.Mvc;
namespace DamageAssesment.Api.Questions.Controllers
{
[Route("api")]
[ApiController]
public class QuestionsController : ControllerBase
{
private readonly IQuestionsProvider questionsProvider;
public QuestionsController(IQuestionsProvider questionsProvider)
{
this.questionsProvider = questionsProvider;
}
// get all questions
[HttpGet("Questions")]
public async Task<IActionResult> GetQuestionsAsync()
{
var result = await this.questionsProvider.GetQuestionsAsync();
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)
{
var result = await this.questionsProvider.GetQuestionAsync(id);
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)
{
if (string.IsNullOrEmpty(Language)) Language = "en";
var result = await this.questionsProvider.GetSurveyQuestionAsync(surveyId, Language);
if (result.IsSuccess)
{
return Ok(result.SurveyQuestions);
}
return NotFound();
}
//update existing question
[HttpPut("Questions")]
public async Task<IActionResult> UpdateQuestion(Models.Question question)
{
if (question != null)
{
var result = await this.questionsProvider.UpdateQuestionAsync(question);
if (result.IsSuccess)
{
return Ok(result.Question);
}
if (result.ErrorMessage == "Not Found")
return NotFound(result.ErrorMessage);
return BadRequest(result.ErrorMessage);
}
return CreatedAtRoute("DefaultApi", new { id = question.Id }, question);
}
//save new question
[HttpPost("Questions")]
public async Task<IActionResult> CreateQuestion(Models.Question question)
{
if (question != null)
{
var result = await this.questionsProvider.PostQuestionAsync(question);
if (result.IsSuccess)
{
return Ok(result.Question);
}
return BadRequest(result.ErrorMessage);
}
return CreatedAtRoute("DefaultApi", new { id = question.Id }, question);
}
// delete existing question
[HttpDelete("Questions/{id}")]
public async Task<IActionResult> DeleteQuestion(int id)
{
var result = await this.questionsProvider.DeleteQuestionAsync(id);
if (result.IsSuccess)
{
return Ok(result.Question);
}
return NotFound();
}
// get all questions
[HttpGet("QuestionCategories")]
public async Task<IActionResult> GetQuestionCategoriesAsync()
{
var result = await this.questionsProvider.GetQuestionCategoriesAsync();
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)
{
var result = await this.questionsProvider.GetQuestionCategoryAsync(id);
if (result.IsSuccess)
{
return Ok(result.QuestionCategory);
}
return NotFound();
}
//update existing question
[HttpPut("QuestionCategories")]
public async Task<IActionResult> UpdateQuestionCategory(Models.QuestionCategory questionCategory)
{
if (questionCategory != null)
{
var result = await this.questionsProvider.UpdateQuestionCategoryAsync(questionCategory);
if (result.IsSuccess)
{
return Ok(result.QuestionCategory);
}
if (result.ErrorMessage == "Not Found")
return NotFound(result.ErrorMessage);
return BadRequest(result.ErrorMessage);
}
return CreatedAtRoute("DefaultApi", new { id = questionCategory.Id }, questionCategory);
}
//save new question
[HttpPost("QuestionCategories")]
public async Task<IActionResult> CreateQuestionCategory(Models.QuestionCategory questionCategory)
{
if (questionCategory != null)
{
var result = await this.questionsProvider.PostQuestionCategoryAsync(questionCategory);
if (result.IsSuccess)
{
return Ok(result.QuestionCategory);
}
return BadRequest(result.ErrorMessage);
}
return CreatedAtRoute("DefaultApi", new { id = questionCategory.Id }, questionCategory);
}
// delete existing question
[HttpDelete("QuestionCategories/{id}")]
public async Task<IActionResult> DeleteQuestionCategory(int id)
{
var result = await this.questionsProvider.DeleteQuestionCategoryAsync(id);
if (result.IsSuccess)
{
return Ok(result.QuestionCategory);
}
return NotFound();
}
}
}

View File

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<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>
</Project>

View File

@ -0,0 +1,29 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace DamageAssesment.Api.Questions.Db
{
public class Question
{
[Key]
public int Id { get; set; }
[ForeignKey("QuestionType")]
public int QuestionTypeId { get; set; }
public QuestionType? QuestionType { get; set; }
//uncomment below propertiers
public int QuestionNumber { get; set; }
public bool IsRequired { get; set; }
public bool Comment { get; set; } //if Comment is true answer has user comment (survey response)
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

@ -0,0 +1,14 @@
using System.Buffers.Text;
using System.ComponentModel.DataAnnotations;
namespace DamageAssesment.Api.Questions.Db
{
public class QuestionCategory
{
[Key]
public int Id { get; set; }
public string CategoryName { get; set; }
public string CategoryImage { get; set; }
}
}

View File

@ -0,0 +1,25 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
namespace DamageAssesment.Api.Questions.Db
{
public class QuestionDbContext : DbContext
{
public DbSet<Db.Question> Questions { get; set; }
public DbSet<Db.QuestionType> QuestionTypes { get; set; }
public DbSet<Db.QuestionsTranslation> QuestionsTranslations { get; set; }
public DbSet<Db.QuestionCategory> QuestionCategories { get; set; }
public QuestionDbContext(DbContextOptions options) : base(options)
{
}
//protected override void OnModelCreating(ModelBuilder modelBuilder)
//{
// modelBuilder.Entity<Question>()
// .HasOne(a => a.QuestionType)
// .WithOne(b => b.Question)
// .HasForeignKey<QuestionType>(b => b.QuestionTypeID);
//}
}
}

View File

@ -0,0 +1,11 @@
using System.ComponentModel.DataAnnotations;
namespace DamageAssesment.Api.Questions.Db
{
public class QuestionType
{
[Key]
public int Id { get; set; }
public string TypeText { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace DamageAssesment.Api.Questions.Db
{
public class QuestionsTranslation
{
[Key]
public int Id { get; set; }
[ForeignKey("Question")]
public int QuestionId { get; set; }
public string QuestionText { get; set; }
public string Language { get; set; }
}
}

View File

@ -0,0 +1,8 @@
namespace DamageAssesment.Api.Questions.Interfaces
{
public interface IQuestionTypesProvider
{
Task<(bool IsSuccess, Db.QuestionType QuestionType, string ErrorMessage)> GetQuestionTypeAsync(int Id);
Task<(bool IsSuccess, IEnumerable<Db.QuestionType> QuestionTypes, string ErrorMessage)> GetQuestionTypesAsync();
}
}

View File

@ -0,0 +1,21 @@
using DamageAssesment.Api.Questions.Models;
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, 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);
}
}

View File

@ -0,0 +1,24 @@
using System.ComponentModel.DataAnnotations;
namespace DamageAssesment.Api.Questions.Models
{
public class Question
{
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

@ -0,0 +1,9 @@
namespace DamageAssesment.Api.Questions.Models
{
public class QuestionCategory
{
public int Id { get; set; }
public string CategoryName { get; set; }
public string CategoryImage { get; set; }
}
}

View File

@ -0,0 +1,8 @@
namespace DamageAssesment.Api.Questions.Models
{
public class QuestionsTranslation
{
public string QuestionText { get; set; }
public string Language { get; set; } = "En";
}
}

View File

@ -0,0 +1,10 @@
namespace DamageAssesment.Api.Questions.Models
{
public class SurveyQuestions
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public string CategoryImage { get; set; }
public List<Question> Questions { get; set; }
}
}

View File

@ -0,0 +1,18 @@
using AutoMapper;
namespace DamageAssesment.Api.Questions.Profiles
{
public class QuestionProfile : AutoMapper.Profile
{
public QuestionProfile()
{
CreateMap<Db.Question, Models.Question>().ForMember(dest => dest.TypeText,
opt => opt.MapFrom(src => src.QuestionType.TypeText));
CreateMap<Models.QuestionCategory, Db.QuestionCategory>();
CreateMap<Db.QuestionCategory, Models.QuestionCategory>();
CreateMap<Models.Question, Db.Question>();
CreateMap<Db.QuestionsTranslation, Models.QuestionsTranslation>();
CreateMap<Models.QuestionsTranslation, Db.QuestionsTranslation>();
}
}
}

View File

@ -0,0 +1,38 @@
using DamageAssesment.Api.Questions.Db;
using DamageAssesment.Api.Questions.Interfaces;
using DamageAssesment.Api.Questions.Providers;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddScoped<IQuestionsProvider, QuestionsProvider>();
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<QuestionDbContext>(option =>
{
option.UseInMemoryDatabase("Questions");
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:60754",
"sslPort": 0
}
},
"profiles": {
"DamageAssesment.Api.Questions": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5133",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@ -0,0 +1,387 @@
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;
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();
}
private 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 = "sp" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 8, QuestionId = 2, QuestionText = "¿Están inundados los terrenos?", Language = "sp" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { Id = 9, QuestionId = 3, QuestionText = "¿El acceso está bloqueado por inundaciones?", Language = "sp" });
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, QuestionGroup = "group1",CategoryId=1 });
questionDbContext.Questions.Add(new Db.Question() { Id = 2, QuestionTypeId = 1, SurveyId = 1, QuestionNumber = 2, IsRequired = false, Comment = true, Key = false, QuestionGroup = "group1", CategoryId = 1 });
questionDbContext.Questions.Add(new Db.Question() { Id = 3, QuestionTypeId = 1, SurveyId = 1, QuestionNumber = 3, IsRequired = true, Comment = false, Key = true, QuestionGroup = "group1", CategoryId = 2 });
questionDbContext.SaveChanges();
}
if (!questionDbContext.QuestionTypes.Any())
{
questionDbContext.QuestionTypes.Add(new Db.QuestionType() { Id = 1, TypeText = "Text 1" });
questionDbContext.QuestionTypes.Add(new Db.QuestionType() { Id = 2, TypeText = "Text 2" });
questionDbContext.QuestionTypes.Add(new Db.QuestionType() { Id = 3, TypeText = "Text 3" });
questionDbContext.QuestionTypes.Add(new Db.QuestionType() { Id = 4, TypeText = "Text 4" });
questionDbContext.QuestionTypes.Add(new Db.QuestionType() { Id = 5, TypeText = "Text 5" });
questionDbContext.SaveChanges();
}
if (!questionDbContext.QuestionCategories.Any())
{
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 1, CategoryName = "Category 1", CategoryImage="img1" });
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 2, CategoryName = "Category 2", CategoryImage = "img1" });
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 3, CategoryName = "Category 3", CategoryImage = "img1" });
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 4, CategoryName = "Category 4", CategoryImage = "img1" });
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { Id = 5, CategoryName = "Category 5", CategoryImage = "img1" });
questionDbContext.SaveChanges();
}
}
public async Task<(bool IsSuccess, IEnumerable<Models.Question> Questions, string ErrorMessage)> GetQuestionsAsync()
{
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.Question>>(questions);
foreach (var question in result)
{
question.Questions=mapper.Map<List<Db.QuestionsTranslation>,List<Models.QuestionsTranslation>>(
questionDbContext.QuestionsTranslations.Where(a=>a.QuestionId==question.Id).ToList());
}
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.Question Question, string ErrorMessage)> GetQuestionAsync(int Id)
{
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.Question>(question);
result.Questions = mapper.Map<List<Db.QuestionsTranslation>, List<Models.QuestionsTranslation>>(
questionDbContext.QuestionsTranslations.Where(a => a.QuestionId == result.Id).ToList());
return (true, result, null);
}
return (false, null, "Not found");
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
public List<Models.Question> GetSurveyQuestion(List<Models.Question> 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());
}
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,
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)
});
}
//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.Question 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;
return (true, Question, null);
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
public async Task<(bool IsSuccess, Models.Question 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();
return (true, Question, null);
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
public async Task<(bool IsSuccess, Models.Question Question, string ErrorMessage)> DeleteQuestionAsync(int Id)
{
try
{
var question = await questionDbContext.Questions.Where(x => x.Id == Id).FirstOrDefaultAsync();
if (question != null)
{
questionDbContext.Questions.Remove(question);
questionDbContext.SaveChanges();
return (true, mapper.Map<Db.Question, Models.Question>(question), $"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.QuestionCategory> QuestionCategories, string ErrorMessage)> GetQuestionCategoriesAsync()
{
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.QuestionCategory>>(questionCategories);
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.QuestionCategory QuestionCategory, string ErrorMessage)> GetQuestionCategoryAsync(int Id)
{
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.QuestionCategory>(questioncategory);
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.QuestionCategory QuestionCategory, string ErrorMessage)> PostQuestionCategoryAsync(Models.QuestionCategory QuestionCategory)
{
try
{
logger?.LogInformation("Query Question");
var dbQuestionCategory = mapper.Map<Models.QuestionCategory, Db.QuestionCategory>(QuestionCategory);
// Question.QuestionType = GetQuestionType(Question.QuestionTypeId);
questionDbContext.QuestionCategories.Add(dbQuestionCategory);
questionDbContext.SaveChanges();
QuestionCategory.Id=dbQuestionCategory.Id;
return (true, QuestionCategory, null);
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
public async Task<(bool IsSuccess, Models.QuestionCategory QuestionCategory, string ErrorMessage)> UpdateQuestionCategoryAsync(Models.QuestionCategory QuestionCategory)
{
try
{
var dbQuestionCategory = mapper.Map<Models.QuestionCategory, Db.QuestionCategory>(QuestionCategory);
questionDbContext.Entry(dbQuestionCategory).State = EntityState.Modified;
questionDbContext.SaveChanges();
return (true, QuestionCategory, null);
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
public async Task<(bool IsSuccess, Models.QuestionCategory QuestionCategory, string ErrorMessage)> DeleteQuestionCategoryAsync(int Id)
{
try
{
var questioncategory = await questionDbContext.QuestionCategories.Where(x => x.Id == Id).FirstOrDefaultAsync();
if (questioncategory != null)
{
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");
}
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);
}
}
}
}

View File

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}