2023-08-15 22:52:30 -05:00
|
|
|
|
using DamageAssesment.Api.Locations.Interfaces;
|
2023-10-19 14:59:02 -05:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-08-15 22:52:30 -05:00
|
|
|
|
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;
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
2023-09-04 20:31:41 -05:00
|
|
|
|
/// Get all regions.2
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// </summary>
|
2023-10-19 14:59:02 -05:00
|
|
|
|
[Authorize(Roles = "admin")]
|
2023-09-13 00:28:24 -05:00
|
|
|
|
[HttpGet("regions")]
|
2023-08-15 22:52:30 -05:00
|
|
|
|
public async Task<ActionResult> GetRegionsAsync()
|
|
|
|
|
{
|
|
|
|
|
var result = await regionProvider.GetRegionsAsync();
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
return Ok(result.Regions);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// GET request for retrieving a region by its ID.
|
|
|
|
|
/// </summary>
|
2023-10-19 14:59:02 -05:00
|
|
|
|
[Authorize(Roles = "admin")]
|
2023-09-13 00:28:24 -05:00
|
|
|
|
[HttpGet("regions/{id}")]
|
|
|
|
|
public async Task<ActionResult> GetRegionAsync(int id)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
var result = await this.regionProvider.GetRegionByIdAsync(id);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Region);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// POST request for creating a new region.
|
|
|
|
|
/// </summary>
|
2023-10-19 14:59:02 -05:00
|
|
|
|
[Authorize(Roles = "admin")]
|
2023-09-13 00:28:24 -05:00
|
|
|
|
[HttpPost("regions")]
|
2023-08-15 22:52:30 -05:00
|
|
|
|
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);
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// PUT request for updating an existing region.
|
|
|
|
|
/// </summary>
|
2023-10-19 14:59:02 -05:00
|
|
|
|
[Authorize(Roles = "admin")]
|
2023-09-13 00:28:24 -05:00
|
|
|
|
[HttpPut("regions/{id}")]
|
|
|
|
|
public async Task<ActionResult> PutRegionAsync(int id, Models.Region region)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
var result = await this.regionProvider.PutRegionAsync(id,region);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Region);
|
|
|
|
|
}
|
|
|
|
|
if (result.ErrorMessage.Equals("Not Found"))
|
|
|
|
|
return NotFound(result.ErrorMessage);
|
|
|
|
|
|
|
|
|
|
return BadRequest(result.ErrorMessage);
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// DELETE request for deleting a region based on ID.
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
2023-10-19 14:59:02 -05:00
|
|
|
|
[Authorize(Roles = "admin")]
|
2023-09-13 00:28:24 -05:00
|
|
|
|
[HttpDelete("regions/{id}")]
|
|
|
|
|
public async Task<ActionResult> DeleteRegionAsync(int id)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
var result = await this.regionProvider.DeleteRegionAsync(id);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Region);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|