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.2
///
[HttpGet]
public async Task GetRegionsAsync()
{
var result = await regionProvider.GetRegionsAsync();
if (result.IsSuccess)
{
return Ok(result.regions);
}
return NoContent();
}
///
/// GET request for retrieving a region by its ID.
///
[HttpGet("{Id}")]
public async Task GetRegionAsync(string Id)
{
var result = await this.regionProvider.GetRegionByIdAsync(Id);
if (result.IsSuccess)
{
return Ok(result.Region);
}
return NotFound();
}
///
/// POST request for creating a new region.
///
[HttpPost]
public async Task PostRegionAsync(Models.Region region)
{
var result = await this.regionProvider.PostRegionAsync(region);
if (result.IsSuccess)
{
return Ok(result.Region);
}
return BadRequest(result.ErrorMessage);
}
///
/// PUT request for updating an existing region.
///
[HttpPut]
public async Task 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);
}
///
/// DELETE request for deleting a region based on ID.
///
[HttpDelete("{Id}")]
public async Task DeleteRegionAsync(string Id)
{
var result = await this.regionProvider.DeleteRegionAsync(Id);
if (result.IsSuccess)
{
return Ok(result.Region);
}
return NotFound();
}
}
}