80 lines
2.1 KiB
C#
80 lines
2.1 KiB
C#
using Contracts;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Services;
|
|
using Services.Abstractions;
|
|
|
|
namespace Web.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/cities")]
|
|
public class CityController : ControllerBase
|
|
{
|
|
private readonly ICityService _cityService;
|
|
public CityController(ICityService cityService)
|
|
{
|
|
_cityService = cityService;
|
|
}
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetAllAsync()
|
|
{
|
|
var cities = await _cityService.GetAllAsync();
|
|
return Ok(cities);
|
|
}
|
|
[HttpGet("{id:guid}")]
|
|
public async Task<IActionResult> GetByIdAsync(Guid id)
|
|
{
|
|
var city = await _cityService.GetByIdAsync(id);
|
|
if (city == null)
|
|
{
|
|
return NotFound("Город не найден.");
|
|
}
|
|
return Ok(city);
|
|
}
|
|
[HttpPost]
|
|
public async Task<IActionResult> CreateAsync([FromBody] CityDtoForCreate cityDto)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
var newCity = await _cityService.CreateAsync(cityDto);
|
|
return Ok(newCity);
|
|
}
|
|
[HttpPut("{id:guid}")]
|
|
public async Task<IActionResult> UpdateAsync(Guid id, [FromBody] CityDtoForUpdate cityDto)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
try
|
|
{
|
|
await _cityService.UpdateAsync(id, cityDto);
|
|
}
|
|
catch (KeyNotFoundException)
|
|
{
|
|
return NotFound("Город не найден.");
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
[HttpDelete("{id:guid}")]
|
|
public async Task<IActionResult> DeleteAsync(Guid id)
|
|
{
|
|
try
|
|
{
|
|
await _cityService.DeleteAsync(id);
|
|
}
|
|
catch (KeyNotFoundException)
|
|
{
|
|
return NotFound("Город не найден.");
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
}
|
|
}
|