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 logger; private IMapper mapper; public LocationsProvider(LocationDbContext locationDbContext, ILogger logger, IMapper mapper) { this.locationDbContext = locationDbContext; this.logger = logger; this.mapper = mapper; SeedData(); } public async Task<(bool IsSuccess, IEnumerable 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>(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(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(Models.Location Location) { try { logger?.LogInformation("Query Location"); if (!LocationExists(Location.Id)) { Db.Location _location = mapper.Map(Location); locationDbContext.Locations.Add(_location); locationDbContext.SaveChanges(); return (true, Location, 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(Models.Location Location) { try { Db.Location _location = mapper.Map(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) { 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(); } } } }