91 lines
2.7 KiB
C#
91 lines
2.7 KiB
C#
using DamageAssesment.Api.Locations.Interfaces;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace DamageAssesment.Api.Locations.Controllers
|
|
{
|
|
[ApiController]
|
|
public class RegionsController : ControllerBase
|
|
{
|
|
private readonly IRegionsProvider regionProvider;
|
|
|
|
public RegionsController(IRegionsProvider regionProvider)
|
|
{
|
|
this.regionProvider = regionProvider;
|
|
}
|
|
/// <summary>
|
|
/// Get all regions.2
|
|
/// </summary>
|
|
|
|
[HttpGet("regions")]
|
|
public async Task<ActionResult> GetRegionsAsync()
|
|
{
|
|
var result = await regionProvider.GetRegionsAsync();
|
|
if (result.IsSuccess)
|
|
{
|
|
return Ok(result.Regions);
|
|
}
|
|
return NoContent();
|
|
}
|
|
/// <summary>
|
|
/// GET request for retrieving a region by its ID.
|
|
/// </summary>
|
|
|
|
[HttpGet("regions/{id}")]
|
|
public async Task<ActionResult> GetRegionAsync(int id)
|
|
{
|
|
var result = await this.regionProvider.GetRegionByIdAsync(id);
|
|
if (result.IsSuccess)
|
|
{
|
|
return Ok(result.Region);
|
|
}
|
|
return NotFound();
|
|
}
|
|
/// <summary>
|
|
/// POST request for creating a new region.
|
|
/// </summary>
|
|
|
|
[HttpPost("regions")]
|
|
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);
|
|
}
|
|
/// <summary>
|
|
/// PUT request for updating an existing region.
|
|
/// </summary>
|
|
|
|
[HttpPut("regions/{id}")]
|
|
public async Task<ActionResult> PutRegionAsync(int id, Models.Region region)
|
|
{
|
|
var result = await this.regionProvider.PutRegionAsync(id,region);
|
|
if (result.IsSuccess)
|
|
{
|
|
return Ok(result.Region);
|
|
}
|
|
if (result.ErrorMessage.Equals("Not Found"))
|
|
return NotFound(result.ErrorMessage);
|
|
|
|
return BadRequest(result.ErrorMessage);
|
|
}
|
|
/// <summary>
|
|
/// DELETE request for deleting a region based on ID.
|
|
/// </summary>
|
|
|
|
|
|
[HttpDelete("regions/{id}")]
|
|
public async Task<ActionResult> DeleteRegionAsync(int id)
|
|
{
|
|
var result = await this.regionProvider.DeleteRegionAsync(id);
|
|
if (result.IsSuccess)
|
|
{
|
|
return Ok(result.Region);
|
|
}
|
|
return NotFound();
|
|
}
|
|
}
|
|
}
|