2023-08-15 22:52:30 -05:00
using AutoMapper ;
using DamageAssesment.Api.Locations.Db ;
using DamageAssesment.Api.Locations.Interfaces ;
using DamageAssesment.Api.Locations.Models ;
using Microsoft.EntityFrameworkCore ;
namespace DamageAssesment.Api.Locations.Providers
{
public class LocationsProvider : ILocationsProvider
{
private LocationDbContext locationDbContext ;
private ILogger < LocationsProvider > logger ;
private IMapper mapper ;
public LocationsProvider ( LocationDbContext locationDbContext , ILogger < LocationsProvider > logger , IMapper mapper )
{
this . locationDbContext = locationDbContext ;
this . logger = logger ;
this . mapper = mapper ;
2023-11-07 16:51:10 -05:00
// SeedData();
2023-08-15 22:52:30 -05:00
}
2023-09-13 00:28:24 -05:00
public async Task < ( bool IsSuccess , IEnumerable < Models . Location > Locations , string ErrorMessage ) > GetLocationsAsync ( )
2023-08-15 22:52:30 -05:00
{
try
{
2023-09-13 00:28:24 -05:00
logger ? . LogInformation ( "Query Location" ) ;
var locations = await locationDbContext . Locations . AsNoTracking ( ) . ToListAsync ( ) ;
if ( locations ! = null )
2023-08-15 22:52:30 -05:00
{
2023-11-07 16:51:10 -05:00
int maxenrollment = ( int ) locations . Max ( a = > a . Enrollment ) ;
foreach ( Db . Location item in locations ) { item . DataValue = GetHeatmapdata ( item . Enrollment , maxenrollment ) ; }
2023-09-13 00:28:24 -05:00
logger ? . LogInformation ( $"{locations.Count} Locations(s) found" ) ;
var result = mapper . Map < IEnumerable < Db . Location > , IEnumerable < Models . Location > > ( locations ) ;
2023-08-15 22:52:30 -05:00
return ( true , result , null ) ;
}
return ( false , null , "Not found" ) ;
}
catch ( Exception ex )
{
logger ? . LogError ( ex . ToString ( ) ) ;
return ( false , null , ex . Message ) ;
}
}
2023-09-13 00:28:24 -05:00
public async Task < ( bool IsSuccess , Models . Location Location , string ErrorMessage ) > GetLocationByIdAsync ( int Id )
2023-08-15 22:52:30 -05:00
{
try
{
logger ? . LogInformation ( "Query Location" ) ;
2023-11-07 16:51:10 -05:00
int maxenrollment = ( int ) await locationDbContext . Locations . AsNoTracking ( ) . MaxAsync ( a = > a . Enrollment ) ;
2023-09-13 00:28:24 -05:00
var location = await locationDbContext . Locations . AsNoTracking ( ) . FirstOrDefaultAsync ( q = > q . Id = = Id ) ;
if ( location ! = null )
2023-11-07 16:51:10 -05:00
{ if ( maxenrollment > 0 )
location . DataValue = GetHeatmapdata ( location . Enrollment , maxenrollment ) ;
2023-09-13 00:28:24 -05:00
logger ? . LogInformation ( $"{location} found" ) ;
var result = mapper . Map < Db . Location , Models . Location > ( location ) ;
2023-08-15 22:52:30 -05:00
return ( true , result , null ) ;
}
return ( false , null , "Not found" ) ;
}
catch ( Exception ex )
{
logger ? . LogError ( ex . ToString ( ) ) ;
return ( false , null , ex . Message ) ;
}
}
2023-11-07 16:51:10 -05:00
private int GetHeatmapdata ( int? enrollment , int maxenrollment )
{
return ( enrollment ! = null ? enrollment . Value : 0 ) * 100 / maxenrollment ;
}
2023-09-13 00:28:24 -05:00
public async Task < ( bool IsSuccess , Models . Location Location , string ErrorMessage ) > PostLocationAsync ( Models . Location location )
2023-08-15 22:52:30 -05:00
{
try
{
2023-09-13 00:28:24 -05:00
if ( ! LocationCodeExists ( location . LocationCode ) )
2023-08-15 22:52:30 -05:00
{
2023-09-13 00:28:24 -05:00
Db . Location _location = mapper . Map < Models . Location , Db . Location > ( location ) ;
2023-08-25 17:44:04 -05:00
locationDbContext . Locations . Add ( _location ) ;
2023-09-13 00:28:24 -05:00
await locationDbContext . SaveChangesAsync ( ) ;
location . Id = _location . Id ;
return ( true , location , null ) ;
2023-08-15 22:52:30 -05:00
}
else
{
2023-09-13 00:28:24 -05:00
return ( false , null , "Location code is already exists" ) ;
2023-08-15 22:52:30 -05:00
}
}
catch ( Exception ex )
{
logger ? . LogError ( ex . ToString ( ) ) ;
return ( false , null , ex . Message ) ;
}
}
2023-09-13 00:28:24 -05:00
public async Task < ( bool IsSuccess , Models . Location Location , string ErrorMessage ) > UpdateLocationAsync ( int Id , Models . Location location )
2023-08-15 22:52:30 -05:00
{
try
{
2023-09-13 00:28:24 -05:00
if ( LocationExists ( Id ) )
{
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" ) ;
}
2023-08-15 22:52:30 -05:00
}
catch ( Exception ex )
{
logger ? . LogError ( ex . ToString ( ) ) ;
2023-09-13 00:28:24 -05:00
return ( false , null , ex . Message ) ;
2023-08-15 22:52:30 -05:00
}
}
2023-09-13 00:28:24 -05:00
public async Task < ( bool IsSuccess , Models . Location Location , string ErrorMessage ) > DeleteLocationAsync ( int Id )
2023-08-15 22:52:30 -05:00
{
try
{
2023-09-13 00:28:24 -05:00
Db . Location location = locationDbContext . Locations . AsNoTracking ( ) . Where ( a = > a . Id = = Id ) . FirstOrDefault ( ) ;
if ( location = = null )
2023-08-15 22:52:30 -05:00
{
2023-09-13 00:28:24 -05:00
return ( false , null , "record not found" ) ;
2023-08-15 22:52:30 -05:00
}
2023-09-13 00:28:24 -05:00
locationDbContext . Locations . Remove ( location ) ;
await locationDbContext . SaveChangesAsync ( ) ;
return ( true , mapper . Map < Db . Location , Models . Location > ( location ) , "Record deleted successfully" ) ;
2023-08-15 22:52:30 -05:00
}
catch ( Exception ex )
{
logger ? . LogError ( ex . ToString ( ) ) ;
2023-09-13 00:28:24 -05:00
return ( false , null , ex . Message ) ;
2023-08-15 22:52:30 -05:00
}
}
2023-09-13 00:28:24 -05:00
private bool LocationExists ( int id )
2023-08-15 22:52:30 -05:00
{
return locationDbContext . Locations . AsNoTracking ( ) . Count ( e = > e . Id = = id ) > 0 ;
}
2023-09-13 00:28:24 -05:00
private bool LocationCodeExists ( string locationCode )
{
return locationDbContext . Locations . AsNoTracking ( ) . Count ( e = > e . LocationCode . ToLower ( ) = = locationCode . ToLower ( ) ) > 0 ;
}
2023-09-04 20:31:41 -05:00
public void SeedData ( )
2023-08-15 22:52:30 -05:00
{
if ( ! locationDbContext . Locations . Any ( ) )
{
2023-11-07 16:51:10 -05:00
locationDbContext . Locations . Add ( new Db . Location ( ) { LocationCode = "0091" , RegionId = 5 , Name = "BOB GRAHAM EDUCATION CENTER" , MaintenanceCenter = "1" , SchoolType = "K8" , DataValue = 25 , Enrollment = 780 , Latitude = 25.83604 , Longitute = - 80.21618 } ) ;
locationDbContext . Locations . Add ( new Db . Location ( ) { LocationCode = "0092" , RegionId = 1 , Name = "NORMAN S. EDELCUP/SUNNY ISLES BEACH K-8" , MaintenanceCenter = "1" , SchoolType = "K8" , DataValue = 46 , Enrollment = 650 , Latitude = 20.83604 , Longitute = - 40.21618 } ) ;
locationDbContext . Locations . Add ( new Db . Location ( ) { LocationCode = "7511" , RegionId = 4 , Name = "MIAMI SPRINGS SHS" , MaintenanceCenter = "2" , SchoolType = "S" , DataValue = 12 , Enrollment = 500 , Latitude = 53.83604 , Longitute = - 60.21618 } ) ;
2023-10-04 14:03:59 -05:00
//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" });
2023-08-15 22:52:30 -05:00
locationDbContext . SaveChanges ( ) ;
}
}
}
}