forked from MDCPS/DamageAssessment_Backend
Copy from old Repository
This commit is contained in:
@ -0,0 +1,85 @@
|
||||
using DamageAssesment.Api.Locations.Interfaces;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace DamageAssesment.Api.Locations.Controllers
|
||||
{
|
||||
[Route("api")]
|
||||
[ApiController]
|
||||
public class LocationsController : ControllerBase
|
||||
{
|
||||
private ILocationsProvider LocationProvider;
|
||||
|
||||
public LocationsController(ILocationsProvider LocationsProvider)
|
||||
{
|
||||
this.LocationProvider = LocationsProvider;
|
||||
}
|
||||
// Get all Locations
|
||||
[HttpGet("Locations")]
|
||||
public async Task<ActionResult> GetLocationsAsync()
|
||||
{
|
||||
|
||||
var result = await LocationProvider.GetLocationsAsync();
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.locations);
|
||||
}
|
||||
return NotFound();
|
||||
|
||||
}
|
||||
// Get all Location based on Id
|
||||
[HttpGet("Locations/{id}")]
|
||||
public async Task<ActionResult> GetLocationByIdAsync(string id)
|
||||
{
|
||||
|
||||
var result = await LocationProvider.GetLocationByIdAsync(id);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.Location);
|
||||
}
|
||||
return NotFound();
|
||||
|
||||
}
|
||||
// Update Location entity
|
||||
[HttpPut("Locations")]
|
||||
public async Task<IActionResult> UpdateLocation(Db.Location Location)
|
||||
{
|
||||
if (Location != null)
|
||||
{
|
||||
var result = await this.LocationProvider.UpdateLocationAsync(Location);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.ErrorMessage);
|
||||
}
|
||||
return NotFound();
|
||||
}
|
||||
return NotFound();
|
||||
}
|
||||
//save new location
|
||||
[HttpPost("Locations")]
|
||||
public async Task<IActionResult> CreateLocation(Db.Location Location)
|
||||
{
|
||||
if (Location != null)
|
||||
{
|
||||
var result = await this.LocationProvider.PostLocationAsync(Location);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.Question);
|
||||
}
|
||||
return NotFound();
|
||||
}
|
||||
return CreatedAtRoute("DefaultApi", new { id = Location.Id }, Location);
|
||||
}
|
||||
//delete existing location
|
||||
[HttpDelete("Locations/{id}")]
|
||||
public async Task<IActionResult> DeleteLocation(string id)
|
||||
{
|
||||
var result = await this.LocationProvider.DeleteLocationAsync(id);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.ErrorMessage);
|
||||
}
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
using DamageAssesment.Api.Locations.Interfaces;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace DamageAssesment.Api.Locations.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class RegionsController : ControllerBase
|
||||
{
|
||||
private readonly IRegionsProvider regionProvider;
|
||||
|
||||
public RegionsController(IRegionsProvider regionProvider)
|
||||
{
|
||||
this.regionProvider = regionProvider;
|
||||
}
|
||||
|
||||
// Get all Regions
|
||||
[HttpGet]
|
||||
public async Task<ActionResult> GetRegionsAsync()
|
||||
{
|
||||
var result = await regionProvider.GetRegionsAsync();
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.regions);
|
||||
}
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpGet("{Id}")]
|
||||
public async Task<ActionResult> GetRegionAsync(string Id)
|
||||
{
|
||||
var result = await this.regionProvider.GetRegionByIdAsync(Id);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.Region);
|
||||
}
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ActionResult> PostRegionAsync(Models.Region region)
|
||||
{
|
||||
var result = await this.regionProvider.PostRegionAsync(region);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.Region);
|
||||
}
|
||||
return BadRequest(result.ErrorMessage);
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
public async Task<ActionResult> PutRegionAsync(Models.Region region)
|
||||
{
|
||||
var result = await this.regionProvider.PutRegionAsync(region);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.Region);
|
||||
}
|
||||
if (result.ErrorMessage.Equals("Not Found"))
|
||||
return NotFound(result.ErrorMessage);
|
||||
|
||||
return BadRequest(result.ErrorMessage);
|
||||
}
|
||||
|
||||
[HttpDelete("{Id}")]
|
||||
public async Task<ActionResult> DeleteRegionAsync(string Id)
|
||||
{
|
||||
var result = await this.regionProvider.DeleteRegionAsync(Id);
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
return Ok(result.Region);
|
||||
}
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.5" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -0,0 +1,23 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace DamageAssesment.Api.Locations.Db
|
||||
{
|
||||
public class Location
|
||||
{
|
||||
[Key]
|
||||
[StringLength(4)]
|
||||
public string Id { get; set; }
|
||||
|
||||
[StringLength(50)]
|
||||
public string Name { get; set; }
|
||||
|
||||
[StringLength(1)]
|
||||
public string MaintenanceCenter { get; set; }
|
||||
|
||||
[StringLength(2)]
|
||||
public string SchoolType { get; set; }
|
||||
[ForeignKey("Region")]
|
||||
public string RegionId { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DamageAssesment.Api.Locations.Db
|
||||
{
|
||||
public class LocationDbContext:DbContext
|
||||
{
|
||||
public DbSet<Db.Location> Locations { get; set; }
|
||||
public DbSet<Db.Region> Regions { get; set; }
|
||||
public LocationDbContext(DbContextOptions options) : base(options)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DamageAssesment.Api.Locations.Db
|
||||
{
|
||||
public class Region
|
||||
{
|
||||
[Key]
|
||||
[StringLength(2)]
|
||||
public string Id { get; set; }
|
||||
|
||||
[StringLength(50)]
|
||||
public string Name { get; set; }
|
||||
|
||||
[StringLength(5)]
|
||||
public string Abbreviation { get; set; }
|
||||
|
||||
// public ICollection<Location> Locations { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
using DamageAssesment.Api.Locations.Db;
|
||||
|
||||
namespace DamageAssesment.Api.Locations.Interfaces
|
||||
{
|
||||
public interface ILocationsProvider
|
||||
{
|
||||
Task<(bool IsSuccess, IEnumerable<Models.Location> locations, string ErrorMessage)> GetLocationsAsync();
|
||||
Task<(bool IsSuccess, Models.Location Location, string ErrorMessage)> GetLocationByIdAsync(string Id);
|
||||
Task<(bool IsSuccess, Models.Location Question, string ErrorMessage)> PostLocationAsync(Db.Location Location);
|
||||
Task<(bool IsSuccess, string ErrorMessage)> UpdateLocationAsync(Db.Location Location);
|
||||
Task<(bool IsSuccess, string ErrorMessage)> DeleteLocationAsync(string Id);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
namespace DamageAssesment.Api.Locations.Interfaces
|
||||
{
|
||||
public interface IRegionsProvider
|
||||
{
|
||||
Task<(bool IsSuccess, IEnumerable<Models.Region> regions, string ErrorMessage)> GetRegionsAsync();
|
||||
Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> GetRegionByIdAsync(string Id);
|
||||
Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> PostRegionAsync(Models.Region region);
|
||||
Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> PutRegionAsync(Models.Region region);
|
||||
Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> DeleteRegionAsync(string Id);
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DamageAssesment.Api.Locations.Models
|
||||
{
|
||||
public class Location
|
||||
{
|
||||
[StringLength(4)]
|
||||
public string Id { get; set; }
|
||||
|
||||
[StringLength(1)]
|
||||
public string RegionId { get; set; }
|
||||
|
||||
[StringLength(50)]
|
||||
public string Name { get; set; }
|
||||
|
||||
[StringLength(1)]
|
||||
public string MaintenanceCenter { get; set; }
|
||||
|
||||
[StringLength(2)]
|
||||
public string SchoolType { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DamageAssesment.Api.Locations.Models
|
||||
{
|
||||
public class Region
|
||||
{
|
||||
|
||||
[StringLength(1)]
|
||||
public string Id { get; set; }
|
||||
|
||||
[StringLength(50)]
|
||||
public string Name { get; set; }
|
||||
|
||||
[StringLength(5)]
|
||||
public string Abbreviation { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
namespace DamageAssesment.Api.Locations.Profiles
|
||||
{
|
||||
public class LocationProfile : AutoMapper.Profile
|
||||
{
|
||||
public LocationProfile()
|
||||
{
|
||||
CreateMap<Db.Location, Models.Location>();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
namespace DamageAssesment.Api.Locations.Profiles
|
||||
{
|
||||
public class RegionProfile : AutoMapper.Profile
|
||||
{
|
||||
public RegionProfile()
|
||||
{
|
||||
CreateMap<Db.Region, Models.Region>();
|
||||
CreateMap<Models.Region, Db.Region>();
|
||||
}
|
||||
}
|
||||
}
|
35
DamageAssesmentApi/DamageAssesment.Api.Locations/Program.cs
Normal file
35
DamageAssesmentApi/DamageAssesment.Api.Locations/Program.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using DamageAssesment.Api.Locations.Db;
|
||||
using DamageAssesment.Api.Locations.Interfaces;
|
||||
using DamageAssesment.Api.Locations.Providers;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.AddScoped<ILocationsProvider, LocationsProvider>();
|
||||
builder.Services.AddScoped<IRegionsProvider, RegionsProvider>();
|
||||
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); //4/30
|
||||
builder.Services.AddDbContext<LocationDbContext>(option =>
|
||||
{
|
||||
option.UseInMemoryDatabase("Locations");
|
||||
});
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
@ -0,0 +1,31 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:20458",
|
||||
"sslPort": 0
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"DamageAssesment.Api.Locations": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5213",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,147 @@
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,164 @@
|
||||
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 RegionsProvider : IRegionsProvider
|
||||
{
|
||||
private LocationDbContext locationDbContext;
|
||||
private ILogger<RegionsProvider> logger;
|
||||
private IMapper mapper;
|
||||
|
||||
public RegionsProvider(LocationDbContext regionDbContext, ILogger<RegionsProvider> logger, IMapper mapper)
|
||||
{
|
||||
this.locationDbContext = regionDbContext;
|
||||
this.logger = logger;
|
||||
this.mapper = mapper;
|
||||
SeedData();
|
||||
}
|
||||
|
||||
public async Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> GetRegionByIdAsync(string Id)
|
||||
{
|
||||
try
|
||||
{
|
||||
logger?.LogInformation("Get Regions from DB");
|
||||
var region = await locationDbContext.Regions.AsNoTracking().FirstOrDefaultAsync(s => s.Id == Id);
|
||||
|
||||
if (region != null)
|
||||
{
|
||||
logger?.LogInformation($"RegionId: {region.Id} Items found");
|
||||
var result = mapper.Map<Db.Region, Models.Region>(region);
|
||||
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, IEnumerable<Models.Region> regions, string ErrorMessage)> GetRegionsAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
logger?.LogInformation("Get all Regions from DB");
|
||||
var regions = await locationDbContext.Regions.AsNoTracking().ToListAsync();
|
||||
|
||||
if (regions != null)
|
||||
{
|
||||
logger?.LogInformation($"{regions.Count} Items(s) found");
|
||||
var result = mapper.Map<IEnumerable<Db.Region>, IEnumerable<Models.Region>>(regions);
|
||||
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.Region Region, string ErrorMessage)> PostRegionAsync(Models.Region region)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (region != null)
|
||||
{
|
||||
var regions = await locationDbContext.Regions.AsNoTracking().ToListAsync();
|
||||
|
||||
region.Id = Convert.ToString(regions.Count + 1);
|
||||
locationDbContext.Regions.Add(mapper.Map<Models.Region, Db.Region>(region));
|
||||
locationDbContext.SaveChanges();
|
||||
logger?.LogInformation($"{region} added successfuly");
|
||||
return (true, region, "Successful");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger?.LogInformation($"{region} cannot be added");
|
||||
return (false, null, "Region cannot be added");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> PutRegionAsync(Models.Region region)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (region != null)
|
||||
{
|
||||
var _region = await locationDbContext.Regions.AsNoTracking().Where(s => s.Id == region.Id).FirstOrDefaultAsync();
|
||||
|
||||
if (_region != null)
|
||||
{
|
||||
locationDbContext.Regions.Update(mapper.Map<Models.Region, Db.Region>(region));
|
||||
locationDbContext.SaveChanges();
|
||||
return (true, region, "Region updated Successfuly");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger?.LogInformation($"RegionID: {region.Id} Not found");
|
||||
return (false, null, "Not Found");
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
logger?.LogInformation($"RegionID: {region.Id} Bad Request");
|
||||
return (false, null, "Bad request");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<(bool IsSuccess, Models.Region Region, string ErrorMessage)> DeleteRegionAsync(string Id)
|
||||
{
|
||||
try
|
||||
{
|
||||
var region = await locationDbContext.Regions.AsNoTracking().Where(x => x.Id == Id).FirstOrDefaultAsync();
|
||||
|
||||
if (region != null)
|
||||
{
|
||||
locationDbContext.Regions.Remove(region);
|
||||
locationDbContext.SaveChanges();
|
||||
return (true, mapper.Map<Db.Region, Models.Region>(region), $"RegionId {Id} deleted Successfuly");
|
||||
}
|
||||
else
|
||||
{
|
||||
logger?.LogInformation($"RegionID: {Id} Not found");
|
||||
return (false, null, "Not Found");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger?.LogError(ex.ToString());
|
||||
return (false, null, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private void SeedData()
|
||||
{
|
||||
if (!locationDbContext.Regions.Any())
|
||||
{
|
||||
locationDbContext.Regions.Add(new Db.Region() { Id = "1", Name = "North", Abbreviation = "N" });
|
||||
locationDbContext.Regions.Add(new Db.Region() { Id = "2", Name = "South", Abbreviation = "S" });
|
||||
locationDbContext.Regions.Add(new Db.Region() { Id = "3", Name = "Central", Abbreviation = "C" });
|
||||
locationDbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
Reference in New Issue
Block a user