using DamageAssesment.Api.Locations.Interfaces; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace DamageAssesment.Api.Locations.Controllers { [ApiController] public class LocationsController : ControllerBase { private ILocationsProvider LocationProvider; public LocationsController(ILocationsProvider LocationsProvider) { this.LocationProvider = LocationsProvider; } /// /// Get all locations. /// [Authorize(Roles = "admin")] [HttpGet("locations")] public async Task GetLocationsAsync() { var result = await LocationProvider.GetLocationsAsync(); if (result.IsSuccess) { return Ok(result.Locations); } return NoContent(); } /// /// Get all locations based on locationdId. /// [Authorize(Roles = "admin")] [HttpGet("locations/{id}")] public async Task GetLocationByIdAsync(int id) { var result = await LocationProvider.GetLocationByIdAsync(id); if (result.IsSuccess) { return Ok(result.Location); } return NotFound(); } /// /// Update a Location. /// [Authorize(Roles = "admin")] [HttpPut("locations/{id}")] public async Task UpdateLocation(int id, Models.Location Location) { if (Location != null) { var result = await this.LocationProvider.UpdateLocationAsync(id, Location); if (result.IsSuccess) { return Ok(result.Location); } return NotFound(); } return NotFound(); } /// /// Save a new location. /// [Authorize(Roles = "admin")] [HttpPost("locations")] public async Task CreateLocation(Models.Location Location) { if (Location != null) { var result = await this.LocationProvider.PostLocationAsync(Location); if (result.IsSuccess) { return Ok(result.Location); } return BadRequest(result.ErrorMessage); } return BadRequest(); } /// /// Delete an existing location. /// [Authorize(Roles = "admin")] [HttpDelete("locations/{id}")] public async Task DeleteLocation(int id) { var result = await this.LocationProvider.DeleteLocationAsync(id); if (result.IsSuccess) { return Ok(result.Location); } return NotFound(); } } }