DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs

161 lines
6.3 KiB
C#
Raw Permalink Normal View History

2023-08-15 22:52:30 -05:00
using AutoMapper;
using DamageAssesment.Api.Surveys.Db;
using DamageAssesment.Api.Surveys.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace DamageAssesment.Api.Surveys.Providers
{
public class SurveysProvider : ISurveyProvider
{
private readonly SurveysDbContext surveyDbContext;
private readonly ILogger<SurveysProvider> logger;
private readonly IMapper mapper;
public SurveysProvider(SurveysDbContext surveysDbContext, ILogger<SurveysProvider> logger, IMapper mapper)
{
this.surveyDbContext = surveysDbContext;
this.logger = logger;
this.mapper = mapper;
seedData();
}
private void seedData()
{
if (!surveyDbContext.Surveys.Any())
{
surveyDbContext.Surveys.Add(new Db.Survey { Id = 1, Title = "Sample Survey Title:Damage Assesment 2014", IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) });
surveyDbContext.Surveys.Add(new Db.Survey { Id = 2, Title = "Sample Survey Title: Damage Assesment 2016", IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) });
surveyDbContext.Surveys.Add(new Db.Survey { Id = 3, Title = "Sample Survey Title: Damage Assesment 2018", IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) });
surveyDbContext.SaveChanges();
}
}
public async Task<(bool IsSuccess, IEnumerable<Models.Survey> Surveys, string ErrorMessage)> GetSurveysAsync()
{
try
{
logger?.LogInformation("Gell all Surveys from DB");
var surveys = await surveyDbContext.Surveys.ToListAsync();
if (surveys != null)
{
logger?.LogInformation($"{surveys.Count} Items(s) found");
var result = mapper.Map<IEnumerable<Db.Survey>, IEnumerable<Models.Survey>>(surveys);
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.Survey Surveys, string ErrorMessage)> GetSurveysAsync(int Id)
{
try
{
logger?.LogInformation("Query Survey");
var survey = await surveyDbContext.Surveys.SingleOrDefaultAsync(s => s.Id == Id);
if (survey != null)
{
logger?.LogInformation($"Survey Id: {Id} found");
var result = mapper.Map<Db.Survey, Models.Survey>(survey);
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.Survey Survey, string ErrorMessage)> PostSurveyAsync(Models.Survey survey)
{
try
{
if (survey != null)
{
var surveys = await surveyDbContext.Surveys.ToListAsync();
survey.Id = surveys.Count + 1;
surveyDbContext.Surveys.Add(mapper.Map<Models.Survey, Db.Survey>(survey));
surveyDbContext.SaveChanges();
return (true, survey, "Successful");
}
else
{
logger?.LogInformation($"Survey Id: {survey.Id} cannot be added");
return (false, null, $"Survey Id: {survey.Id} cannot be added");
}
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
public async Task<(bool IsSuccess, Models.Survey Survey, string ErrorMessage)> PutSurveyAsync(int Id, Models.Survey survey)
{
try
{
if (survey != null)
{
var _survey = await surveyDbContext.Surveys.Where(s => s.Id == Id).SingleOrDefaultAsync();
if (_survey != null)
{
_survey.Title = survey.Title;
_survey.IsEnabled = survey.IsEnabled;
_survey.StartDate = survey.StartDate;
_survey.EndDate = survey.EndDate;
surveyDbContext.SaveChanges();
return (true, mapper.Map<Db.Survey, Models.Survey>(_survey), "Successful");
}
else
{
logger?.LogInformation($"Survey Id : {Id} Not found");
return (false, null, "Not Found");
}
}
else
{
logger?.LogInformation($"Survey Id: {Id} 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.Survey Survey, string ErrorMessage)> DeleteSurveyAsync(int Id)
{
try
{
var survey = await surveyDbContext.Surveys.Where(x => x.Id == Id).SingleOrDefaultAsync();
if (survey != null)
{
surveyDbContext.Surveys.Remove(survey);
surveyDbContext.SaveChanges();
return (true, mapper.Map<Db.Survey, Models.Survey>(survey), $"Survey Id: {Id} deleted Successfuly");
}
else
{
logger?.LogInformation($"Survey Id : {Id} Not found");
return (false, null, "Not Found");
}
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
}
}