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-09-04 20:31:41 -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-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-09-13 00:28:24 -05:00
var location = await locationDbContext . Locations . AsNoTracking ( ) . FirstOrDefaultAsync ( q = > q . Id = = Id ) ;
if ( location ! = null )
2023-08-15 22:52:30 -05:00
{
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-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-09-13 00:28:24 -05:00
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" } ) ;
2023-08-15 22:52:30 -05:00
locationDbContext . SaveChanges ( ) ;
}
}
}
}