Update survey response, adding EmployeeId , Location Id as int, adjust end point for ansers submission in batch

This commit is contained in:
Reginald Cherenfant Jasmin
2023-09-13 01:28:24 -04:00
parent 4cf7d9f891
commit 9109d0d793
68 changed files with 525 additions and 540 deletions

View File

@ -20,39 +20,16 @@ namespace DamageAssesment.Api.Locations.Providers
//SeedData();
}
public async Task<(bool IsSuccess, IEnumerable<Models.Location> locations, string ErrorMessage)> GetLocationsAsync()
{
try
{
logger?.LogInformation("Query Question");
var Location = await locationDbContext.Locations.AsNoTracking().ToListAsync();
if (Location != null)
{
logger?.LogInformation($"{Location.Count} Locations(s) found");
var result = mapper.Map<IEnumerable<Db.Location>, IEnumerable<Models.Location>>(Location);
return (true, result, null);
}
return (false, null, "Not found");
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
public async Task<(bool IsSuccess, Models.Location Location, string ErrorMessage)> GetLocationByIdAsync(string Id)
public async Task<(bool IsSuccess, IEnumerable<Models.Location> Locations, string ErrorMessage)> GetLocationsAsync()
{
try
{
logger?.LogInformation("Query Location");
var Location = await locationDbContext.Locations.AsNoTracking().FirstOrDefaultAsync(q => q.Id == Id);
if (Location != null)
var locations = await locationDbContext.Locations.AsNoTracking().ToListAsync();
if (locations != null)
{
logger?.LogInformation($"{Location} customer(s) found");
var result = mapper.Map<Db.Location, Models.Location>(Location);
logger?.LogInformation($"{locations.Count} Locations(s) found");
var result = mapper.Map<IEnumerable<Db.Location>, IEnumerable<Models.Location>>(locations);
return (true, result, null);
}
return (false, null, "Not found");
@ -63,21 +40,42 @@ namespace DamageAssesment.Api.Locations.Providers
return (false, null, ex.Message);
}
}
public async Task<(bool IsSuccess, Models.Location Question, string ErrorMessage)> PostLocationAsync(Models.Location Location)
public async Task<(bool IsSuccess, Models.Location Location, string ErrorMessage)> GetLocationByIdAsync(int Id)
{
try
{
logger?.LogInformation("Query Location");
if (!LocationExists(Location.Id))
var location = await locationDbContext.Locations.AsNoTracking().FirstOrDefaultAsync(q => q.Id == Id);
if (location != null)
{
Db.Location _location = mapper.Map<Models.Location, Db.Location>(Location);
logger?.LogInformation($"{location} found");
var result = mapper.Map<Db.Location, Models.Location>(location);
return (true, result, null);
}
return (false, null, "Not found");
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
public async Task<(bool IsSuccess, Models.Location Location, string ErrorMessage)> PostLocationAsync(Models.Location location)
{
try
{
if (!LocationCodeExists(location.LocationCode))
{
Db.Location _location = mapper.Map<Models.Location, Db.Location>(location);
locationDbContext.Locations.Add(_location);
locationDbContext.SaveChanges();
return (true, Location, null);
await locationDbContext.SaveChangesAsync();
location.Id = _location.Id;
return (true, location, null);
}
else
{
return (false, null, "Location is Already exists");
return (false, null, "Location code is already exists");
}
}
catch (Exception ex)
@ -86,57 +84,67 @@ namespace DamageAssesment.Api.Locations.Providers
return (false, null, ex.Message);
}
}
public async Task<(bool IsSuccess, string ErrorMessage)> UpdateLocationAsync(Models.Location Location)
public async Task<(bool IsSuccess, Models.Location Location, string ErrorMessage)> UpdateLocationAsync(int Id, Models.Location location)
{
try
{
Db.Location _location = mapper.Map<Models.Location, Db.Location>(Location);
locationDbContext.Entry(_location).State = EntityState.Modified;
locationDbContext.SaveChanges();
return (true, "Record updated successfully");
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, ex.Message);
}
}
public async Task<(bool IsSuccess, string ErrorMessage)> DeleteLocationAsync(string Id)
{
try
{
Db.Location Location = locationDbContext.Locations.AsNoTracking().Where(a => a.Id == Id).FirstOrDefault();
if (Location == null)
if (LocationExists(Id))
{
return (false, "record not found");
Db.Location _location = mapper.Map<Models.Location, Db.Location>(location);
locationDbContext.Entry(_location).State = EntityState.Modified;
await locationDbContext.SaveChangesAsync();
return (true, location, "Record updated successfully");
}
else
{
return (false, null, "Location is not exists");
}
locationDbContext.Locations.Remove(Location);
locationDbContext.SaveChanges();
return (true, "Record deleted successfully");
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, ex.Message);
return (false, null, ex.Message);
}
}
public async Task<(bool IsSuccess, Models.Location Location, string ErrorMessage)> DeleteLocationAsync(int Id)
{
try
{
Db.Location location = locationDbContext.Locations.AsNoTracking().Where(a => a.Id == Id).FirstOrDefault();
if (location == null)
{
return (false, null, "record not found");
}
locationDbContext.Locations.Remove(location);
await locationDbContext.SaveChangesAsync();
return (true, mapper.Map<Db.Location, Models.Location>(location), "Record deleted successfully");
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
private bool LocationExists(string id)
private bool LocationExists(int id)
{
return locationDbContext.Locations.AsNoTracking().Count(e => e.Id == id) > 0;
}
private bool LocationCodeExists(string locationCode)
{
return locationDbContext.Locations.AsNoTracking().Count(e => e.LocationCode.ToLower() == locationCode.ToLower()) > 0;
}
public void SeedData()
{
if (!locationDbContext.Locations.Any())
{
locationDbContext.Locations.Add(new Db.Location() { Id = "Loc1", RegionId = "1", Name = "BOB GRAHAM EDUCATION CENTER 1", MaintenanceCenter = "1", SchoolType = "US" });
locationDbContext.Locations.Add(new Db.Location() { Id = "Loc2", RegionId = "2", Name = "BOB GRAHAM EDUCATION CENTER 2", MaintenanceCenter = "1", SchoolType = "US" });
locationDbContext.Locations.Add(new Db.Location() { Id = "Loc3", RegionId = "3", Name = "BOB GRAHAM EDUCATION CENTER 3", MaintenanceCenter = "1", SchoolType = "US" });
locationDbContext.Locations.Add(new Db.Location() { Id = "Loc4", RegionId = "1", Name = "BOB GRAHAM EDUCATION CENTER 4", MaintenanceCenter = "1", SchoolType = "US" });
locationDbContext.Locations.Add(new Db.Location() { Id = "Loc5", RegionId = "2", Name = "BOB GRAHAM EDUCATION CENTER 5", MaintenanceCenter = "1", SchoolType = "US" });
locationDbContext.Locations.Add(new Db.Location() { Id = "Loc6", RegionId = "3", Name = "BOB GRAHAM EDUCATION CENTER 6", MaintenanceCenter = "1", SchoolType = "US" });
locationDbContext.Locations.Add(new Db.Location() { Id = 1, LocationCode = "Loc1", RegionId = 1, Name = "BOB GRAHAM EDUCATION CENTER 1", MaintenanceCenter = "1", SchoolType = "US" });
locationDbContext.Locations.Add(new Db.Location() { Id = 2, LocationCode = "Loc2", RegionId = 2, Name = "BOB GRAHAM EDUCATION CENTER 2", 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 = 5, LocationCode = "Loc5", RegionId = 2, Name = "BOB GRAHAM EDUCATION CENTER 5", MaintenanceCenter = "1", SchoolType = "US" });
locationDbContext.Locations.Add(new Db.Location() { Id = 6, LocationCode = "Loc6", RegionId = 3, Name = "BOB GRAHAM EDUCATION CENTER 6", MaintenanceCenter = "1", SchoolType = "US" });
locationDbContext.SaveChanges();
}

View File

@ -19,7 +19,7 @@ namespace DamageAssesment.Api.Locations.Providers
//SeedData();
}
public async Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> GetRegionByIdAsync(string Id)
public async Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> GetRegionByIdAsync(int Id)
{
try
{
@ -41,7 +41,7 @@ namespace DamageAssesment.Api.Locations.Providers
}
}
public async Task<(bool IsSuccess, IEnumerable<Models.Region> regions, string ErrorMessage)> GetRegionsAsync()
public async Task<(bool IsSuccess, IEnumerable<Models.Region> Regions, string ErrorMessage)> GetRegionsAsync()
{
try
{
@ -69,11 +69,10 @@ namespace DamageAssesment.Api.Locations.Providers
{
if (region != null)
{
var regions = await locationDbContext.Regions.AsNoTracking().ToListAsync();
region.Id = Convert.ToString(regions.Count + 1);
locationDbContext.Regions.Add(mapper.Map<Models.Region, Db.Region>(region));
locationDbContext.SaveChanges();
var _region = mapper.Map<Models.Region, Db.Region>(region);
locationDbContext.Regions.Add(_region);
await locationDbContext.SaveChangesAsync();
region.Id = _region.Id;
logger?.LogInformation($"{region} added successfuly");
return (true, region, "Successful");
}
@ -90,30 +89,38 @@ namespace DamageAssesment.Api.Locations.Providers
}
}
public async Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> PutRegionAsync(Models.Region region)
public async Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> PutRegionAsync(int Id, Models.Region region)
{
try
{
if (region != null)
{
var _region = await locationDbContext.Regions.AsNoTracking().Where(s => s.Id == region.Id).FirstOrDefaultAsync();
var _region = await locationDbContext.Regions.AsNoTracking().Where(s => s.Id == Id).FirstOrDefaultAsync();
if (_region != null)
{
locationDbContext.Regions.Update(mapper.Map<Models.Region, Db.Region>(region));
locationDbContext.SaveChanges();
return (true, region, "Region updated Successfuly");
if (RegionExists(region.Id))
{
locationDbContext.Regions.Update(mapper.Map<Models.Region, Db.Region>(region));
locationDbContext.SaveChanges();
return (true, region, "Region updated Successfuly");
}
else
{
logger?.LogInformation($"RegionID: {Id} Not exists");
return (false, null, "Region not exists");
}
}
else
{
logger?.LogInformation($"RegionID: {region.Id} Not found");
logger?.LogInformation($"RegionID: {Id} Not found");
return (false, null, "Not Found");
}
}
else
{
logger?.LogInformation($"RegionID: {region.Id} Bad Request");
logger?.LogInformation($"RegionID: {Id} Bad Request");
return (false, null, "Bad request");
}
}
@ -124,7 +131,7 @@ namespace DamageAssesment.Api.Locations.Providers
}
}
public async Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> DeleteRegionAsync(string Id)
public async Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> DeleteRegionAsync(int Id)
{
try
{
@ -149,13 +156,18 @@ namespace DamageAssesment.Api.Locations.Providers
}
}
private bool RegionExists(int id)
{
return locationDbContext.Regions.AsNoTracking().Count(e => e.Id == id) > 0;
}
public void SeedData()
{
if (!locationDbContext.Regions.Any())
{
locationDbContext.Regions.Add(new Db.Region() { Id = "1", Name = "North", Abbreviation = "N" });
locationDbContext.Regions.Add(new Db.Region() { Id = "2", Name = "South", Abbreviation = "S" });
locationDbContext.Regions.Add(new Db.Region() { Id = "3", Name = "Central", Abbreviation = "C" });
locationDbContext.Regions.Add(new Db.Region() { Id = 1, Name = "North", Abbreviation = "N" });
locationDbContext.Regions.Add(new Db.Region() { Id = 2, Name = "South", Abbreviation = "S" });
locationDbContext.Regions.Add(new Db.Region() { Id = 3, Name = "Central", Abbreviation = "C" });
locationDbContext.SaveChanges();
}
}