bug fix with seed data changes

This commit is contained in:
Santhosh S 2023-10-06 18:22:37 -04:00
parent 1510c3ab12
commit 9b91afd329
13 changed files with 124 additions and 100 deletions

View File

@ -8,19 +8,20 @@ namespace DamageAssesment.Api.Answers.Providers
{
public class AnswersProvider : IAnswersProvider
{
private AnswerDbContext answerDbContext;
private ILogger<AnswersProvider> logger;
private IMapper mapper;
// Constructor with dependency injection and data seeding
public AnswersProvider(AnswerDbContext answerDbContext, ILogger<AnswersProvider> logger, IMapper mapper)
{
this.answerDbContext = answerDbContext;
this.logger = logger;
this.mapper = mapper;
SeedData();
SeedData(); // Seed initial data if the table is empty
}
// Get all answers
public async Task<(bool IsSuccess, IEnumerable<Models.Answer> Answers, string ErrorMessage)> GetAnswersAsync()
{
try
@ -40,9 +41,9 @@ namespace DamageAssesment.Api.Answers.Providers
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
// Get an answer by its ID
public async Task<(bool IsSuccess, Models.Answer Answer, string ErrorMessage)> GetAnswerByIdAsync(int Id)
{
try
@ -63,6 +64,8 @@ namespace DamageAssesment.Api.Answers.Providers
return (false, null, ex.Message);
}
}
// Get answers by survey response ID
public async Task<(bool IsSuccess, IEnumerable<Models.Answer> Answers, string ErrorMessage)> GetAnswersAsync(int surveyResponseId)
{
try
@ -74,17 +77,17 @@ namespace DamageAssesment.Api.Answers.Providers
{
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);
}
}
// Get answers by question ID
public async Task<(bool IsSuccess, IEnumerable<Models.Answer> Answers, string ErrorMessage)> GetAnswersByQuestionAsync(int questionId)
{
try
@ -96,17 +99,17 @@ namespace DamageAssesment.Api.Answers.Providers
{
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);
}
}
// Create a new answer
public async Task<(bool IsSuccess, Models.Answer Answer, string ErrorMessage)> PostAnswerAsync(Models.Answer Answer)
{
try
@ -120,7 +123,7 @@ namespace DamageAssesment.Api.Answers.Providers
var result = mapper.Map<Db.Answer, Models.Answer>(answer);
return (true, result, null);
}
return (false, null, "Answer is already exits");
return (false, null, "Answer is already exists");
}
catch (Exception ex)
{
@ -128,6 +131,8 @@ namespace DamageAssesment.Api.Answers.Providers
return (false, null, ex.Message);
}
}
// Update an existing answer
public async Task<(bool IsSuccess, Models.Answer Answer, string ErrorMessage)> UpdateAnswerAsync(Models.Answer Answer)
{
try
@ -156,14 +161,14 @@ namespace DamageAssesment.Api.Answers.Providers
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false,null, ex.Message);
return (false, null, ex.Message);
}
}
// Delete an answer by its ID
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();
@ -173,26 +178,29 @@ namespace DamageAssesment.Api.Answers.Providers
}
answerDbContext.Answers.Remove(answer);
answerDbContext.SaveChanges();
return (true, mapper.Map<Db.Answer, Models.Answer>(answer), $"AnswerId {Id} deleted Successfuly");
return (true, mapper.Map<Db.Answer, Models.Answer>(answer), $"AnswerId {Id} deleted successfully");
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false,null, ex.Message);
return (false, null, ex.Message);
}
}
// Check if an answer with a specific ID exists
private bool AnswerExists(int id)
{
return answerDbContext.Answers.AsNoTracking().Count(e => e.Id == id) > 0;
}
// Seed initial data if the table is empty
public void SeedData()
{
if (!answerDbContext.Answers.Any())
{
answerDbContext.Answers.Add(new Db.Answer() { Id = 1, AnswerText = "Yes", Comment = "Comment test 4", QuestionId = 1, SurveyResponseId = 1 });
answerDbContext.Answers.Add(new Db.Answer() { Id = 2, AnswerText = "No", Comment = "Comment test 5", QuestionId = 2, SurveyResponseId = 1 });
// Uncomment the lines below to add more initial data if needed
//answerDbContext.Answers.Add(new Db.Answer() { Id = 3, AnswerText = "No", Comment = "No Comment", QuestionId = 3, SurveyResponseId = 1 });
//answerDbContext.Answers.Add(new Db.Answer() { Id = 4, AnswerText = "Yes", Comment = "No Comment", QuestionId = 1, SurveyResponseId = 2 });
//answerDbContext.Answers.Add(new Db.Answer() { Id = 5, AnswerText = "No", Comment = "No Comment", QuestionId = 2, SurveyResponseId = 2 });

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>

View File

@ -19,7 +19,7 @@ namespace DamageAssesment.Api.Employees.Providers
this.EmployeeDbContext = EmployeeDbContext;
this.logger = logger;
this.mapper = mapper;
SeedData();
// SeedData();
}
public async Task<(bool IsSuccess, IEnumerable<Models.Employee> Employees, string ErrorMessage)> GetEmployeesAsync()
@ -156,8 +156,8 @@ namespace DamageAssesment.Api.Employees.Providers
{
if (!EmployeeDbContext.Employees.Any())
{
EmployeeDbContext.Employees.Add(new Db.Employee() { EmployeeCode = "Emp1", Name = "David", Email = "David@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-18), IsActive = true, PreferredLanguage = "en" });
EmployeeDbContext.Employees.Add(new Db.Employee() { EmployeeCode = "Emp2", Name = "Smith", Email = "Smith@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-22), IsActive = true, PreferredLanguage = "fr" });
EmployeeDbContext.Employees.Add(new Db.Employee() { EmployeeCode = "10101", Name = "David", Email = "david@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-18), IsActive = true, PreferredLanguage = "en" });
EmployeeDbContext.Employees.Add(new Db.Employee() { EmployeeCode = "20202", Name = "Smith", Email = "smith@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-22), IsActive = true, PreferredLanguage = "fr" });
//EmployeeDbContext.Employees.Add(new Db.Employee() { Id = 3, EmployeeCode = "Emp3", Name = "ABC3", Email = "abc3@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-30), IsActive = true, PreferredLanguage = "fr" });
//EmployeeDbContext.Employees.Add(new Db.Employee() { Id = 4, EmployeeCode = "Emp4", Name = "ABC4", Email = "abc4@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-20), IsActive = true, PreferredLanguage = "en" });
//EmployeeDbContext.Employees.Add(new Db.Employee() { Id = 5, EmployeeCode = "Emp5", Name = "ABC5", Email = "abc5@gmail.com", OfficePhoneNumber = "12345678", BirthDate = DateTime.Now.AddYears(-23), IsActive = true, PreferredLanguage = "es" });

View File

@ -17,7 +17,7 @@ namespace DamageAssesment.Api.Locations.Providers
this.locationDbContext = locationDbContext;
this.logger = logger;
this.mapper = mapper;
SeedData();
// SeedData();
}
public async Task<(bool IsSuccess, IEnumerable<Models.Location> Locations, string ErrorMessage)> GetLocationsAsync()
@ -139,8 +139,9 @@ namespace DamageAssesment.Api.Locations.Providers
{
if (!locationDbContext.Locations.Any())
{
locationDbContext.Locations.Add(new Db.Location() { LocationCode = "Loc1", RegionId = 1, Name = "BOB GRAHAM EDUCATION CENTER 1", MaintenanceCenter = "1", SchoolType = "US" });
locationDbContext.Locations.Add(new Db.Location() { LocationCode = "Loc2", RegionId = 2, Name = "BOB GRAHAM EDUCATION CENTER 2", MaintenanceCenter = "1", SchoolType = "US" });
locationDbContext.Locations.Add(new Db.Location() { LocationCode = "0091", RegionId = 5, Name = "BOB GRAHAM EDUCATION CENTER", MaintenanceCenter = "1", SchoolType = "K8" });
locationDbContext.Locations.Add(new Db.Location() { LocationCode = "0092", RegionId = 1, Name = "NORMAN S. EDELCUP/SUNNY ISLES BEACH K-8", MaintenanceCenter = "1", SchoolType = "K8" });
locationDbContext.Locations.Add(new Db.Location() { LocationCode = "7511", RegionId = 4, Name = "MIAMI SPRINGS SHS", MaintenanceCenter = "2", SchoolType = "S" });
//locationDbContext.Locations.Add(new Db.Location() { Id = 3, LocationCode = "Loc3", RegionId = 3, Name = "BOB GRAHAM EDUCATION CENTER 3", MaintenanceCenter = "1", SchoolType = "US" });
//locationDbContext.Locations.Add(new Db.Location() { Id = 4, LocationCode = "Loc4", RegionId = 1, Name = "BOB GRAHAM EDUCATION CENTER 4", MaintenanceCenter = "1", SchoolType = "US" });
//locationDbContext.Locations.Add(new Db.Location() { Id = 5, LocationCode = "Loc5", RegionId = 2, Name = "BOB GRAHAM EDUCATION CENTER 5", MaintenanceCenter = "1", SchoolType = "US" });

View File

@ -16,7 +16,7 @@ namespace DamageAssesment.Api.Locations.Providers
this.locationDbContext = regionDbContext;
this.logger = logger;
this.mapper = mapper;
SeedData();
// SeedData();
}
public async Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> GetRegionByIdAsync(int Id)
@ -166,8 +166,10 @@ namespace DamageAssesment.Api.Locations.Providers
if (!locationDbContext.Regions.Any())
{
locationDbContext.Regions.Add(new Db.Region() { Name = "North", Abbreviation = "N" });
locationDbContext.Regions.Add(new Db.Region() { Name = "South", Abbreviation = "S" });
locationDbContext.Regions.Add(new Db.Region() { Name = "Central", Abbreviation = "C" });
locationDbContext.Regions.Add(new Db.Region() { Name = "South", Abbreviation = "S" });
locationDbContext.Regions.Add(new Db.Region() { Name = "Charter Schools", Abbreviation = "CS" });
locationDbContext.Regions.Add(new Db.Region() { Name = "Alternate and Special Centers", Abbreviation = "AC" });
locationDbContext.SaveChanges();
}
}

View File

@ -19,37 +19,11 @@ namespace DamageAssesment.Api.Questions.Providers
this.questionDbContext = questionDbContext;
this.logger = logger;
this.mapper = mapper;
SeedData();
// SeedData();
}
public void SeedData()
{
if (!questionDbContext.QuestionsTranslations.Any())
{
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 1, QuestionText = "Can You Open ?", Language = "en" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 1, QuestionText = "Peux-tu ouvrir ?", Language = "fr" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 1, QuestionText = "Puedes abrir ?", Language = "es" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 2, QuestionText = "Are the grounds flodded ?", Language = "en" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 2, QuestionText = "Les terrains sont-ils inondés ?", Language = "fr" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 2, QuestionText = "¿Están inundados los terrenos?", Language = "es" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 3, QuestionText = "Is the access blocked by flooding ?", Language = "en" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 3, QuestionText = "L'accès est-il bloqué par les inondations ?", Language = "fr" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 3, QuestionText = "¿El acceso está bloqueado por inundaciones?", Language = "es" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 4, QuestionText = "Are the grounds flodded ?", Language = "en" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 4, QuestionText = "Les terrains sont-ils inondés ?", Language = "fr" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 4, QuestionText = "¿Están inundados los terrenos?", Language = "es" });
questionDbContext.SaveChanges();
}
if (!questionDbContext.Questions.Any())
{
questionDbContext.Questions.Add(new Db.Question() { QuestionTypeId = 1, SurveyId = 1, QuestionNumber = 1, IsRequired = true, Comment = false, Key = true, CategoryId=1 });
questionDbContext.Questions.Add(new Db.Question() { QuestionTypeId = 1, SurveyId = 1, QuestionNumber = 2, IsRequired = false, Comment = true, Key = false, CategoryId = 1 });
questionDbContext.Questions.Add(new Db.Question() { QuestionTypeId = 1, SurveyId = 2, QuestionNumber = 1, IsRequired = true, Comment = false, Key = true, CategoryId = 1 });
questionDbContext.Questions.Add(new Db.Question() { QuestionTypeId = 1, SurveyId = 2, QuestionNumber = 2, IsRequired = false, Comment = true, Key = false, CategoryId = 1 });
//questionDbContext.Questions.Add(new Db.Question() { QuestionTypeId = 1, SurveyId = 2, QuestionNumber = 3, IsRequired = true, Comment = false, Key = true, CategoryId = 2 });
questionDbContext.SaveChanges();
}
if (!questionDbContext.QuestionTypes.Any())
{
questionDbContext.QuestionTypes.Add(new Db.QuestionType() { TypeText = "RadioButton" });
@ -60,7 +34,7 @@ namespace DamageAssesment.Api.Questions.Providers
if (!questionDbContext.QuestionCategories.Any())
{
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { IconName = "Flooding", IconLibrary= "https://example.com/images/img1.png" });
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { IconName = "Flooding", IconLibrary = "https://example.com/images/img1.png" });
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { IconName = "Electrical", IconLibrary = "https://example.com/images/img2.png" });
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { IconName = "Structural", IconLibrary = "https://example.com/images/img3.png" });
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { IconName = "Utility", IconLibrary = "https://example.com/images/img4.png" });
@ -70,7 +44,6 @@ namespace DamageAssesment.Api.Questions.Providers
if (!questionDbContext.CategoryTranslations.Any())
{
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 1, Title = "Flooding", Language = "en" });
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 2, Title = "Electrical", Language = "en" });
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 3, Title = "Structural", Language = "en" });
@ -88,6 +61,37 @@ namespace DamageAssesment.Api.Questions.Providers
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 5, Title = "Escombros", Language = "es" });
questionDbContext.SaveChanges();
}
if (!questionDbContext.Questions.Any())
{
var question1 = new Db.Question() { QuestionTypeId = 1, SurveyId = 1, QuestionNumber = 1, IsRequired = true, Comment = false, Key = true, CategoryId = 1 };
var question2 = new Db.Question() { QuestionTypeId = 1, SurveyId = 1, QuestionNumber = 2, IsRequired = false, Comment = true, Key = false, CategoryId = 1 };
var question3 = new Db.Question() { QuestionTypeId = 1, SurveyId = 2, QuestionNumber = 1, IsRequired = true, Comment = false, Key = true, CategoryId = 1 };
var question4 = new Db.Question() { QuestionTypeId = 1, SurveyId = 2, QuestionNumber = 2, IsRequired = false, Comment = true, Key = false, CategoryId = 1 };
questionDbContext.Questions.Add(question1);
questionDbContext.Questions.Add(question2);
questionDbContext.Questions.Add(question3);
questionDbContext.Questions.Add(question4);
questionDbContext.SaveChanges();
}
if (!questionDbContext.QuestionsTranslations.Any())
{
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 1, QuestionText = "Can You Open ?", Language = "en" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 1, QuestionText = "Peux-tu ouvrir ?", Language = "fr" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 1, QuestionText = "Puedes abrir ?", Language = "es" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 2, QuestionText = "Are the grounds flooded ?", Language = "en" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 2, QuestionText = "Les terrains sont-ils inondés ?", Language = "fr" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 2, QuestionText = "¿Están inundados los terrenos?", Language = "es" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 3, QuestionText = "Is the access blocked by flooding ?", Language = "en" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 3, QuestionText = "L'accès est-il bloqué par les inondations ?", Language = "fr" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 3, QuestionText = "¿El acceso está bloqueado por inundaciones?", Language = "es" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 4, QuestionText = "Are the grounds flooded ?", Language = "en" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 4, QuestionText = "Les terrains sont-ils inondés ?", Language = "fr" });
questionDbContext.QuestionsTranslations.Add(new Db.QuestionsTranslation() { QuestionId = 4, QuestionText = "¿Están inundados los terrenos?", Language = "es" });
questionDbContext.SaveChanges();
}
}
public List<Models.CategoryTranslation> GetCategoryTranslations(int id, string? language)

View File

@ -31,23 +31,24 @@ namespace DamageAssesment.Api.Responses.Providers
this.questionServiceProvider = questionServiceProvider;
this.surveyServiceProvider = surveyServiceProvider;
this.mapper = mapper;
seedData();
SeedData();
}
private void seedData()
public void SeedData()
{
// Check if SurveyResponses exist, if not, seed data
if (!surveyResponseDbContext.SurveyResponses.Any())
{
// Create and save SurveyResponse records with references to existing Employee and Location records
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = 1, LocationId = 1, ClientDevice = "Mobile", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "true", CreatedDate = DateTime.Now });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 2, EmployeeId = 2, LocationId = 2, ClientDevice = "Desktop", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "true", CreatedDate = DateTime.Now });
//surveyResponseDbContext.Responses.Add(new Db.SurveyResponse { Id = 3, SurveyId = 3, EmployeeId = 4, LocationId = 1, ClientDevice = "Mobile", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "true", CreatedDate = DateTime.Now });
//surveyResponseDbContext.Responses.Add(new Db.SurveyResponse { Id = 4, SurveyId = 4, EmployeeId = 1, LocationId = 2, ClientDevice = "Desktop", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "false", CreatedDate = DateTime.Now });
//surveyResponseDbContext.Responses.Add(new Db.SurveyResponse { Id = 6, SurveyId = 1, EmployeeId = 4, LocationId = 2, ClientDevice = "Desktop", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "true", CreatedDate = DateTime.Now });
//surveyResponseDbContext.Responses.Add(new Db.SurveyResponse { Id = 7, SurveyId = 1, EmployeeId = 4, LocationId = 3, ClientDevice = "Desktop", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "false", CreatedDate = DateTime.Now });
surveyResponseDbContext.SurveyResponses.Add(new Db.SurveyResponse { SurveyId = 1, EmployeeId = 2, LocationId = 2, ClientDevice = "Mobile", Latitude = 98.8767, Longitute = -129.9897, KeyAnswerResult = "true", CreatedDate = DateTime.Now });
surveyResponseDbContext.SaveChanges();
}
}
public async Task<(bool IsSuccess, dynamic Answers, string ErrorMessage)> GetAnswersByRegionAsync(int surveyId, int employeeid)
{
try

View File

@ -16,13 +16,14 @@
// "AttachmentUrlBase": "http://localhost:5243",
// "SurveyUrlBase": "http://localhost:5009"
//},
//Endpoints for docker-container
"EndPointSettings": {
"AnswerUrlBase": "http://damageassesment.api.answers:80",
"LocationUrlBase": "http://damageassesment.api.locations:80",
"QuestionUrlBase": "http://damageassesment.api.questions:80",
"EmployeeUrlBase": "http://damageassesment.api.employees:80",
"AttachmentUrlBase": "http://damageassesment.api.attachments:80",
"SurveyUrlBase": "http://damageassesment.api.survey:80"
"SurveyUrlBase": "http://damageassesment.api.surveys:80"
},
"RessourceSettings": {

View File

@ -18,39 +18,44 @@ namespace DamageAssesment.Api.Surveys.Providers
this.surveyDbContext = surveysDbContext;
this.logger = logger;
this.mapper = mapper;
seedData();
//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), CreatedDate = DateTime.Now });
surveyDbContext.Surveys.Add(new Db.Survey { 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();
}
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 = false, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90), CreatedDate = DateTime.Now };
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" });
surveyDbContext.Surveys.Add(survey1);
surveyDbContext.Surveys.Add(survey2);
surveyDbContext.Surveys.Add(survey3);
surveyDbContext.SaveChanges();
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" });
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 { 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" });
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 Idalia Aftermath Survey" });
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { SurveyId = survey3.Id, Language = "es", Title = "Encuesta sobre las secuelas del huracán Idalia" });
surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation { SurveyId = survey3.Id, Language = "fr", Title = "Enquête sur les conséquences de l'ouragan Idalia" });
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)
{
@ -67,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();
@ -78,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");
@ -113,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;
@ -130,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);
@ -145,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
@ -156,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
@ -179,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
@ -228,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
@ -240,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
{

View File

@ -34,14 +34,7 @@ services:
context: .
dockerfile: DamageAssesment.Api.Questions/Dockerfile
# damageassesment.api.surveyresponses:
# image: ${DOCKER_REGISTRY-}damageassesmentapisurveyresponses
# build:
# context: .
# dockerfile: DamageAssesment.Api.SurveyResponses/Dockerfile
damageassesment.api.surveys:
image: ${DOCKER_REGISTRY-}damageassesmentapisurveys
build: