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;
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <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)
|
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
return Ok(result.Locations);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
2023-09-01 11:14:25 -05:00
|
|
|
|
return NoContent();
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get all locations based on locationdId.
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
2023-10-04 10:55:35 -05:00
|
|
|
|
[HttpGet("locations/{id}")]
|
2023-09-13 00:28:24 -05:00
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Update a Location.
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
2023-10-04 10:55:35 -05:00
|
|
|
|
[HttpPut("locations/{id}")]
|
2023-09-13 00:28:24 -05:00
|
|
|
|
public async Task<IActionResult> UpdateLocation(int id, Models.Location Location)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
if (Location != null)
|
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
var result = await this.LocationProvider.UpdateLocationAsync(id, Location);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
return Ok(result.Location);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Save a new location.
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
2023-10-04 10:55:35 -05:00
|
|
|
|
[HttpPost("locations")]
|
2023-08-25 17:51:50 -05:00
|
|
|
|
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)
|
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
return Ok(result.Location);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
2023-09-13 00:28:24 -05:00
|
|
|
|
return BadRequest(result.ErrorMessage);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
2023-09-01 11:14:25 -05:00
|
|
|
|
return BadRequest();
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Delete an existing location.
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
2023-10-04 10:55:35 -05:00
|
|
|
|
[HttpDelete("locations/{id}")]
|
2023-09-13 00:28:24 -05:00
|
|
|
|
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)
|
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
return Ok(result.Location);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|