DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Locations/Providers/LocationsProvider.cs
2023-09-13 13:16:42 -04:00

157 lines
6.7 KiB
C#

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;
//SeedData();
}
public async Task<(bool IsSuccess, IEnumerable<Models.Location> Locations, string ErrorMessage)> GetLocationsAsync()
{
try
{
logger?.LogInformation("Query Location");
var locations = await locationDbContext.Locations.AsNoTracking().ToListAsync();
if (locations != null)
{
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");
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
public async Task<(bool IsSuccess, Models.Location Location, string ErrorMessage)> GetLocationByIdAsync(int Id)
{
try
{
logger?.LogInformation("Query Location");
var location = await locationDbContext.Locations.AsNoTracking().FirstOrDefaultAsync(q => q.Id == Id);
if (location != null)
{
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);
await locationDbContext.SaveChangesAsync();
location.Id = _location.Id;
return (true, location, null);
}
else
{
return (false, null, "Location code is already exists");
}
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
public async Task<(bool IsSuccess, Models.Location Location, string ErrorMessage)> UpdateLocationAsync(int Id, Models.Location location)
{
try
{
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");
}
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
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(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() { LocationCode = "Loc1", RegionId = 1, Name = "BOB GRAHAM EDUCATION CENTER 1", MaintenanceCenter = "1", SchoolType = "US" });
locationDbContext.Locations.Add(new Db.Location() { LocationCode = "Loc2", RegionId = 2, Name = "BOB GRAHAM EDUCATION CENTER 2", MaintenanceCenter = "1", SchoolType = "US" });
locationDbContext.Locations.Add(new Db.Location() { LocationCode = "Loc3", RegionId = 3, Name = "BOB GRAHAM EDUCATION CENTER 3", MaintenanceCenter = "1", SchoolType = "US" });
locationDbContext.Locations.Add(new Db.Location() { LocationCode = "Loc4", RegionId = 1, Name = "BOB GRAHAM EDUCATION CENTER 4", MaintenanceCenter = "1", SchoolType = "US" });
locationDbContext.Locations.Add(new Db.Location() { LocationCode = "Loc5", RegionId = 2, Name = "BOB GRAHAM EDUCATION CENTER 5", MaintenanceCenter = "1", SchoolType = "US" });
locationDbContext.Locations.Add(new Db.Location() { LocationCode = "Loc6", RegionId = 3, Name = "BOB GRAHAM EDUCATION CENTER 6", MaintenanceCenter = "1", SchoolType = "US" });
locationDbContext.SaveChanges();
}
}
}
}