From 9b91afd329a3f9a4d76690b79ede48379e2bce22 Mon Sep 17 00:00:00 2001 From: Santhosh S <913341@dadeschools.net> Date: Fri, 6 Oct 2023 18:22:37 -0400 Subject: [PATCH] bug fix with seed data changes --- .../Providers/AnswerProvider.cs | 36 +++++---- .../Response-1/Answer-1/Attachment_1.txt | 1 + .../DASA_Documents/Active/Document_1.txt | 1 + .../DASA_Documents/Active/Document_2.txt | 1 + .../DamageAssesment.Api.DocuLinks.csproj | 2 +- .../Providers/EmployeesProvider.cs | 6 +- .../Providers/LocationsProvider.cs | 7 +- .../Providers/RegionsProvider.cs | 6 +- .../Providers/QuestionsProvider.cs | 62 ++++++++------- .../Providers/SurveyResponsesProvider.cs | 15 ++-- .../appsettings.json | 3 +- .../Providers/SurveysProvider.cs | 75 +++++++++++-------- DamageAssesmentApi/docker-compose.yml | 9 +-- 13 files changed, 124 insertions(+), 100 deletions(-) create mode 100644 DamageAssesmentApi/DamageAssesment.Api.Attachments/DMS_Attachments/Active/Response-1/Answer-1/Attachment_1.txt create mode 100644 DamageAssesmentApi/DamageAssesment.Api.DocuLinks/DASA_Documents/Active/Document_1.txt create mode 100644 DamageAssesmentApi/DamageAssesment.Api.DocuLinks/DASA_Documents/Active/Document_2.txt diff --git a/DamageAssesmentApi/DamageAssesment.Api.Answers/Providers/AnswerProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Answers/Providers/AnswerProvider.cs index 9291628..84dff1d 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Answers/Providers/AnswerProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Answers/Providers/AnswerProvider.cs @@ -8,19 +8,20 @@ namespace DamageAssesment.Api.Answers.Providers { public class AnswersProvider : IAnswersProvider { - private AnswerDbContext answerDbContext; private ILogger logger; private IMapper mapper; + // Constructor with dependency injection and data seeding public AnswersProvider(AnswerDbContext answerDbContext, ILogger 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 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 Answers, string ErrorMessage)> GetAnswersAsync(int surveyResponseId) { try @@ -74,17 +77,17 @@ namespace DamageAssesment.Api.Answers.Providers { var result = mapper.Map, IEnumerable>(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 Answers, string ErrorMessage)> GetAnswersByQuestionAsync(int questionId) { try @@ -96,17 +99,17 @@ namespace DamageAssesment.Api.Answers.Providers { var result = mapper.Map, IEnumerable>(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(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(answer), $"AnswerId {Id} deleted Successfuly"); + return (true, mapper.Map(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 }); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Attachments/DMS_Attachments/Active/Response-1/Answer-1/Attachment_1.txt b/DamageAssesmentApi/DamageAssesment.Api.Attachments/DMS_Attachments/Active/Response-1/Answer-1/Attachment_1.txt new file mode 100644 index 0000000..eed7e79 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.Attachments/DMS_Attachments/Active/Response-1/Answer-1/Attachment_1.txt @@ -0,0 +1 @@ +sample \ No newline at end of file diff --git a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/DASA_Documents/Active/Document_1.txt b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/DASA_Documents/Active/Document_1.txt new file mode 100644 index 0000000..eed7e79 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/DASA_Documents/Active/Document_1.txt @@ -0,0 +1 @@ +sample \ No newline at end of file diff --git a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/DASA_Documents/Active/Document_2.txt b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/DASA_Documents/Active/Document_2.txt new file mode 100644 index 0000000..eed7e79 --- /dev/null +++ b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/DASA_Documents/Active/Document_2.txt @@ -0,0 +1 @@ +sample \ No newline at end of file diff --git a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/DamageAssesment.Api.DocuLinks.csproj b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/DamageAssesment.Api.DocuLinks.csproj index 2e8659c..45347ee 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/DamageAssesment.Api.DocuLinks.csproj +++ b/DamageAssesmentApi/DamageAssesment.Api.DocuLinks/DamageAssesment.Api.DocuLinks.csproj @@ -1,4 +1,4 @@ - + net6.0 diff --git a/DamageAssesmentApi/DamageAssesment.Api.Employees/Providers/EmployeesProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Employees/Providers/EmployeesProvider.cs index eed1e09..b1741be 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Employees/Providers/EmployeesProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Employees/Providers/EmployeesProvider.cs @@ -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 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" }); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Locations/Providers/LocationsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Locations/Providers/LocationsProvider.cs index 9952655..198a765 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Locations/Providers/LocationsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Locations/Providers/LocationsProvider.cs @@ -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 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" }); diff --git a/DamageAssesmentApi/DamageAssesment.Api.Locations/Providers/RegionsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Locations/Providers/RegionsProvider.cs index 7e140ab..2eb72ac 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Locations/Providers/RegionsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Locations/Providers/RegionsProvider.cs @@ -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(); } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs index 850c165..92da2b1 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Questions/Providers/QuestionsProvider.cs @@ -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 GetCategoryTranslations(int id, string? language) diff --git a/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs index 0631932..5278c5c 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Responses/Providers/SurveyResponsesProvider.cs @@ -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 diff --git a/DamageAssesmentApi/DamageAssesment.Api.Responses/appsettings.json b/DamageAssesmentApi/DamageAssesment.Api.Responses/appsettings.json index 914d671..f23b75a 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Responses/appsettings.json +++ b/DamageAssesmentApi/DamageAssesment.Api.Responses/appsettings.json @@ -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": { diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs index 3d08544..2d6cbc9 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs @@ -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 GetSurveyTranslations(int id, IEnumerable SurveyTranslation,string? language) + // Method to get survey translations for a given survey ID and language + public IEnumerable GetSurveyTranslations(int id, IEnumerable 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 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 Surveys, string ErrorMessage)> GetSurveysAsync(string language) { IEnumerable 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(_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 { diff --git a/DamageAssesmentApi/docker-compose.yml b/DamageAssesmentApi/docker-compose.yml index f421f5a..0e6f487 100644 --- a/DamageAssesmentApi/docker-compose.yml +++ b/DamageAssesmentApi/docker-compose.yml @@ -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: