API: Add StreetController
This commit is contained in:
parent
2a68fa8257
commit
83fc5bdb1d
88
src/API/StreetController.cs
Normal file
88
src/API/StreetController.cs
Normal file
@ -0,0 +1,88 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using NetTopologySuite.Geometries;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/streets")]
|
||||
public class StreetController : ControllerBase
|
||||
{
|
||||
private readonly StreetService StreetService;
|
||||
|
||||
public StreetController(StreetService streetService, ILogger<StreetController> logger)
|
||||
{
|
||||
StreetService = streetService;
|
||||
}
|
||||
|
||||
// POST /api/streets/
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> CreateStreet([FromBody] CreateStreetDTO dto)
|
||||
{
|
||||
if (dto == null)
|
||||
return BadRequest("Invalid request body.");
|
||||
|
||||
try
|
||||
{
|
||||
var geometry = StreetService.ToGeometry(dto.Geometry);
|
||||
|
||||
var street = new Street(dto.Name, dto.Capacity, geometry);
|
||||
var createdStreet = await StreetService.CreateStreetAsync(street);
|
||||
|
||||
return Created();
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
return Conflict(ex);
|
||||
}
|
||||
}
|
||||
|
||||
// GET /api/streets/{streetname}
|
||||
[HttpGet("{streetname}")]
|
||||
public async Task<IActionResult> GetStreet(string streetname)
|
||||
{
|
||||
try
|
||||
{
|
||||
var street = await StreetService.GetStreetAsync(streetname);
|
||||
return Ok(street);
|
||||
}
|
||||
catch (KeyNotFoundException ex)
|
||||
{
|
||||
return NotFound(ex);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE /api/streets/{streetname}
|
||||
[HttpDelete("{streetname}")]
|
||||
public async Task<IActionResult> DeleteStreet(string streetname)
|
||||
{
|
||||
var deleted = await StreetService.DeleteStreetAsync(streetname);
|
||||
if (!deleted)
|
||||
return NotFound($"Street '{streetname}' not found.");
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
// PATCH /api/streets/{streetname}
|
||||
[HttpPatch("{streetname}")]
|
||||
public async Task<IActionResult> AddPoint(string streetname, [FromBody] AddPointDTO dto)
|
||||
{
|
||||
if (dto == null)
|
||||
return BadRequest("Invalid request body.");
|
||||
|
||||
try
|
||||
{
|
||||
await StreetService.AddPointAsync(streetname, StreetService.ToGeometryPoint(dto.Point), dto.usePostGIS);
|
||||
return Ok();
|
||||
}
|
||||
catch (KeyNotFoundException ex)
|
||||
{
|
||||
return NotFound(ex);
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
return BadRequest(ex);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return Conflict("Concurrency conflict. Please retry your request.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user