DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Locations/Controllers/RegionsController.cs

92 lines
2.6 KiB
C#
Raw Normal View History

2023-08-15 22:52:30 -05:00
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;
}
/// <summary>
/// Get all regions.2
/// </summary>
2023-08-15 22:52:30 -05:00
[HttpGet]
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>
2023-08-15 22:52:30 -05:00
[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();
}
/// <summary>
/// POST request for creating a new region.
/// </summary>
2023-08-15 22:52:30 -05:00
[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);
}
/// <summary>
/// PUT request for updating an existing region.
/// </summary>
2023-08-15 22:52:30 -05:00
[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);
}
/// <summary>
/// DELETE request for deleting a region based on ID.
/// </summary>
2023-08-15 22:52:30 -05:00
[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();
}
}
}