78 lines
2.2 KiB
C#
Raw Permalink Normal View History

2024-12-12 00:18:00 +04:00
using Contracts;
using Microsoft.AspNetCore.Mvc;
using Services.Abstractions;
namespace Web.Controllers
{
[ApiController]
[Route("api/streets")]
public class StreetController : ControllerBase
{
private readonly IStreetService _streetService;
public StreetController(IStreetService streetService)
{
_streetService = streetService;
}
[HttpGet]
public async Task<IActionResult> GetAllAsync()
{
var cities = await _streetService.GetAllAsync();
return Ok(cities);
}
[HttpGet("{id:guid}")]
public async Task<IActionResult> GetByIdAsync(Guid id)
{
var street = await _streetService.GetByIdAsync(id);
if (street == null)
{
return NotFound("Город не найден.");
}
return Ok(street);
}
[HttpPost]
public async Task<IActionResult> CreateAsync([FromBody] StreetDtoForCreate streetDto)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var newStreet = await _streetService.CreateAsync(streetDto);
return Ok(newStreet);
}
[HttpPut("{id:guid}")]
public async Task<IActionResult> UpdateAsync(Guid id, [FromBody] StreetDtoForUpdate streetDto)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
try
{
await _streetService.UpdateAsync(id, streetDto);
}
catch (KeyNotFoundException)
{
return NotFound("Город не найден.");
}
return NoContent();
}
[HttpDelete("{id:guid}")]
public async Task<IActionResult> DeleteAsync(Guid id)
{
try
{
await _streetService.DeleteAsync(id);
}
catch (KeyNotFoundException)
{
return NotFound("Город не найден.");
}
return NoContent();
}
}
}