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

100 lines
3.0 KiB
C#

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;
}
/// <summary>
/// Get all locations.
/// </summary>
[Authorize(Roles = "admin")]
[HttpGet("Locations")]
public async Task<ActionResult> GetLocationsAsync()
{
var result = await LocationProvider.GetLocationsAsync();
if (result.IsSuccess)
{
return Ok(result.Locations);
}
return NoContent();
}
/// <summary>
/// Get all locations based on locationdId.
/// </summary>
[Authorize(Roles = "admin")]
[HttpGet("Locations/{id}")]
public async Task<ActionResult> GetLocationByIdAsync(int id)
{
var result = await LocationProvider.GetLocationByIdAsync(id);
if (result.IsSuccess)
{
return Ok(result.Location);
}
return NotFound();
}
/// <summary>
/// Update a Location.
/// </summary>
[Authorize(Roles = "admin")]
[HttpPut("Locations/{id}")]
public async Task<IActionResult> 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();
}
/// <summary>
/// Save a new location.
/// </summary>
[Authorize(Roles = "admin")]
[HttpPost("Locations")]
public async Task<IActionResult> 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();
}
/// <summary>
/// Delete an existing location.
/// </summary>
[Authorize(Roles = "admin")]
[HttpDelete("Locations/{id}")]
public async Task<IActionResult> DeleteLocation(int id)
{
var result = await this.LocationProvider.DeleteLocationAsync(id);
if (result.IsSuccess)
{
return Ok(result.Location);
}
return NotFound();
}
}
}