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

99 lines
2.8 KiB
C#
Raw Permalink Normal View History

2023-08-15 22:52:30 -05:00
using DamageAssesment.Api.Locations.Interfaces;
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>
2023-10-04 10:55:35 -05:00
[HttpGet("locations")]
2023-08-15 22:52:30 -05:00
public async Task<ActionResult> GetLocationsAsync()
{
var result = await LocationProvider.GetLocationsAsync();
if (result.IsSuccess)
{
return Ok(result.Locations);
2023-08-15 22:52:30 -05:00
}
return NoContent();
2023-08-15 22:52:30 -05:00
}
/// <summary>
/// Get all locations based on locationdId.
/// </summary>
2023-10-04 10:55:35 -05:00
[HttpGet("locations/{id}")]
public async Task<ActionResult> GetLocationByIdAsync(int id)
2023-08-15 22:52:30 -05:00
{
var result = await LocationProvider.GetLocationByIdAsync(id);
if (result.IsSuccess)
{
return Ok(result.Location);
}
return NotFound();
}
/// <summary>
/// Update a Location.
/// </summary>
2023-10-04 10:55:35 -05:00
[HttpPut("locations/{id}")]
public async Task<IActionResult> UpdateLocation(int id, Models.Location Location)
2023-08-15 22:52:30 -05:00
{
if (Location != null)
{
var result = await this.LocationProvider.UpdateLocationAsync(id, Location);
2023-08-15 22:52:30 -05:00
if (result.IsSuccess)
{
return Ok(result.Location);
2023-08-15 22:52:30 -05:00
}
return NotFound();
}
return NotFound();
}
/// <summary>
/// Save a new location.
/// </summary>
2023-10-04 10:55:35 -05:00
[HttpPost("locations")]
public async Task<IActionResult> CreateLocation(Models.Location Location)
2023-08-15 22:52:30 -05:00
{
if (Location != null)
{
var result = await this.LocationProvider.PostLocationAsync(Location);
if (result.IsSuccess)
{
return Ok(result.Location);
2023-08-15 22:52:30 -05:00
}
return BadRequest(result.ErrorMessage);
2023-08-15 22:52:30 -05:00
}
return BadRequest();
2023-08-15 22:52:30 -05:00
}
/// <summary>
/// Delete an existing location.
/// </summary>
2023-10-04 10:55:35 -05:00
[HttpDelete("locations/{id}")]
public async Task<IActionResult> DeleteLocation(int id)
2023-08-15 22:52:30 -05:00
{
var result = await this.LocationProvider.DeleteLocationAsync(id);
if (result.IsSuccess)
{
return Ok(result.Location);
2023-08-15 22:52:30 -05:00
}
return NotFound();
}
}
}