using DamageAssesment.Api.Locations.Interfaces; using Microsoft.AspNetCore.Authorization; 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; } /// /// Get all regions.2 /// [Authorize(Roles = "admin")] [HttpGet("regions")] 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. /// [Authorize(Roles = "admin")] [HttpGet("regions/{id}")] public async Task GetRegionAsync(int id) { var result = await this.regionProvider.GetRegionByIdAsync(id); if (result.IsSuccess) { return Ok(result.Region); } return NotFound(); } /// /// POST request for creating a new region. /// [Authorize(Roles = "admin")] [HttpPost("regions")] 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. /// [Authorize(Roles = "admin")] [HttpPut("regions/{id}")] public async Task 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); } /// /// DELETE request for deleting a region based on ID. /// [Authorize(Roles = "admin")] [HttpDelete("regions/{id}")] public async Task DeleteRegionAsync(int id) { var result = await this.regionProvider.DeleteRegionAsync(id); if (result.IsSuccess) { return Ok(result.Region); } return NotFound(); } } }