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

101 lines
2.8 KiB
C#
Raw 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
{
[Route("api")]
[ApiController]
public class LocationsController : ControllerBase
{
private ILocationsProvider LocationProvider;
public LocationsController(ILocationsProvider LocationsProvider)
{
this.LocationProvider = LocationsProvider;
}
/// <summary>
/// Get all locations.
/// </summary>
2023-08-15 22:52:30 -05:00
[HttpGet("Locations")]
public async Task<ActionResult> GetLocationsAsync()
{
var result = await LocationProvider.GetLocationsAsync();
if (result.IsSuccess)
{
return Ok(result.locations);
}
return NotFound();
}
/// <summary>
/// Get all locations based on locationdId.
/// </summary>
2023-08-15 22:52:30 -05:00
[HttpGet("Locations/{id}")]
public async Task<ActionResult> GetLocationByIdAsync(string id)
{
var result = await LocationProvider.GetLocationByIdAsync(id);
if (result.IsSuccess)
{
return Ok(result.Location);
}
return NotFound();
}
/// <summary>
/// Update a Location.
/// </summary>
2023-08-15 22:52:30 -05:00
[HttpPut("Locations")]
public async Task<IActionResult> UpdateLocation(Models.Location Location)
2023-08-15 22:52:30 -05:00
{
if (Location != null)
{
var result = await this.LocationProvider.UpdateLocationAsync(Location);
if (result.IsSuccess)
{
return Ok(result.ErrorMessage);
}
return NotFound();
}
return NotFound();
}
/// <summary>
/// Save a new location.
/// </summary>
2023-08-15 22:52:30 -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.Question);
}
return NotFound();
}
return CreatedAtRoute("DefaultApi", new { id = Location.Id }, Location);
}
/// <summary>
/// Delete an existing location.
/// </summary>
2023-08-15 22:52:30 -05:00
[HttpDelete("Locations/{id}")]
public async Task<IActionResult> DeleteLocation(string id)
{
var result = await this.LocationProvider.DeleteLocationAsync(id);
if (result.IsSuccess)
{
return Ok(result.ErrorMessage);
}
return NotFound();
}
}
}