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 ;
2023-10-06 17:22:37 -05:00
//seedData();
2023-08-15 22:52:30 -05:00
}
2023-10-06 17:22:37 -05:00
// Method to seed initial data into the database
2023-09-04 20:31:41 -05:00
public void seedData ( )
2023-08-15 22:52:30 -05:00
{
if ( ! surveyDbContext . Surveys . Any ( ) )
{
2023-10-23 10:39:56 -05:00
var survey1 = new Db . Survey { IsEnabled = true , StartDate = DateTime . Now . AddDays ( 10 ) , EndDate = DateTime . Now . AddDays ( 90 ) , CreatedDate = DateTime . Now } ;
var survey2 = new Db . Survey { IsEnabled = true , StartDate = DateTime . Now . AddDays ( - 10 ) , EndDate = DateTime . Now . AddDays ( 90 ) , CreatedDate = DateTime . Now . AddDays ( - 10 ) } ;
var survey3 = new Db . Survey { IsEnabled = true , StartDate = DateTime . Now . AddDays ( - 100 ) , EndDate = DateTime . Now . AddDays ( - 10 ) , CreatedDate = DateTime . Now . AddDays ( - 100 ) } ;
2023-08-25 07:55:11 -05:00
2023-10-06 17:22:37 -05:00
surveyDbContext . Surveys . Add ( survey1 ) ;
surveyDbContext . Surveys . Add ( survey2 ) ;
surveyDbContext . Surveys . Add ( survey3 ) ;
surveyDbContext . SaveChanges ( ) ;
2023-08-25 07:55:11 -05:00
2023-10-06 17:22:37 -05:00
if ( ! surveyDbContext . SurveysTranslation . Any ( ) )
{
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { SurveyId = survey1 . Id , Language = "en" , Title = "Impact of Tropical Storm Emily on Florida's Economy" } ) ;
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { SurveyId = survey1 . Id , Language = "es" , Title = "Impacto de la tormenta tropical Emily en la economía de Florida" } ) ;
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { SurveyId = survey1 . Id , Language = "fr" , Title = "Impact de la tempête tropicale Emily sur l'économie de la Floride" } ) ;
2023-08-25 07:55:11 -05:00
2023-10-06 17:22:37 -05:00
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { SurveyId = survey2 . Id , Language = "en" , Title = "Hurricane Andrew Aftermath Survey" } ) ;
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { SurveyId = survey2 . Id , Language = "es" , Title = "Encuesta sobre las secuelas del huracán Andrew" } ) ;
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { SurveyId = survey2 . Id , Language = "fr" , Title = "Enquête sur les conséquences de l'ouragan Andrew" } ) ;
2023-10-11 14:33:10 -05:00
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { SurveyId = survey3 . Id , Language = "en" , Title = "Hurricane Irma" } ) ;
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { SurveyId = survey3 . Id , Language = "es" , Title = "Huracán Irma" } ) ;
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { SurveyId = survey3 . Id , Language = "fr" , Title = "Ouragan Irma" } ) ;
2023-08-25 07:55:11 -05:00
2023-10-06 17:22:37 -05:00
surveyDbContext . SaveChanges ( ) ;
}
2023-08-15 22:52:30 -05:00
}
}
2023-10-06 17:22:37 -05:00
// Method to get survey translations for a given survey ID and language
public IEnumerable < Models . SurveyTranslation > GetSurveyTranslations ( int id , IEnumerable < Models . SurveyTranslation > SurveyTranslation , string? language )
2023-08-15 22:52:30 -05:00
{
2023-09-08 14:40:06 -05:00
if ( SurveyTranslation = = null )
{
if ( string . IsNullOrEmpty ( language ) )
{
SurveyTranslation = mapper . Map < IEnumerable < Db . SurveyTranslation > , IEnumerable < Models . SurveyTranslation > > (
surveyDbContext . SurveysTranslation . Where ( a = > a . SurveyId = = id ) . ToList ( ) ) ;
}
else
{
SurveyTranslation = mapper . Map < IEnumerable < Db . SurveyTranslation > , IEnumerable < Models . SurveyTranslation > > (
surveyDbContext . SurveysTranslation . Where ( a = > a . SurveyId = = id & & a . Language = = language ) . ToList ( ) ) ;
}
}
return SurveyTranslation ;
}
2023-10-06 17:22:37 -05:00
// Method to create a multi-language object from survey translations
2023-09-08 14:40:06 -05:00
public object CreateMultiLanguageObject ( IEnumerable < Models . SurveyTranslation > surveyTranslations )
{
object MultiLanguage = new object ( ) ;
Dictionary < string , string > dict = new Dictionary < string , string > ( ) ;
foreach ( Models . SurveyTranslation item in surveyTranslations )
{
dict . Add ( item . Language , item . Title ) ;
}
MultiLanguage = dict ;
return MultiLanguage ;
}
2023-11-01 13:29:47 -05:00
public string GetStatus ( DateTime ? StartDate , DateTime ? EndDate )
2023-10-23 10:39:56 -05:00
{
2023-11-01 13:29:47 -05:00
try
{
if ( StartDate > DateTime . Now )
return SurveyStatus . PENDING . ToString ( ) ;
else if ( StartDate < = DateTime . Now & & EndDate > DateTime . Now )
return SurveyStatus . ACTIVE . ToString ( ) ;
else
return SurveyStatus . INACTIVE . ToString ( ) ;
}
catch
{
2023-10-23 10:39:56 -05:00
return SurveyStatus . INACTIVE . ToString ( ) ;
2023-11-01 13:29:47 -05:00
}
2023-10-23 10:39:56 -05:00
}
2023-10-06 17:22:37 -05:00
// Method to get surveys asynchronously with multi-language support
2023-09-08 14:40:06 -05:00
public async Task < ( bool IsSuccess , IEnumerable < Models . MultiLanSurvey > Surveys , string ErrorMessage ) > GetSurveysAsync ( string language )
{
IEnumerable < Models . MultiLanSurvey > surveysList = null ;
2023-08-15 22:52:30 -05:00
try
{
2023-10-06 17:22:37 -05:00
logger ? . LogInformation ( "Get all Surveys from DB" ) ;
2023-10-16 13:13:20 -05:00
//checking is enabled in survey response
var surveys = await surveyDbContext . Surveys . ToListAsync ( ) ; //Where(s => s.IsEnabled == true)
2023-08-15 22:52:30 -05:00
if ( surveys ! = null )
{
2023-09-08 14:40:06 -05:00
surveysList = from s in surveys
2023-10-06 17:22:37 -05:00
select new Models . MultiLanSurvey
2023-09-08 14:40:06 -05:00
{
Id = s . Id ,
StartDate = s . StartDate ,
EndDate = s . EndDate ,
IsEnabled = s . IsEnabled ,
CreatedDate = s . CreatedDate ,
2023-10-23 10:39:56 -05:00
Status = GetStatus ( s . StartDate , s . EndDate ) ,
2023-10-06 17:22:37 -05:00
Titles = CreateMultiLanguageObject ( GetSurveyTranslations ( s . Id , null , language ) )
2023-09-08 14:40:06 -05:00
} ;
2023-08-25 07:55:11 -05:00
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-10-06 17:22:37 -05:00
// Method to get a specific survey by ID asynchronously with multi-language support
2023-09-08 14:40:06 -05:00
public async Task < ( bool IsSuccess , Models . MultiLanSurvey Surveys , string ErrorMessage ) > GetSurveysAsync ( int id , string language )
2023-08-15 22:52:30 -05:00
{
try
{
logger ? . LogInformation ( "Query Survey" ) ;
2023-11-01 13:29:47 -05:00
// removed is enabled becuase we are using it in responses to get response
var survey = await surveyDbContext . Surveys . SingleOrDefaultAsync ( s = > s . Id = = id ) ;
2023-10-06 17:22:37 -05:00
2023-08-15 22:52:30 -05:00
if ( survey ! = null )
{
2023-09-08 14:40:06 -05:00
Models . MultiLanSurvey result = null ;
2023-08-25 07:55:11 -05:00
var surveyTranslations = await surveyDbContext . SurveysTranslation . Where ( s = > s . SurveyId = = survey . Id ) . ToListAsync ( ) ;
2023-09-08 14:40:06 -05:00
result = new Models . MultiLanSurvey
2023-08-25 07:55:11 -05:00
{
2023-09-08 14:40:06 -05:00
Id = survey . Id ,
StartDate = survey . StartDate ,
EndDate = survey . EndDate ,
IsEnabled = survey . IsEnabled ,
CreatedDate = survey . CreatedDate ,
2023-10-23 10:39:56 -05:00
Status = GetStatus ( survey . StartDate , survey . EndDate ) ,
2023-10-06 17:22:37 -05:00
Titles = CreateMultiLanguageObject ( GetSurveyTranslations ( survey . Id , null , language ) )
2023-09-08 14:40:06 -05:00
} ;
logger ? . LogInformation ( $"Survey Id: {id} found" ) ;
2023-08-15 22:52:30 -05:00
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
// Method to create a new survey asynchronously with multi-language support
2023-09-08 14:40:06 -05:00
public async Task < ( bool IsSuccess , Models . MultiLanSurvey Survey , string ErrorMessage ) > PostSurveyAsync ( Models . Survey survey )
2023-08-15 22:52:30 -05:00
{
try
{
if ( survey ! = null )
{
2023-08-25 17:44:04 -05:00
survey . CreatedDate = DateTime . Now ;
Db . Survey _survey = mapper . Map < Models . Survey , Db . Survey > ( survey ) ;
2023-08-25 07:55:11 -05:00
2023-08-25 16:51:07 -05:00
surveyDbContext . Surveys . Add ( _survey ) ;
await surveyDbContext . SaveChangesAsync ( ) ;
2023-10-06 17:22:37 -05:00
2023-08-25 07:55:11 -05:00
foreach ( var title in survey . Titles )
{
2023-10-06 17:22:37 -05:00
surveyDbContext . SurveysTranslation . Add ( new Db . SurveyTranslation { SurveyId = _survey . Id , Language = title . Language , Title = title . Title } ) ;
2023-08-25 07:55:11 -05:00
}
2023-08-18 14:30:34 -05:00
await surveyDbContext . SaveChangesAsync ( ) ;
2023-09-08 14:40:06 -05:00
var result = mapper . Map < Db . Survey , Models . MultiLanSurvey > ( _survey ) ;
2023-10-23 10:39:56 -05:00
result . Status = GetStatus ( _survey . StartDate , _survey . EndDate ) ;
2023-10-06 17:22:37 -05:00
result . Titles = CreateMultiLanguageObject ( GetSurveyTranslations ( _survey . Id , survey . Titles , "" ) ) ;
2023-09-08 14:40:06 -05:00
return ( true , result , "Successful" ) ;
2023-08-15 22:52:30 -05:00
}
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 ) ;
}
}
2023-10-06 17:22:37 -05:00
// Method to update an existing survey asynchronously with multi-language support
2023-09-08 14:40:06 -05:00
public async Task < ( bool IsSuccess , Models . MultiLanSurvey Survey , string ErrorMessage ) > PutSurveyAsync ( int Id , Models . Survey survey )
2023-08-15 22:52:30 -05:00
{
try
{
if ( survey ! = null )
{
2023-09-13 00:28:24 -05:00
var _survey = await surveyDbContext . Surveys . AsNoTracking ( ) . Where ( s = > s . Id = = Id ) . SingleOrDefaultAsync ( ) ;
2023-08-15 22:52:30 -05:00
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-09-13 00:28:24 -05:00
_survey = mapper . Map < Models . Survey , Db . Survey > ( survey ) ;
surveyDbContext . Surveys . Update ( _survey ) ;
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 > ( ) ;
foreach ( var title in survey . Titles )
{
2023-10-31 15:19:24 -05:00
listSurveyTranslation . Add ( new Db . SurveyTranslation { SurveyId = _survey . Id , Language = title . Language , Title = title . Title } ) ;
2023-08-25 07:55:11 -05:00
}
surveyDbContext . SurveysTranslation . AddRange ( listSurveyTranslation ) ;
await surveyDbContext . SaveChangesAsync ( ) ;
2023-09-08 14:40:06 -05:00
var result = mapper . Map < Db . Survey , Models . MultiLanSurvey > ( _survey ) ;
2023-10-23 10:39:56 -05:00
result . Status = GetStatus ( _survey . StartDate , _survey . EndDate ) ;
2023-09-08 14:40:06 -05:00
result . Titles = CreateMultiLanguageObject ( GetSurveyTranslations ( _survey . Id , survey . Titles , "" ) ) ;
2023-08-25 07:55:11 -05:00
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 ) ;
}
}
2023-10-06 17:22:37 -05:00
// Method to delete a survey by ID asynchronously with multi-language support
2023-09-08 14:40:06 -05:00
public async Task < ( bool IsSuccess , Models . MultiLanSurvey Survey , string ErrorMessage ) > DeleteSurveyAsync ( int Id )
2023-08-15 22:52:30 -05:00
{
try
{
var survey = await surveyDbContext . Surveys . Where ( x = > x . Id = = Id ) . SingleOrDefaultAsync ( ) ;
if ( survey ! = null )
{
2023-09-08 14:40:06 -05:00
var result = mapper . Map < Db . Survey , Models . MultiLanSurvey > ( survey ) ;
2023-10-23 10:39:56 -05:00
result . Status = GetStatus ( survey . StartDate , survey . EndDate ) ;
2023-09-08 14:40:06 -05:00
result . Titles = CreateMultiLanguageObject ( GetSurveyTranslations ( survey . Id , null , "" ) ) ;
2023-08-15 22:52:30 -05:00
surveyDbContext . Surveys . Remove ( survey ) ;
2023-08-18 14:30:34 -05:00
await surveyDbContext . SaveChangesAsync ( ) ;
2023-10-06 17:22:37 -05:00
return ( true , result , $"Survey Id: {Id} deleted Successfully" ) ;
2023-08-15 22:52:30 -05:00
}
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 ) ;
}
}
}
}