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 public class AnswersProvider : IAnswersProvider
{ {
private AnswerDbContext answerDbContext; private AnswerDbContext answerDbContext;
private ILogger<AnswersProvider> logger; private ILogger<AnswersProvider> logger;
private IMapper mapper; private IMapper mapper;
// Constructor with dependency injection and data seeding
public AnswersProvider(AnswerDbContext answerDbContext, ILogger<AnswersProvider> logger, IMapper mapper) public AnswersProvider(AnswerDbContext answerDbContext, ILogger<AnswersProvider> logger, IMapper mapper)
{ {
this.answerDbContext = answerDbContext; this.answerDbContext = answerDbContext;
this.logger = logger; this.logger = logger;
this.mapper = mapper; 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() public async Task<(bool IsSuccess, IEnumerable<Models.Answer> Answers, string ErrorMessage)> GetAnswersAsync()
{ {
try try
@ -40,9 +41,9 @@ namespace DamageAssesment.Api.Answers.Providers
logger?.LogError(ex.ToString()); logger?.LogError(ex.ToString());
return (false, null, ex.Message); return (false, null, ex.Message);
} }
} }
// Get an answer by its ID
public async Task<(bool IsSuccess, Models.Answer Answer, string ErrorMessage)> GetAnswerByIdAsync(int Id) public async Task<(bool IsSuccess, Models.Answer Answer, string ErrorMessage)> GetAnswerByIdAsync(int Id)
{ {
try try
@ -63,6 +64,8 @@ namespace DamageAssesment.Api.Answers.Providers
return (false, null, ex.Message); 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) public async Task<(bool IsSuccess, IEnumerable<Models.Answer> Answers, string ErrorMessage)> GetAnswersAsync(int surveyResponseId)
{ {
try try
@ -74,17 +77,17 @@ namespace DamageAssesment.Api.Answers.Providers
{ {
var result = mapper.Map<IEnumerable<Db.Answer>, IEnumerable<Models.Answer>>(respAnswers); var result = mapper.Map<IEnumerable<Db.Answer>, IEnumerable<Models.Answer>>(respAnswers);
return (true, result, null); return (true, result, null);
} }
return (false, null, "Not Found"); return (false, null, "Not Found");
} }
catch (Exception ex) catch (Exception ex)
{ {
logger?.LogError(ex.ToString()); logger?.LogError(ex.ToString());
return (false, null, ex.Message); return (false, null, ex.Message);
} }
} }
// Get answers by question ID
public async Task<(bool IsSuccess, IEnumerable<Models.Answer> Answers, string ErrorMessage)> GetAnswersByQuestionAsync(int questionId) public async Task<(bool IsSuccess, IEnumerable<Models.Answer> Answers, string ErrorMessage)> GetAnswersByQuestionAsync(int questionId)
{ {
try try
@ -96,17 +99,17 @@ namespace DamageAssesment.Api.Answers.Providers
{ {
var result = mapper.Map<IEnumerable<Db.Answer>, IEnumerable<Models.Answer>>(respAnswers); var result = mapper.Map<IEnumerable<Db.Answer>, IEnumerable<Models.Answer>>(respAnswers);
return (true, result, null); return (true, result, null);
} }
return (false, null, "Not Found"); return (false, null, "Not Found");
} }
catch (Exception ex) catch (Exception ex)
{ {
logger?.LogError(ex.ToString()); logger?.LogError(ex.ToString());
return (false, null, ex.Message); return (false, null, ex.Message);
} }
} }
// Create a new answer
public async Task<(bool IsSuccess, Models.Answer Answer, string ErrorMessage)> PostAnswerAsync(Models.Answer Answer) public async Task<(bool IsSuccess, Models.Answer Answer, string ErrorMessage)> PostAnswerAsync(Models.Answer Answer)
{ {
try try
@ -120,7 +123,7 @@ namespace DamageAssesment.Api.Answers.Providers
var result = mapper.Map<Db.Answer, Models.Answer>(answer); var result = mapper.Map<Db.Answer, Models.Answer>(answer);
return (true, result, null); return (true, result, null);
} }
return (false, null, "Answer is already exits"); return (false, null, "Answer is already exists");
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -128,6 +131,8 @@ namespace DamageAssesment.Api.Answers.Providers
return (false, null, ex.Message); return (false, null, ex.Message);
} }
} }
// Update an existing answer
public async Task<(bool IsSuccess, Models.Answer Answer, string ErrorMessage)> UpdateAnswerAsync(Models.Answer Answer) public async Task<(bool IsSuccess, Models.Answer Answer, string ErrorMessage)> UpdateAnswerAsync(Models.Answer Answer)
{ {
try try
@ -156,14 +161,14 @@ namespace DamageAssesment.Api.Answers.Providers
} }
catch (Exception ex) catch (Exception ex)
{ {
logger?.LogError(ex.ToString()); 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) public async Task<(bool IsSuccess, Models.Answer Answer, string ErrorMessage)> DeleteAnswerAsync(int Id)
{ {
try try
{ {
Db.Answer answer = answerDbContext.Answers.AsNoTracking().Where(a => a.Id == Id).FirstOrDefault(); 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.Answers.Remove(answer);
answerDbContext.SaveChanges(); 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) catch (Exception ex)
{ {
logger?.LogError(ex.ToString()); 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) private bool AnswerExists(int id)
{ {
return answerDbContext.Answers.AsNoTracking().Count(e => e.Id == id) > 0; return answerDbContext.Answers.AsNoTracking().Count(e => e.Id == id) > 0;
} }
// Seed initial data if the table is empty
public void SeedData() public void SeedData()
{ {
if (!answerDbContext.Answers.Any()) 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 = 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 }); 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 = 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 = 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 }); //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> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>

View File

@ -19,7 +19,7 @@ namespace DamageAssesment.Api.Employees.Providers
this.EmployeeDbContext = EmployeeDbContext; this.EmployeeDbContext = EmployeeDbContext;
this.logger = logger; this.logger = logger;
this.mapper = mapper; this.mapper = mapper;
SeedData(); // SeedData();
} }
public async Task<(bool IsSuccess, IEnumerable<Models.Employee> Employees, string ErrorMessage)> GetEmployeesAsync() 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()) 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 = "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 = "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 = "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 = 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 = 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" }); //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.locationDbContext = locationDbContext;
this.logger = logger; this.logger = logger;
this.mapper = mapper; this.mapper = mapper;
SeedData(); // SeedData();
} }
public async Task<(bool IsSuccess, IEnumerable<Models.Location> Locations, string ErrorMessage)> GetLocationsAsync() 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()) 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 = "0091", RegionId = 5, Name = "BOB GRAHAM EDUCATION CENTER", MaintenanceCenter = "1", SchoolType = "K8" });
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 = "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 = 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 = 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" }); //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.locationDbContext = regionDbContext;
this.logger = logger; this.logger = logger;
this.mapper = mapper; this.mapper = mapper;
SeedData(); // SeedData();
} }
public async Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> GetRegionByIdAsync(int Id) 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()) if (!locationDbContext.Regions.Any())
{ {
locationDbContext.Regions.Add(new Db.Region() { Name = "North", Abbreviation = "N" }); 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 = "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(); locationDbContext.SaveChanges();
} }
} }

View File

@ -19,37 +19,11 @@ namespace DamageAssesment.Api.Questions.Providers
this.questionDbContext = questionDbContext; this.questionDbContext = questionDbContext;
this.logger = logger; this.logger = logger;
this.mapper = mapper; this.mapper = mapper;
SeedData(); // SeedData();
} }
public void 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()) if (!questionDbContext.QuestionTypes.Any())
{ {
questionDbContext.QuestionTypes.Add(new Db.QuestionType() { TypeText = "RadioButton" }); questionDbContext.QuestionTypes.Add(new Db.QuestionType() { TypeText = "RadioButton" });
@ -60,7 +34,7 @@ namespace DamageAssesment.Api.Questions.Providers
if (!questionDbContext.QuestionCategories.Any()) 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 = "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 = "Structural", IconLibrary = "https://example.com/images/img3.png" });
questionDbContext.QuestionCategories.Add(new Db.QuestionCategory() { IconName = "Utility", IconLibrary = "https://example.com/images/img4.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()) if (!questionDbContext.CategoryTranslations.Any())
{ {
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 1, Title = "Flooding", Language = "en" }); 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 = 2, Title = "Electrical", Language = "en" });
questionDbContext.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 3, Title = "Structural", 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.CategoryTranslations.Add(new Db.CategoryTranslation() { CategoryId = 5, Title = "Escombros", Language = "es" });
questionDbContext.SaveChanges(); 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) public List<Models.CategoryTranslation> GetCategoryTranslations(int id, string? language)

View File

@ -31,23 +31,24 @@ namespace DamageAssesment.Api.Responses.Providers
this.questionServiceProvider = questionServiceProvider; this.questionServiceProvider = questionServiceProvider;
this.surveyServiceProvider = surveyServiceProvider; this.surveyServiceProvider = surveyServiceProvider;
this.mapper = mapper; this.mapper = mapper;
seedData(); SeedData();
} }
private void seedData() public void SeedData()
{ {
// Check if SurveyResponses exist, if not, seed data
if (!surveyResponseDbContext.SurveyResponses.Any()) 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 = 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.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.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.SaveChanges(); surveyResponseDbContext.SaveChanges();
} }
} }
public async Task<(bool IsSuccess, dynamic Answers, string ErrorMessage)> GetAnswersByRegionAsync(int surveyId, int employeeid) public async Task<(bool IsSuccess, dynamic Answers, string ErrorMessage)> GetAnswersByRegionAsync(int surveyId, int employeeid)
{ {
try try

View File

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

View File

@ -18,39 +18,44 @@ namespace DamageAssesment.Api.Surveys.Providers
this.surveyDbContext = surveysDbContext; this.surveyDbContext = surveysDbContext;
this.logger = logger; this.logger = logger;
this.mapper = mapper; this.mapper = mapper;
seedData(); //seedData();
} }
// Method to seed initial data into the database
public void seedData() public void seedData()
{ {
if (!surveyDbContext.Surveys.Any()) if (!surveyDbContext.Surveys.Any())
{ {
surveyDbContext.Surveys.Add(new Db.Survey {IsEnabled = true, StartDate = DateTime.Now, EndDate = DateTime.Now.AddDays(90), CreatedDate = DateTime.Now }); var survey1 = 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 }); var survey2 = 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 }); var survey3 = new Db.Survey { IsEnabled = false, 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(); surveyDbContext.Surveys.Add(survey1);
} surveyDbContext.Surveys.Add(survey2);
surveyDbContext.Surveys.Add(survey3);
surveyDbContext.SaveChanges();
if (!surveyDbContext.SurveysTranslation.Any()) 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 = survey1.Id, 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 = survey1.Id, 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.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 = survey2.Id, 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 = survey2.Id, 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 = "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 { SurveyId = survey3.Id, Language = "en", Title = "Hurricane Idalia Aftermath Survey" });
//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 { SurveyId = survey3.Id, Language = "es", Title = "Encuesta sobre las secuelas del huracán Idalia" });
//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 = 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) if (SurveyTranslation == null)
{ {
@ -67,6 +72,8 @@ namespace DamageAssesment.Api.Surveys.Providers
} }
return SurveyTranslation; return SurveyTranslation;
} }
// Method to create a multi-language object from survey translations
public object CreateMultiLanguageObject(IEnumerable<Models.SurveyTranslation> surveyTranslations) public object CreateMultiLanguageObject(IEnumerable<Models.SurveyTranslation> surveyTranslations)
{ {
object MultiLanguage = new object(); object MultiLanguage = new object();
@ -78,28 +85,27 @@ namespace DamageAssesment.Api.Surveys.Providers
MultiLanguage = dict; MultiLanguage = dict;
return MultiLanguage; 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) public async Task<(bool IsSuccess, IEnumerable<Models.MultiLanSurvey> Surveys, string ErrorMessage)> GetSurveysAsync(string language)
{ {
IEnumerable<Models.MultiLanSurvey> surveysList = null; IEnumerable<Models.MultiLanSurvey> surveysList = null;
try 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 surveys = await surveyDbContext.Surveys.Where(s => s.IsEnabled == true).ToListAsync();
//var surveyTranslations = await surveyDbContext.SurveysTranslation.ToListAsync();
if (surveys != null) if (surveys != null)
{ {
surveysList = from s in surveys surveysList = from s in surveys
select new select new Models.MultiLanSurvey
Models.MultiLanSurvey
{ {
Id = s.Id, Id = s.Id,
StartDate = s.StartDate, StartDate = s.StartDate,
EndDate = s.EndDate, EndDate = s.EndDate,
IsEnabled = s.IsEnabled, IsEnabled = s.IsEnabled,
CreatedDate = s.CreatedDate, CreatedDate = s.CreatedDate,
Titles = CreateMultiLanguageObject(GetSurveyTranslations(s.Id,null, language)) Titles = CreateMultiLanguageObject(GetSurveyTranslations(s.Id, null, language))
}; };
logger?.LogInformation($"{surveys.Count} Items(s) found"); logger?.LogInformation($"{surveys.Count} Items(s) found");
@ -113,12 +119,15 @@ namespace DamageAssesment.Api.Surveys.Providers
return (false, null, ex.Message); 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) public async Task<(bool IsSuccess, Models.MultiLanSurvey Surveys, string ErrorMessage)> GetSurveysAsync(int id, string language)
{ {
try try
{ {
logger?.LogInformation("Query Survey"); logger?.LogInformation("Query Survey");
var survey = await surveyDbContext.Surveys.SingleOrDefaultAsync(s => s.Id == id && s.IsEnabled == true); var survey = await surveyDbContext.Surveys.SingleOrDefaultAsync(s => s.Id == id && s.IsEnabled == true);
if (survey != null) if (survey != null)
{ {
Models.MultiLanSurvey result = null; Models.MultiLanSurvey result = null;
@ -130,8 +139,7 @@ namespace DamageAssesment.Api.Surveys.Providers
EndDate = survey.EndDate, EndDate = survey.EndDate,
IsEnabled = survey.IsEnabled, IsEnabled = survey.IsEnabled,
CreatedDate = survey.CreatedDate, CreatedDate = survey.CreatedDate,
Titles = CreateMultiLanguageObject(GetSurveyTranslations(survey.Id,null, language)) Titles = CreateMultiLanguageObject(GetSurveyTranslations(survey.Id, null, language))
}; };
logger?.LogInformation($"Survey Id: {id} found"); logger?.LogInformation($"Survey Id: {id} found");
return (true, result, null); 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) public async Task<(bool IsSuccess, Models.MultiLanSurvey Survey, string ErrorMessage)> PostSurveyAsync(Models.Survey survey)
{ {
try try
@ -159,7 +168,7 @@ namespace DamageAssesment.Api.Surveys.Providers
foreach (var title in survey.Titles) 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(); await surveyDbContext.SaveChangesAsync();
var result = mapper.Map<Db.Survey, Models.MultiLanSurvey>(_survey); var result = mapper.Map<Db.Survey, Models.MultiLanSurvey>(_survey);
@ -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) public async Task<(bool IsSuccess, Models.MultiLanSurvey Survey, string ErrorMessage)> PutSurveyAsync(int Id, Models.Survey survey)
{ {
try 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) public async Task<(bool IsSuccess, Models.MultiLanSurvey Survey, string ErrorMessage)> DeleteSurveyAsync(int Id)
{ {
try try
@ -240,7 +251,7 @@ namespace DamageAssesment.Api.Surveys.Providers
result.Titles = CreateMultiLanguageObject(GetSurveyTranslations(survey.Id, null, "")); result.Titles = CreateMultiLanguageObject(GetSurveyTranslations(survey.Id, null, ""));
surveyDbContext.Surveys.Remove(survey); surveyDbContext.Surveys.Remove(survey);
await surveyDbContext.SaveChangesAsync(); await surveyDbContext.SaveChangesAsync();
return (true, result, $"Survey Id: {Id} deleted Successfuly"); return (true, result, $"Survey Id: {Id} deleted Successfully");
} }
else else
{ {

View File

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