From 7a50089f6a67496e41426c6254f862b118fde918 Mon Sep 17 00:00:00 2001 From: Reginald Cherenfant Jasmin Date: Fri, 25 Aug 2023 18:44:04 -0400 Subject: [PATCH] Update DbContext Changes --- .../Controllers/EmployeesController.cs | 6 +++--- .../Db/EmployeeDbContext.cs | 2 -- .../Interfaces/IEmployeesProvider.cs | 4 ++-- .../Providers/EmployeesProvider.cs | 18 ++++++++++-------- .../Interfaces/ILocationsProvider.cs | 4 ++-- .../Providers/LocationsProvider.cs | 13 +++++++------ .../Providers/SurveysProvider.cs | 9 +++------ 7 files changed, 27 insertions(+), 29 deletions(-) diff --git a/DamageAssesmentApi/DamageAssesment.Api.Employees/Controllers/EmployeesController.cs b/DamageAssesmentApi/DamageAssesment.Api.Employees/Controllers/EmployeesController.cs index a074f21..8d004e6 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Employees/Controllers/EmployeesController.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Employees/Controllers/EmployeesController.cs @@ -55,11 +55,11 @@ namespace DamageAssesment.Api.Employees.Controllers /// /// The updated employee object. [HttpPut("Employees")] - public async Task UpdateEmployee(Db.Employee Employee) + public async Task UpdateEmployee(string Id, Models.Employee Employee) { if (Employee != null) { - var result = await this.EmployeeProvider.UpdateEmployeeAsync(Employee); + var result = await this.EmployeeProvider.UpdateEmployeeAsync(Id,Employee); if (result.IsSuccess) { return Ok(result.Employee); @@ -77,7 +77,7 @@ namespace DamageAssesment.Api.Employees.Controllers /// /// The employee information for creating a new employee. [HttpPost("Employees")] - public async Task CreateEmployee(Db.Employee Employee) + public async Task CreateEmployee(Models.Employee Employee) { if (Employee != null) { diff --git a/DamageAssesmentApi/DamageAssesment.Api.Employees/Db/EmployeeDbContext.cs b/DamageAssesmentApi/DamageAssesment.Api.Employees/Db/EmployeeDbContext.cs index 64234cd..0d7bc5e 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Employees/Db/EmployeeDbContext.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Employees/Db/EmployeeDbContext.cs @@ -7,8 +7,6 @@ namespace DamageAssesment.Api.Employees.Db public DbSet Employees { get; set; } public EmployeeDbContext(DbContextOptions options) : base(options) { - - } } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Employees/Interfaces/IEmployeesProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Employees/Interfaces/IEmployeesProvider.cs index 059483d..529141c 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Employees/Interfaces/IEmployeesProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Employees/Interfaces/IEmployeesProvider.cs @@ -4,8 +4,8 @@ { Task<(bool IsSuccess, IEnumerable Employees, string ErrorMessage)> GetEmployeesAsync(); Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> GetEmployeeByIdAsync(string Id); - Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> PostEmployeeAsync(Db.Employee Employee); - Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> UpdateEmployeeAsync(Db.Employee Employee); + Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> PostEmployeeAsync(Models.Employee Employee); + Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> UpdateEmployeeAsync(string Id, Models.Employee Employee); Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> DeleteEmployeeAsync(string Id); } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Employees/Providers/EmployeesProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Employees/Providers/EmployeesProvider.cs index 8e31e82..67ca1eb 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Employees/Providers/EmployeesProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Employees/Providers/EmployeesProvider.cs @@ -65,17 +65,18 @@ namespace DamageAssesment.Api.Employees.Providers return (false, null, ex.Message); } } - public async Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> PostEmployeeAsync(Db.Employee Employee) + public async Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> PostEmployeeAsync(Models.Employee Employee) { try { + Db.Employee _employee = mapper.Map(Employee); + logger?.LogInformation("Query Employee"); if (!EmployeeExists(Employee.Id)) { - EmployeeDbContext.Employees.Add(Employee); + EmployeeDbContext.Employees.Add(_employee); EmployeeDbContext.SaveChanges(); - var result = mapper.Map(Employee); - return (true, result, null); + return (true, Employee, null); } return (false, null, "Employee is already exits"); } @@ -85,19 +86,20 @@ namespace DamageAssesment.Api.Employees.Providers return (false, null, ex.Message); } } - public async Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> UpdateEmployeeAsync(Db.Employee Employee) + public async Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> UpdateEmployeeAsync(string Id , Models.Employee Employee) { try { if (Employee != null) { - var _employee = await EmployeeDbContext.Employees.AsNoTracking().Where(s => s.Id.ToLower() == Employee.Id.ToLower()).FirstOrDefaultAsync(); + var _employee = await EmployeeDbContext.Employees.AsNoTracking().Where(s => s.Id.ToLower() == Id.ToLower()).FirstOrDefaultAsync(); if (_employee != null) { - EmployeeDbContext.Employees.Update(Employee); + Db.Employee vEmployee = mapper.Map(Employee); + EmployeeDbContext.Employees.Update(vEmployee); EmployeeDbContext.SaveChanges(); - return (true, mapper.Map(Employee), "Successful"); + return (true, Employee, "Successful"); } else { diff --git a/DamageAssesmentApi/DamageAssesment.Api.Locations/Interfaces/ILocationsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Locations/Interfaces/ILocationsProvider.cs index 8e85496..ff0ee55 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Locations/Interfaces/ILocationsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Locations/Interfaces/ILocationsProvider.cs @@ -6,8 +6,8 @@ namespace DamageAssesment.Api.Locations.Interfaces { Task<(bool IsSuccess, IEnumerable locations, string ErrorMessage)> GetLocationsAsync(); Task<(bool IsSuccess, Models.Location Location, string ErrorMessage)> GetLocationByIdAsync(string Id); - Task<(bool IsSuccess, Models.Location Question, string ErrorMessage)> PostLocationAsync(Db.Location Location); - Task<(bool IsSuccess, string ErrorMessage)> UpdateLocationAsync(Db.Location Location); + Task<(bool IsSuccess, Models.Location Question, string ErrorMessage)> PostLocationAsync(Models.Location Location); + Task<(bool IsSuccess, string ErrorMessage)> UpdateLocationAsync(Models.Location Location); Task<(bool IsSuccess, string ErrorMessage)> DeleteLocationAsync(string Id); } } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Locations/Providers/LocationsProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Locations/Providers/LocationsProvider.cs index fcba375..cf0e978 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Locations/Providers/LocationsProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Locations/Providers/LocationsProvider.cs @@ -63,17 +63,17 @@ namespace DamageAssesment.Api.Locations.Providers return (false, null, ex.Message); } } - public async Task<(bool IsSuccess, Models.Location Question, string ErrorMessage)> PostLocationAsync(Db.Location Location) + public async Task<(bool IsSuccess, Models.Location Question, string ErrorMessage)> PostLocationAsync(Models.Location Location) { try { logger?.LogInformation("Query Location"); if (!LocationExists(Location.Id)) { - locationDbContext.Locations.Add(Location); + Db.Location _location = mapper.Map(Location); + locationDbContext.Locations.Add(_location); locationDbContext.SaveChanges(); - var result = mapper.Map(Location); - return (true, result, null); + return (true, Location, null); } else { @@ -86,11 +86,12 @@ namespace DamageAssesment.Api.Locations.Providers return (false, null, ex.Message); } } - public async Task<(bool IsSuccess, string ErrorMessage)> UpdateLocationAsync(Db.Location Location) + public async Task<(bool IsSuccess, string ErrorMessage)> UpdateLocationAsync(Models.Location Location) { try { - locationDbContext.Entry(Location).State = EntityState.Modified; + Db.Location _location = mapper.Map(Location); + locationDbContext.Entry(_location).State = EntityState.Modified; locationDbContext.SaveChanges(); return (true, "Record updated successfully"); } diff --git a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs index 6070d78..38ea756 100644 --- a/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs +++ b/DamageAssesmentApi/DamageAssesment.Api.Surveys/Providers/SurveysProvider.cs @@ -181,15 +181,12 @@ namespace DamageAssesment.Api.Surveys.Providers { if (survey != null) { - var surveys = await surveyDbContext.Surveys.ToListAsync(); + survey.CreatedDate = DateTime.Now; + Db.Survey _survey = mapper.Map(survey); - Db.Survey _survey = new Db.Survey { IsEnabled = survey.IsEnabled, StartDate = survey.StartDate, EndDate = survey.EndDate, CreatedDate = DateTime.Now }; - surveyDbContext.Surveys.Add(_survey); await surveyDbContext.SaveChangesAsync(); - - //var surveyTranslations = await surveyDbContext.SurveysTranslation.ToListAsync(); - + foreach (var title in survey.Titles) { surveyDbContext.SurveysTranslation.Add(new Db.SurveyTranslation {SurveyId = _survey.Id, Language = title.Language, Title = title.Title });