Update DbContext Changes

This commit is contained in:
Reginald Cherenfant Jasmin 2023-08-25 18:44:04 -04:00
parent 099055d088
commit 7a50089f6a
7 changed files with 27 additions and 29 deletions

View File

@ -55,11 +55,11 @@ namespace DamageAssesment.Api.Employees.Controllers
/// </summary> /// </summary>
/// <param name="Employee">The updated employee object.</param> /// <param name="Employee">The updated employee object.</param>
[HttpPut("Employees")] [HttpPut("Employees")]
public async Task<IActionResult> UpdateEmployee(Db.Employee Employee) public async Task<IActionResult> UpdateEmployee(string Id, Models.Employee Employee)
{ {
if (Employee != null) if (Employee != null)
{ {
var result = await this.EmployeeProvider.UpdateEmployeeAsync(Employee); var result = await this.EmployeeProvider.UpdateEmployeeAsync(Id,Employee);
if (result.IsSuccess) if (result.IsSuccess)
{ {
return Ok(result.Employee); return Ok(result.Employee);
@ -77,7 +77,7 @@ namespace DamageAssesment.Api.Employees.Controllers
/// </summary> /// </summary>
/// <param name="Employee">The employee information for creating a new employee.</param> /// <param name="Employee">The employee information for creating a new employee.</param>
[HttpPost("Employees")] [HttpPost("Employees")]
public async Task<IActionResult> CreateEmployee(Db.Employee Employee) public async Task<IActionResult> CreateEmployee(Models.Employee Employee)
{ {
if (Employee != null) if (Employee != null)
{ {

View File

@ -7,8 +7,6 @@ namespace DamageAssesment.Api.Employees.Db
public DbSet<Db.Employee> Employees { get; set; } public DbSet<Db.Employee> Employees { get; set; }
public EmployeeDbContext(DbContextOptions options) : base(options) public EmployeeDbContext(DbContextOptions options) : base(options)
{ {
} }
} }
} }

View File

@ -4,8 +4,8 @@
{ {
Task<(bool IsSuccess, IEnumerable<Models.Employee> Employees, string ErrorMessage)> GetEmployeesAsync(); Task<(bool IsSuccess, IEnumerable<Models.Employee> Employees, string ErrorMessage)> GetEmployeesAsync();
Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> GetEmployeeByIdAsync(string Id); 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)> PostEmployeeAsync(Models.Employee Employee);
Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> UpdateEmployeeAsync(Db.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); Task<(bool IsSuccess, Models.Employee Employee, string ErrorMessage)> DeleteEmployeeAsync(string Id);
} }
} }

View File

@ -65,17 +65,18 @@ namespace DamageAssesment.Api.Employees.Providers
return (false, null, ex.Message); 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 try
{ {
Db.Employee _employee = mapper.Map<Models.Employee, Db.Employee>(Employee);
logger?.LogInformation("Query Employee"); logger?.LogInformation("Query Employee");
if (!EmployeeExists(Employee.Id)) if (!EmployeeExists(Employee.Id))
{ {
EmployeeDbContext.Employees.Add(Employee); EmployeeDbContext.Employees.Add(_employee);
EmployeeDbContext.SaveChanges(); EmployeeDbContext.SaveChanges();
var result = mapper.Map<Db.Employee, Models.Employee>(Employee); return (true, Employee, null);
return (true, result, null);
} }
return (false, null, "Employee is already exits"); return (false, null, "Employee is already exits");
} }
@ -85,19 +86,20 @@ namespace DamageAssesment.Api.Employees.Providers
return (false, null, ex.Message); 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 try
{ {
if (Employee != null) 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) if (_employee != null)
{ {
EmployeeDbContext.Employees.Update(Employee); Db.Employee vEmployee = mapper.Map<Models.Employee, Db.Employee>(Employee);
EmployeeDbContext.Employees.Update(vEmployee);
EmployeeDbContext.SaveChanges(); EmployeeDbContext.SaveChanges();
return (true, mapper.Map<Db.Employee, Models.Employee>(Employee), "Successful"); return (true, Employee, "Successful");
} }
else else
{ {

View File

@ -6,8 +6,8 @@ namespace DamageAssesment.Api.Locations.Interfaces
{ {
Task<(bool IsSuccess, IEnumerable<Models.Location> locations, string ErrorMessage)> GetLocationsAsync(); Task<(bool IsSuccess, IEnumerable<Models.Location> locations, string ErrorMessage)> GetLocationsAsync();
Task<(bool IsSuccess, Models.Location Location, string ErrorMessage)> GetLocationByIdAsync(string Id); 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, Models.Location Question, string ErrorMessage)> PostLocationAsync(Models.Location Location);
Task<(bool IsSuccess, string ErrorMessage)> UpdateLocationAsync(Db.Location Location); Task<(bool IsSuccess, string ErrorMessage)> UpdateLocationAsync(Models.Location Location);
Task<(bool IsSuccess, string ErrorMessage)> DeleteLocationAsync(string Id); Task<(bool IsSuccess, string ErrorMessage)> DeleteLocationAsync(string Id);
} }
} }

View File

@ -63,17 +63,17 @@ namespace DamageAssesment.Api.Locations.Providers
return (false, null, ex.Message); 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 try
{ {
logger?.LogInformation("Query Location"); logger?.LogInformation("Query Location");
if (!LocationExists(Location.Id)) if (!LocationExists(Location.Id))
{ {
locationDbContext.Locations.Add(Location); Db.Location _location = mapper.Map<Models.Location, Db.Location>(Location);
locationDbContext.Locations.Add(_location);
locationDbContext.SaveChanges(); locationDbContext.SaveChanges();
var result = mapper.Map<Db.Location, Models.Location>(Location); return (true, Location, null);
return (true, result, null);
} }
else else
{ {
@ -86,11 +86,12 @@ namespace DamageAssesment.Api.Locations.Providers
return (false, null, ex.Message); 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 try
{ {
locationDbContext.Entry(Location).State = EntityState.Modified; Db.Location _location = mapper.Map<Models.Location, Db.Location>(Location);
locationDbContext.Entry(_location).State = EntityState.Modified;
locationDbContext.SaveChanges(); locationDbContext.SaveChanges();
return (true, "Record updated successfully"); return (true, "Record updated successfully");
} }

View File

@ -181,15 +181,12 @@ namespace DamageAssesment.Api.Surveys.Providers
{ {
if (survey != null) if (survey != null)
{ {
var surveys = await surveyDbContext.Surveys.ToListAsync(); survey.CreatedDate = DateTime.Now;
Db.Survey _survey = mapper.Map<Models.Survey, Db.Survey>(survey);
Db.Survey _survey = new Db.Survey { IsEnabled = survey.IsEnabled, StartDate = survey.StartDate, EndDate = survey.EndDate, CreatedDate = DateTime.Now };
surveyDbContext.Surveys.Add(_survey); surveyDbContext.Surveys.Add(_survey);
await surveyDbContext.SaveChangesAsync(); await surveyDbContext.SaveChangesAsync();
//var surveyTranslations = await surveyDbContext.SurveysTranslation.ToListAsync();
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 });