|
|
|
@ -21,35 +21,41 @@ namespace DamageAssesment.Api.Surveys.Providers
|
|
|
|
|
//seedData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Method to seed initial data into the database
|
|
|
|
|
public void seedData()
|
|
|
|
|
{
|
|
|
|
|
if (!surveyDbContext.Surveys.Any())
|
|
|
|
|
{
|
|
|
|
|
surveyDbContext.Surveys.Add(new Db.Survey { IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) });
|
|
|
|
|
surveyDbContext.Surveys.Add(new Db.Survey { IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) });
|
|
|
|
|
surveyDbContext.Surveys.Add(new Db.Survey { IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90) });
|
|
|
|
|
var survey1 = new Db.Survey { IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90), CreatedDate = DateTime.Now };
|
|
|
|
|
var survey2 = new Db.Survey { IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90), CreatedDate = DateTime.Now };
|
|
|
|
|
var survey3 = new Db.Survey { IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90), CreatedDate = DateTime.Now };
|
|
|
|
|
|
|
|
|
|
surveyDbContext.Surveys.Add(survey1);
|
|
|
|
|
surveyDbContext.Surveys.Add(survey2);
|
|
|
|
|
surveyDbContext.Surveys.Add(survey3);
|
|
|
|
|
surveyDbContext.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!surveyDbContext.SurveysTranslation.Any())
|
|
|
|
|
{
|
|
|
|
|
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { SurveyId = 1, Language = "en", Title = "Impact of Tropical Storm Emily on Florida's Economy" });
|
|
|
|
|
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { SurveyId = 1, Language = "es", Title = "Impacto de la tormenta tropical Emily en la economía de Florida" });
|
|
|
|
|
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { SurveyId = 1, Language = "fr", Title = "Impact de la tempête tropicale Emily sur l'économie de la Floride" });
|
|
|
|
|
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" });
|
|
|
|
|
|
|
|
|
|
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { SurveyId = 2, Language = "en", Title = "Hurricane Andrew Aftermath Survey" });
|
|
|
|
|
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { SurveyId = 2, Language = "es", Title = "Encuesta sobre las secuelas del huracán Andrew" });
|
|
|
|
|
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { SurveyId = 2, Language = "fr", Title = "Enquête sur les conséquences de l'ouragan Andrew" });
|
|
|
|
|
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" });
|
|
|
|
|
|
|
|
|
|
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" });
|
|
|
|
|
|
|
|
|
|
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { SurveyId = 3, Language = "en", Title = "Public Perception of Hurricane Michael's Response" });
|
|
|
|
|
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { SurveyId = 3, Language = "es", Title = "Percepción pública de la respuesta del huracán Michael" });
|
|
|
|
|
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { SurveyId = 3, Language = "fr", Title = "Perception du public de la réponse de l'ouragan Michael" });
|
|
|
|
|
|
|
|
|
|
surveyDbContext.SaveChangesAsync();
|
|
|
|
|
surveyDbContext.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<Models.SurveyTranslation> GetSurveyTranslations(int id, IEnumerable<Models.SurveyTranslation> SurveyTranslation,string? language)
|
|
|
|
|
// 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)
|
|
|
|
|
{
|
|
|
|
|
if (SurveyTranslation == null)
|
|
|
|
|
{
|
|
|
|
@ -66,6 +72,8 @@ namespace DamageAssesment.Api.Surveys.Providers
|
|
|
|
|
}
|
|
|
|
|
return SurveyTranslation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Method to create a multi-language object from survey translations
|
|
|
|
|
public object CreateMultiLanguageObject(IEnumerable<Models.SurveyTranslation> surveyTranslations)
|
|
|
|
|
{
|
|
|
|
|
object MultiLanguage = new object();
|
|
|
|
@ -77,28 +85,27 @@ namespace DamageAssesment.Api.Surveys.Providers
|
|
|
|
|
MultiLanguage = dict;
|
|
|
|
|
return MultiLanguage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Method to get surveys asynchronously with multi-language support
|
|
|
|
|
public async Task<(bool IsSuccess, IEnumerable<Models.MultiLanSurvey> Surveys, string ErrorMessage)> GetSurveysAsync(string language)
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<Models.MultiLanSurvey> surveysList = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation("Gell all Surveys from DB");
|
|
|
|
|
logger?.LogInformation("Get all Surveys from DB");
|
|
|
|
|
var surveys = await surveyDbContext.Surveys.Where(s => s.IsEnabled == true).ToListAsync();
|
|
|
|
|
//var surveyTranslations = await surveyDbContext.SurveysTranslation.ToListAsync();
|
|
|
|
|
|
|
|
|
|
if (surveys != null)
|
|
|
|
|
{
|
|
|
|
|
surveysList = from s in surveys
|
|
|
|
|
select new
|
|
|
|
|
Models.MultiLanSurvey
|
|
|
|
|
select new Models.MultiLanSurvey
|
|
|
|
|
{
|
|
|
|
|
Id = s.Id,
|
|
|
|
|
StartDate = s.StartDate,
|
|
|
|
|
EndDate = s.EndDate,
|
|
|
|
|
IsEnabled = s.IsEnabled,
|
|
|
|
|
CreatedDate = s.CreatedDate,
|
|
|
|
|
Titles = CreateMultiLanguageObject(GetSurveyTranslations(s.Id,null, language))
|
|
|
|
|
|
|
|
|
|
Titles = CreateMultiLanguageObject(GetSurveyTranslations(s.Id, null, language))
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
logger?.LogInformation($"{surveys.Count} Items(s) found");
|
|
|
|
@ -112,12 +119,15 @@ namespace DamageAssesment.Api.Surveys.Providers
|
|
|
|
|
return (false, null, ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Method to get a specific survey by ID asynchronously with multi-language support
|
|
|
|
|
public async Task<(bool IsSuccess, Models.MultiLanSurvey Surveys, string ErrorMessage)> GetSurveysAsync(int id, string language)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
logger?.LogInformation("Query Survey");
|
|
|
|
|
var survey = await surveyDbContext.Surveys.SingleOrDefaultAsync(s => s.Id == id && s.IsEnabled == true);
|
|
|
|
|
|
|
|
|
|
if (survey != null)
|
|
|
|
|
{
|
|
|
|
|
Models.MultiLanSurvey result = null;
|
|
|
|
@ -129,8 +139,7 @@ namespace DamageAssesment.Api.Surveys.Providers
|
|
|
|
|
EndDate = survey.EndDate,
|
|
|
|
|
IsEnabled = survey.IsEnabled,
|
|
|
|
|
CreatedDate = survey.CreatedDate,
|
|
|
|
|
Titles = CreateMultiLanguageObject(GetSurveyTranslations(survey.Id,null, language))
|
|
|
|
|
|
|
|
|
|
Titles = CreateMultiLanguageObject(GetSurveyTranslations(survey.Id, null, language))
|
|
|
|
|
};
|
|
|
|
|
logger?.LogInformation($"Survey Id: {id} found");
|
|
|
|
|
return (true, result, null);
|
|
|
|
@ -144,6 +153,7 @@ namespace DamageAssesment.Api.Surveys.Providers
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Method to create a new survey asynchronously with multi-language support
|
|
|
|
|
public async Task<(bool IsSuccess, Models.MultiLanSurvey Survey, string ErrorMessage)> PostSurveyAsync(Models.Survey survey)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
@ -155,14 +165,14 @@ namespace DamageAssesment.Api.Surveys.Providers
|
|
|
|
|
|
|
|
|
|
surveyDbContext.Surveys.Add(_survey);
|
|
|
|
|
await surveyDbContext.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var title in survey.Titles)
|
|
|
|
|
{
|
|
|
|
|
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation {SurveyId = _survey.Id, Language = title.Language, Title = title.Title });
|
|
|
|
|
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { SurveyId = _survey.Id, Language = title.Language, Title = title.Title });
|
|
|
|
|
}
|
|
|
|
|
await surveyDbContext.SaveChangesAsync();
|
|
|
|
|
var result = mapper.Map<Db.Survey, Models.MultiLanSurvey>(_survey);
|
|
|
|
|
result.Titles = CreateMultiLanguageObject(GetSurveyTranslations(_survey.Id, survey.Titles, ""));
|
|
|
|
|
result.Titles = CreateMultiLanguageObject(GetSurveyTranslations(_survey.Id, survey.Titles, ""));
|
|
|
|
|
return (true, result, "Successful");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
@ -178,6 +188,7 @@ namespace DamageAssesment.Api.Surveys.Providers
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Method to update an existing survey asynchronously with multi-language support
|
|
|
|
|
public async Task<(bool IsSuccess, Models.MultiLanSurvey Survey, string ErrorMessage)> PutSurveyAsync(int Id, Models.Survey survey)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
@ -227,6 +238,7 @@ namespace DamageAssesment.Api.Surveys.Providers
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Method to delete a survey by ID asynchronously with multi-language support
|
|
|
|
|
public async Task<(bool IsSuccess, Models.MultiLanSurvey Survey, string ErrorMessage)> DeleteSurveyAsync(int Id)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
@ -239,7 +251,7 @@ namespace DamageAssesment.Api.Surveys.Providers
|
|
|
|
|
result.Titles = CreateMultiLanguageObject(GetSurveyTranslations(survey.Id, null, ""));
|
|
|
|
|
surveyDbContext.Surveys.Remove(survey);
|
|
|
|
|
await surveyDbContext.SaveChangesAsync();
|
|
|
|
|
return (true, result, $"Survey Id: {Id} deleted Successfuly");
|
|
|
|
|
return (true, result, $"Survey Id: {Id} deleted Successfully");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|