DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Answers/Providers/AnswerProvider.cs
2023-08-18 17:14:41 -04:00

209 lines
7.9 KiB
C#

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;
public AnswersProvider(AnswerDbContext answerDbContext, ILogger<AnswersProvider> logger, IMapper mapper)
{
this.answerDbContext = answerDbContext;
this.logger = logger;
this.mapper = mapper;
SeedData();
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
public async Task<(bool IsSuccess, Models.Answer Answer, string ErrorMessage)> PostAnswerAsync(Db.Answer Answer)
{
try
{
logger?.LogInformation("Query Answer");
if (!AnswerExists(Answer.Id))
{
answerDbContext.Answers.Add(Answer);
answerDbContext.SaveChanges();
var result = mapper.Map<Db.Answer, Models.Answer>(Answer);
return (true, result, null);
}
return (false, null, "Answer is already exits");
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
public async Task<(bool IsSuccess, Models.Answer Answer, string ErrorMessage)> UpdateAnswerAsync(Db.Answer Answer)
{
try
{
if (Answer != null)
{
var existing = answerDbContext.Answers.AsNoTracking().FirstOrDefault(x => x.Id == Answer.Id);
if (existing != null)
{
answerDbContext.Answers.Update(Answer);
answerDbContext.SaveChanges();
return (true, mapper.Map<Db.Answer, Models.Answer>(Answer), "Successful");
}
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());
return (false,null, ex.Message);
}
}
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();
return (true, mapper.Map<Db.Answer, Models.Answer>(answer), $"AnswerId {Id} deleted Successfuly");
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false,null, ex.Message);
}
}
private bool AnswerExists(int id)
{
return answerDbContext.Answers.AsNoTracking().Count(e => e.Id == id) > 0;
}
private void SeedData()
{
if (!answerDbContext.Answers.Any())
{
answerDbContext.Answers.Add(new Db.Answer() { AnswerText = "Yes", Comment = "", QuestionId = 1, SurveyResponseId = 1 });
answerDbContext.Answers.Add(new Db.Answer() { AnswerText = "Yes", 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 });
answerDbContext.SaveChanges();
}
}
}
}