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

@ -4,12 +4,10 @@ using Microsoft.AspNetCore.Mvc;
namespace DamageAssesment.Api.Locations.Controllers
{
[Route("api")]
[ApiController]
public class LocationsController : ControllerBase
{
private ILocationsProvider LocationProvider;
public LocationsController(ILocationsProvider LocationsProvider)
{
this.LocationProvider = LocationsProvider;
@ -25,7 +23,7 @@ namespace DamageAssesment.Api.Locations.Controllers
var result = await LocationProvider.GetLocationsAsync();
if (result.IsSuccess)
{
return Ok(result.locations);
return Ok(result.Locations);
}
return NoContent();
@ -35,7 +33,7 @@ namespace DamageAssesment.Api.Locations.Controllers
/// </summary>
[HttpGet("Locations/{id}")]
public async Task<ActionResult> GetLocationByIdAsync(string id)
public async Task<ActionResult> GetLocationByIdAsync(int id)
{
var result = await LocationProvider.GetLocationByIdAsync(id);
@ -50,15 +48,15 @@ namespace DamageAssesment.Api.Locations.Controllers
/// Update a Location.
/// </summary>
[HttpPut("Locations")]
public async Task<IActionResult> UpdateLocation(Models.Location Location)
[HttpPut("Locations/{id}")]
public async Task<IActionResult> UpdateLocation(int id, Models.Location Location)
{
if (Location != null)
{
var result = await this.LocationProvider.UpdateLocationAsync(Location);
var result = await this.LocationProvider.UpdateLocationAsync(id, Location);
if (result.IsSuccess)
{
return Ok(result.ErrorMessage);
return Ok(result.Location);
}
return NotFound();
}
@ -76,9 +74,9 @@ namespace DamageAssesment.Api.Locations.Controllers
var result = await this.LocationProvider.PostLocationAsync(Location);
if (result.IsSuccess)
{
return Ok(result.Question);
return Ok(result.Location);
}
return BadRequest();
return BadRequest(result.ErrorMessage);
}
return BadRequest();
}
@ -87,12 +85,12 @@ namespace DamageAssesment.Api.Locations.Controllers
/// </summary>
[HttpDelete("Locations/{id}")]
public async Task<IActionResult> DeleteLocation(string id)
public async Task<IActionResult> DeleteLocation(int id)
{
var result = await this.LocationProvider.DeleteLocationAsync(id);
if (result.IsSuccess)
{
return Ok(result.ErrorMessage);
return Ok(result.Location);
}
return NotFound();
}

View File

@ -3,7 +3,6 @@ using Microsoft.AspNetCore.Mvc;
namespace DamageAssesment.Api.Locations.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class RegionsController : ControllerBase
{
@ -17,13 +16,13 @@ namespace DamageAssesment.Api.Locations.Controllers
/// Get all regions.2
/// </summary>
[HttpGet]
[HttpGet("regions")]
public async Task<ActionResult> GetRegionsAsync()
{
var result = await regionProvider.GetRegionsAsync();
if (result.IsSuccess)
{
return Ok(result.regions);
return Ok(result.Regions);
}
return NoContent();
}
@ -31,10 +30,10 @@ namespace DamageAssesment.Api.Locations.Controllers
/// GET request for retrieving a region by its ID.
/// </summary>
[HttpGet("{Id}")]
public async Task<ActionResult> GetRegionAsync(string Id)
[HttpGet("regions/{id}")]
public async Task<ActionResult> GetRegionAsync(int id)
{
var result = await this.regionProvider.GetRegionByIdAsync(Id);
var result = await this.regionProvider.GetRegionByIdAsync(id);
if (result.IsSuccess)
{
return Ok(result.Region);
@ -45,7 +44,7 @@ namespace DamageAssesment.Api.Locations.Controllers
/// POST request for creating a new region.
/// </summary>
[HttpPost]
[HttpPost("regions")]
public async Task<ActionResult> PostRegionAsync(Models.Region region)
{
var result = await this.regionProvider.PostRegionAsync(region);
@ -59,10 +58,10 @@ namespace DamageAssesment.Api.Locations.Controllers
/// PUT request for updating an existing region.
/// </summary>
[HttpPut]
public async Task<ActionResult> PutRegionAsync(Models.Region region)
[HttpPut("regions/{id}")]
public async Task<ActionResult> PutRegionAsync(int id, Models.Region region)
{
var result = await this.regionProvider.PutRegionAsync(region);
var result = await this.regionProvider.PutRegionAsync(id,region);
if (result.IsSuccess)
{
return Ok(result.Region);
@ -77,10 +76,10 @@ namespace DamageAssesment.Api.Locations.Controllers
/// </summary>
[HttpDelete("{Id}")]
public async Task<ActionResult> DeleteRegionAsync(string Id)
[HttpDelete("regions/{id}")]
public async Task<ActionResult> DeleteRegionAsync(int id)
{
var result = await this.regionProvider.DeleteRegionAsync(Id);
var result = await this.regionProvider.DeleteRegionAsync(id);
if (result.IsSuccess)
{
return Ok(result.Region);

View File

@ -6,18 +6,20 @@ namespace DamageAssesment.Api.Locations.Db
public class Location
{
[Key]
public int Id { get; set; }
[ForeignKey("Region")]
public int RegionId { get; set; }
[StringLength(4)]
public string Id { get; set; }
public string LocationCode { get; set; }
[StringLength(50)]
public string Name { get; set; }
[StringLength(1)]
[StringLength(4)]
public string MaintenanceCenter { get; set; }
[StringLength(2)]
public string SchoolType { get; set; }
[ForeignKey("Region")]
public string RegionId { get; set; }
}
}

View File

@ -2,7 +2,7 @@
namespace DamageAssesment.Api.Locations.Db
{
public class LocationDbContext:DbContext
public class LocationDbContext : DbContext
{
public DbSet<Db.Location> Locations { get; set; }
public DbSet<Db.Region> Regions { get; set; }
@ -10,5 +10,17 @@ namespace DamageAssesment.Api.Locations.Db
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Location>()
.Property(item => item.Id)
.ValueGeneratedOnAdd();
modelBuilder.Entity<Region>()
.Property(item => item.Id)
.ValueGeneratedOnAdd();
}
}
}

View File

@ -5,15 +5,12 @@ namespace DamageAssesment.Api.Locations.Db
public class Region
{
[Key]
[StringLength(2)]
public string Id { get; set; }
public int Id { get; set; }
[StringLength(50)]
public string Name { get; set; }
[StringLength(5)]
public string Abbreviation { get; set; }
// public ICollection<Location> Locations { get; set; }
}
}

View File

@ -4,11 +4,11 @@ namespace DamageAssesment.Api.Locations.Interfaces
{
public interface ILocationsProvider
{
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 Question, string ErrorMessage)> PostLocationAsync(Models.Location Location);
Task<(bool IsSuccess, string ErrorMessage)> UpdateLocationAsync(Models.Location Location);
Task<(bool IsSuccess, string ErrorMessage)> DeleteLocationAsync(string Id);
Task<(bool IsSuccess, IEnumerable<Models.Location> Locations, string ErrorMessage)> GetLocationsAsync();
Task<(bool IsSuccess, Models.Location Location, string ErrorMessage)> GetLocationByIdAsync(int Id);
Task<(bool IsSuccess, Models.Location Location, string ErrorMessage)> PostLocationAsync(Models.Location Location);
Task<(bool IsSuccess, Models.Location Location, string ErrorMessage)> UpdateLocationAsync(int Id, Models.Location Location);
Task<(bool IsSuccess, Models.Location Location, string ErrorMessage)> DeleteLocationAsync(int Id);
void SeedData();
}
}

View File

@ -2,11 +2,11 @@
{
public interface IRegionsProvider
{
Task<(bool IsSuccess, IEnumerable<Models.Region> regions, string ErrorMessage)> GetRegionsAsync();
Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> GetRegionByIdAsync(string Id);
Task<(bool IsSuccess, IEnumerable<Models.Region> Regions, string ErrorMessage)> GetRegionsAsync();
Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> GetRegionByIdAsync(int Id);
Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> PostRegionAsync(Models.Region region);
Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> PutRegionAsync(Models.Region region);
Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> DeleteRegionAsync(string Id);
Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> PutRegionAsync(int Id, Models.Region region);
Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> DeleteRegionAsync(int Id);
void SeedData();
}
}

View File

@ -4,19 +4,12 @@ namespace DamageAssesment.Api.Locations.Models
{
public class Location
{
[StringLength(4)]
public string Id { get; set; }
[StringLength(1)]
public string RegionId { get; set; }
[StringLength(50)]
[Key]
public int Id { get; set; }
public int RegionId { get; set; }
public string LocationCode { get; set; }
public string Name { get; set; }
[StringLength(1)]
public string MaintenanceCenter { get; set; }
[StringLength(2)]
public string SchoolType { get; set; }
}
}

View File

@ -4,14 +4,8 @@ namespace DamageAssesment.Api.Locations.Models
{
public class Region
{
[StringLength(1)]
public string Id { get; set; }
[StringLength(50)]
public int Id { get; set; }
public string Name { get; set; }
[StringLength(5)]
public string Abbreviation { get; set; }
}
}

View File

@ -5,6 +5,7 @@
public LocationProfile()
{
CreateMap<Db.Location, Models.Location>();
CreateMap<Models.Location, Db.Location>();
}
}
}

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();
}
}