DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Locations/Providers/LocationsProvider.cs

148 lines
5.9 KiB
C#
Raw Normal View History

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;
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)
{
try
{
logger?.LogInformation("Query Location");
var Location = await locationDbContext.Locations.AsNoTracking().FirstOrDefaultAsync(q => q.Id == Id);
if (Location != null)
{
logger?.LogInformation($"{Location} customer(s) 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 Question, string ErrorMessage)> PostLocationAsync(Db.Location Location)
{
try
{
logger?.LogInformation("Query Location");
if (!LocationExists(Location.Id))
{
locationDbContext.Locations.Add(Location);
locationDbContext.SaveChanges();
var result = mapper.Map<Db.Location, Models.Location>(Location);
return (true, result, null);
}
else
{
return (false, null, "Location is Already exists");
}
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, null, ex.Message);
}
}
public async Task<(bool IsSuccess, string ErrorMessage)> UpdateLocationAsync(Db.Location Location)
{
try
{
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)
{
return (false, "record not found");
}
locationDbContext.Locations.Remove(Location);
locationDbContext.SaveChanges();
return (true, "Record deleted successfully");
}
catch (Exception ex)
{
logger?.LogError(ex.ToString());
return (false, ex.Message);
}
}
private bool LocationExists(string id)
{
return locationDbContext.Locations.AsNoTracking().Count(e => e.Id == id) > 0;
}
private 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.SaveChanges();
}
}
}
}