DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Answers/Providers/AnswerProvider.cs

212 lines
8.6 KiB
C#
Raw Normal View History

2023-08-15 22:52:30 -05:00
using AutoMapper;
using DamageAssesment.Api.Answers.Db;
using DamageAssesment.Api.Answers.Interfaces;
using DamageAssesment.Api.Answers.Models;
using Microsoft.EntityFrameworkCore;
namespace DamageAssesment.Api.Answers.Providers
{
public class AnswersProvider : IAnswersProvider
{
private AnswerDbContext answerDbContext;
private ILogger<AnswersProvider> logger;
private IMapper mapper;
2023-10-06 17:22:37 -05:00
// Constructor with dependency injection and data seeding
2023-08-15 22:52:30 -05:00
public AnswersProvider(AnswerDbContext answerDbContext, ILogger<AnswersProvider> logger, IMapper mapper)
{
this.answerDbContext = answerDbContext;
this.logger = logger;
this.mapper = mapper;
2023-10-06 17:22:37 -05:00
SeedData(); // Seed initial data if the table is empty
2023-08-15 22:52:30 -05:00
}
2023-10-06 17:22:37 -05:00
// Get all answers
2023-08-15 22:52:30 -05:00
public async Task<(bool IsSuccess, IEnumerable<Models.Answer> Answers, string ErrorMessage)> GetAnswersAsync()
{
try
{
logger?.LogInformation("Query Question");
var answer = await answerDbContext.Answers.AsNoTracking().ToListAsync();
if (answer != null)
{
logger?.LogInformation($"{answer.Count} Answers(s) found");
var result = mapper.Map<IEnumerable<Db.Answer>, IEnumerable<Models.Answer>>(answer);
return (true, result, null);
}
return (false, null, "Not found");
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
2023-10-06 17:22:37 -05:00
// Get an answer by its ID
2023-08-15 22:52:30 -05:00
public async Task<(bool IsSuccess, Models.Answer Answer, string ErrorMessage)> GetAnswerByIdAsync(int Id)
{
try
{
logger?.LogInformation("Query Answer");
var answer = await answerDbContext.Answers.AsNoTracking().FirstOrDefaultAsync(q => q.Id == Id);
if (answer != null)
{
logger?.LogInformation($"{answer} customer(s) found");
var result = mapper.Map<Db.Answer, Models.Answer>(answer);
return (true, result, null);
}
return (false, null, "Not found");
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
2023-10-06 17:22:37 -05:00
// Get answers by survey response ID
2023-08-15 22:52:30 -05:00
public async Task<(bool IsSuccess, IEnumerable<Models.Answer> Answers, string ErrorMessage)> GetAnswersAsync(int surveyResponseId)
{
try
{
var respAnswers = await answerDbContext.Answers.AsNoTracking()
.Where(a => a.SurveyResponseId == surveyResponseId).AsNoTracking()
.ToListAsync();
if (respAnswers != null)
{
var result = mapper.Map<IEnumerable<Db.Answer>, IEnumerable<Models.Answer>>(respAnswers);
return (true, result, null);
}
return (false, null, "Not Found");
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
2023-10-06 17:22:37 -05:00
// Get answers by question ID
2023-08-15 22:52:30 -05:00
public async Task<(bool IsSuccess, IEnumerable<Models.Answer> Answers, string ErrorMessage)> GetAnswersByQuestionAsync(int questionId)
{
try
{
var respAnswers = await answerDbContext.Answers.AsNoTracking()
.Where(a => a.QuestionId == questionId).AsNoTracking()
.ToListAsync();
if (respAnswers != null)
{
var result = mapper.Map<IEnumerable<Db.Answer>, IEnumerable<Models.Answer>>(respAnswers);
return (true, result, null);
}
return (false, null, "Not Found");
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
2023-10-06 17:22:37 -05:00
// Create a new answer
public async Task<(bool IsSuccess, Models.Answer Answer, string ErrorMessage)> PostAnswerAsync(Models.Answer Answer)
2023-08-15 22:52:30 -05:00
{
try
{
logger?.LogInformation("Query Answer");
if (!AnswerExists(Answer.Id))
{
2023-08-25 17:24:46 -05:00
Db.Answer answer = mapper.Map<Models.Answer, Db.Answer>(Answer);
answerDbContext.Answers.Add(answer);
await answerDbContext.SaveChangesAsync();
2023-08-25 17:24:46 -05:00
var result = mapper.Map<Db.Answer, Models.Answer>(answer);
2023-08-15 22:52:30 -05:00
return (true, result, null);
}
2023-10-06 17:22:37 -05:00
return (false, null, "Answer is already exists");
2023-08-15 22:52:30 -05:00
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
2023-10-06 17:22:37 -05:00
// Update an existing answer
public async Task<(bool IsSuccess, Models.Answer Answer, string ErrorMessage)> UpdateAnswerAsync(Models.Answer Answer)
2023-08-15 22:52:30 -05:00
{
try
{
if (Answer != null)
{
var existing = answerDbContext.Answers.AsNoTracking().FirstOrDefault(x => x.Id == Answer.Id);
if (existing != null)
{
2023-08-25 17:24:46 -05:00
Db.Answer answer = mapper.Map<Models.Answer, Db.Answer>(Answer);
answerDbContext.Answers.Update(answer);
await answerDbContext.SaveChangesAsync();
2023-08-25 17:24:46 -05:00
return (true, mapper.Map<Db.Answer, Models.Answer>(answer), "Successful");
2023-08-15 22:52:30 -05:00
}
else
{
logger?.LogInformation($"{Answer} Not found");
return (false, null, "Not Found");
}
}
else
{
logger?.LogInformation($"{Answer} Bad Request");
return (false, null, "Bad request");
}
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
2023-10-06 17:22:37 -05:00
return (false, null, ex.Message);
2023-08-15 22:52:30 -05:00
}
}
2023-10-06 17:22:37 -05:00
// Delete an answer by its ID
2023-08-15 22:52:30 -05:00
public async Task<(bool IsSuccess, Models.Answer Answer, string ErrorMessage)> DeleteAnswerAsync(int Id)
{
try
{
Db.Answer answer = answerDbContext.Answers.AsNoTracking().Where(a => a.Id == Id).FirstOrDefault();
if (answer == null)
{
return (false, null, "Not Found");
}
answerDbContext.Answers.Remove(answer);
answerDbContext.SaveChanges();
2023-10-06 17:22:37 -05:00
return (true, mapper.Map<Db.Answer, Models.Answer>(answer), $"AnswerId {Id} deleted successfully");
2023-08-15 22:52:30 -05:00
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
2023-10-06 17:22:37 -05:00
return (false, null, ex.Message);
2023-08-15 22:52:30 -05:00
}
}
2023-10-06 17:22:37 -05:00
// Check if an answer with a specific ID exists
2023-08-15 22:52:30 -05:00
private bool AnswerExists(int id)
{
return answerDbContext.Answers.AsNoTracking().Count(e => e.Id == id) > 0;
}
2023-10-06 17:22:37 -05:00
// Seed initial data if the table is empty
public void SeedData()
2023-08-15 22:52:30 -05:00
{
if (!answerDbContext.Answers.Any())
{
2023-08-18 16:14:41 -05:00
answerDbContext.Answers.Add(new Db.Answer() { AnswerText = "Yes", Comment = "", QuestionId = 1, SurveyResponseId = 1 });
2023-10-10 15:28:30 -05:00
answerDbContext.Answers.Add(new Db.Answer() { AnswerText = "No", Comment = "myComment", QuestionId = 2, SurveyResponseId = 1 });
//answerDbContext.Answers.Add(new Db.Answer() { AnswerText = "No", Comment = "No Comment", QuestionId = 3, SurveyResponseId = 1 });
//answerDbContext.Answers.Add(new Db.Answer() { AnswerText = "Yes", Comment = "No Comment", QuestionId = 1, SurveyResponseId = 2 });
//answerDbContext.Answers.Add(new Db.Answer() { AnswerText = "No", Comment = "No Comment", QuestionId = 2, SurveyResponseId = 2 });
//answerDbContext.Answers.Add(new Db.Answer() { AnswerText = "No", Comment = "No Comment", QuestionId = 3, SurveyResponseId = 2 });
2023-08-15 22:52:30 -05:00
answerDbContext.SaveChanges();
}
}
}
}