2023-08-15 22:52:30 -05:00
using AutoMapper ;
using DamageAssesment.Api.Surveys.Db ;
using DamageAssesment.Api.Surveys.Interfaces ;
2023-08-25 07:55:11 -05:00
using DamageAssesment.Api.Surveys.Models ;
2023-08-15 22:52:30 -05:00
using Microsoft.EntityFrameworkCore ;
2023-08-25 07:55:11 -05:00
using System.Collections.Generic ;
2023-08-15 22:52:30 -05:00
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 ( ) )
{
2023-08-25 07:55:11 -05:00
surveyDbContext . Surveys . Add ( new Db . Survey { Id = 1 , IsEnabled = true , StartDate = DateTime . Now , EndDate = DateTime . Now . AddDays ( 90 ) , CreatedDate = DateTime . Now } ) ;
surveyDbContext . Surveys . Add ( new Db . Survey { Id = 2 , IsEnabled = true , StartDate = DateTime . Now , EndDate = DateTime . Now . AddDays ( 90 ) , CreatedDate = DateTime . Now } ) ;
surveyDbContext . Surveys . Add ( new Db . Survey { Id = 3 , IsEnabled = true , StartDate = DateTime . Now , EndDate = DateTime . Now . AddDays ( 90 ) , CreatedDate = DateTime . Now } ) ;
surveyDbContext . SaveChangesAsync ( ) ;
}
if ( ! surveyDbContext . SurveysTranslation . Any ( ) )
{
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { Id = 1 , SurveyId = 1 , Language = "en" , Title = "Impact of Tropical Storm Emily on Florida's Economy" } ) ;
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { Id = 2 , SurveyId = 1 , Language = "es" , Title = "Impacto de la tormenta tropical Emily en la economía de Florida" } ) ;
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { Id = 3 , SurveyId = 1 , Language = "fr" , Title = "Impact de la tempête tropicale Emily sur l'économie de la Floride" } ) ;
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { Id = 4 , SurveyId = 2 , Language = "en" , Title = "Hurricane Andrew Aftermath Survey" } ) ;
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { Id = 5 , SurveyId = 2 , Language = "es" , Title = "Encuesta sobre las secuelas del huracán Andrew" } ) ;
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { Id = 6 , SurveyId = 2 , Language = "fr" , Title = "Enquête sur les conséquences de l'ouragan Andrew" } ) ;
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { Id = 7 , SurveyId = 3 , Language = "en" , Title = "Public Perception of Hurricane Michael's Response" } ) ;
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { Id = 8 , SurveyId = 3 , Language = "es" , Title = "Percepción pública de la respuesta del huracán Michael" } ) ;
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { Id = 9 , SurveyId = 3 , Language = "fr" , Title = "Perception du public de la réponse de l'ouragan Michael" } ) ;
2023-08-18 14:30:34 -05:00
surveyDbContext . SaveChangesAsync ( ) ;
2023-08-15 22:52:30 -05:00
}
}
2023-08-25 07:55:11 -05:00
public async Task < ( bool IsSuccess , IEnumerable < Models . Survey > Surveys , string ErrorMessage ) > GetSurveysAsync ( string Language )
2023-08-15 22:52:30 -05:00
{
2023-08-25 07:55:11 -05:00
IEnumerable < Models . Survey > surveysList = null ;
2023-08-15 22:52:30 -05:00
try
{
logger ? . LogInformation ( "Gell all Surveys from DB" ) ;
2023-08-25 07:55:11 -05:00
var surveys = await surveyDbContext . Surveys . Where ( s = > s . IsEnabled = = true ) . ToListAsync ( ) ;
var surveyTranslations = await surveyDbContext . SurveysTranslation . ToListAsync ( ) ;
2023-08-15 22:52:30 -05:00
if ( surveys ! = null )
{
2023-08-25 07:55:11 -05:00
if ( string . IsNullOrEmpty ( Language ) )
{
surveysList = from s in surveys
select new
Models . Survey
{
Id = s . Id ,
StartDate = s . StartDate ,
EndDate = s . EndDate ,
IsEnabled = s . IsEnabled ,
CreatedDate = s . CreatedDate ,
Titles = from t in surveyTranslations
where t . SurveyId = = s . Id
select new Models . SurveyTranslation
{
Title = t . Title ,
Language = t . Language
}
} ;
}
else
{
surveysList = from s in surveys
select new
Models . Survey
{
Id = s . Id ,
StartDate = s . StartDate ,
EndDate = s . EndDate ,
IsEnabled = s . IsEnabled ,
CreatedDate = s . CreatedDate ,
Titles = from t in surveyTranslations
where t . SurveyId = = s . Id
& & t . Language = = Language
select new Models . SurveyTranslation
{
Title = t . Title ,
Language = t . Language
}
} ;
}
2023-08-15 22:52:30 -05:00
logger ? . LogInformation ( $"{surveys.Count} Items(s) found" ) ;
2023-08-25 07:55:11 -05:00
return ( true , surveysList , null ) ;
2023-08-15 22:52:30 -05:00
}
return ( false , null , "Not found" ) ;
}
catch ( Exception ex )
{
logger ? . LogError ( ex . ToString ( ) ) ;
return ( false , null , ex . Message ) ;
}
}
2023-08-25 07:55:11 -05:00
public async Task < ( bool IsSuccess , Models . Survey Surveys , string ErrorMessage ) > GetSurveysAsync ( int Id , string Language )
2023-08-15 22:52:30 -05:00
{
try
{
logger ? . LogInformation ( "Query Survey" ) ;
2023-08-25 07:55:11 -05:00
var survey = await surveyDbContext . Surveys . SingleOrDefaultAsync ( s = > s . Id = = Id & & s . IsEnabled = = true ) ;
2023-08-15 22:52:30 -05:00
if ( survey ! = null )
{
2023-08-25 07:55:11 -05:00
Models . Survey result = null ;
var surveyTranslations = await surveyDbContext . SurveysTranslation . Where ( s = > s . SurveyId = = survey . Id ) . ToListAsync ( ) ;
if ( string . IsNullOrEmpty ( Language ) )
{
result = new Models . Survey
{
Id = survey . Id ,
StartDate = survey . StartDate ,
EndDate = survey . EndDate ,
IsEnabled = survey . IsEnabled ,
CreatedDate = survey . CreatedDate ,
Titles = from t in surveyTranslations
select new Models . SurveyTranslation
{
Title = t . Title ,
Language = t . Language
}
} ;
}
else
{
result = new Models . Survey
{
Id = survey . Id ,
StartDate = survey . StartDate ,
EndDate = survey . EndDate ,
IsEnabled = survey . IsEnabled ,
CreatedDate = survey . CreatedDate ,
Titles = from t in surveyTranslations
where t . Language = = Language
select new Models . SurveyTranslation
{
Title = t . Title ,
Language = t . Language
}
} ;
}
2023-08-15 22:52:30 -05:00
logger ? . LogInformation ( $"Survey Id: {Id} found" ) ;
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 ( ) ;
2023-08-25 07:55:11 -05:00
int Id = surveys . Count + 1 ;
surveyDbContext . Surveys . Add ( new Db . Survey { Id = Id , IsEnabled = survey . IsEnabled , StartDate = survey . StartDate , EndDate = survey . EndDate , CreatedDate = DateTime . Now } ) ;
var surveyTranslations = await surveyDbContext . SurveysTranslation . ToListAsync ( ) ;
int count = surveyTranslations . Count ;
foreach ( var title in survey . Titles )
{
count + + ;
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { Id = count , SurveyId = Id , Language = title . Language , Title = title . Title } ) ;
}
2023-08-18 14:30:34 -05:00
await surveyDbContext . SaveChangesAsync ( ) ;
2023-08-15 22:52:30 -05:00
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 )
{
2023-08-25 07:55:11 -05:00
var surveysTranslation = await surveyDbContext . SurveysTranslation . Where ( s = > s . SurveyId = = Id ) . ToListAsync ( ) ;
surveyDbContext . SurveysTranslation . RemoveRange ( surveysTranslation ) ;
await surveyDbContext . SaveChangesAsync ( ) ;
2023-08-15 22:52:30 -05:00
_survey . IsEnabled = survey . IsEnabled ;
_survey . StartDate = survey . StartDate ;
_survey . EndDate = survey . EndDate ;
2023-08-18 14:30:34 -05:00
await surveyDbContext . SaveChangesAsync ( ) ;
2023-08-25 07:55:11 -05:00
List < Db . SurveyTranslation > listSurveyTranslation = new List < Db . SurveyTranslation > ( ) ;
Random random = new Random ( ) ;
foreach ( var title in survey . Titles )
{
listSurveyTranslation . Add ( new Db . SurveyTranslation { Id = random . Next ( ) , SurveyId = _survey . Id , Language = title . Language , Title = title . Title } ) ;
}
surveyDbContext . SurveysTranslation . AddRange ( listSurveyTranslation ) ;
await surveyDbContext . SaveChangesAsync ( ) ;
var result = new Models . Survey
{
Id = Id ,
StartDate = survey . StartDate ,
EndDate = survey . EndDate ,
IsEnabled = survey . IsEnabled ,
CreatedDate = survey . CreatedDate ,
Titles = from t in listSurveyTranslation
select new Models . SurveyTranslation
{
Title = t . Title ,
Language = t . Language
}
} ;
return ( true , result , "Successful" ) ;
2023-08-15 22:52:30 -05:00
}
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 ) ;
2023-08-18 14:30:34 -05:00
await surveyDbContext . SaveChangesAsync ( ) ;
2023-08-15 22:52:30 -05:00
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 ) ;
}
}
}
}