Crud работают

This commit is contained in:
romai 2024-12-12 00:18:00 +04:00
parent c5041e5fed
commit 0717772516
6 changed files with 240 additions and 6 deletions

View File

@ -2,7 +2,7 @@
namespace Contracts
{
public record CityDto(Guid Id, string Name, Guid RegionId, List<StreetDto> Streets);
public record CityDto(Guid Id, string Name, Guid RegionId, List<StreetDto>? Streets);
public record CityDtoForCreate(string Name, Guid RegionId);
public record CityDtoForUpdate(Guid Id, string Name, Guid RegionId);
}

View File

@ -2,7 +2,7 @@
namespace Contracts
{
public record RegionDto(Guid Id, string Name, int Code);
public record RegionDto(Guid Id, string Name, int Code, List<CityDto>? Cities);
public record RegionDtoForCreate(string Name, int Code);
public record RegionDtoForUpdate(string Name, int Code, List<CityDto>? Cities);
public record RegionDtoForUpdate(string Name, int Code);
}

View File

@ -1,9 +1,9 @@
namespace Contracts
{
public record StreetDto(Guid Id, string Name, List<int> HouseNumbers,
public record StreetDto(Guid Id, string Name, List<int>? HouseNumbers,
int Index, int OkatoCode, int TaxCode, Guid CityId);
public record StreetDtoForCreate(string Name, List<int> HouseNumbers,
public record StreetDtoForCreate(string Name, List<int>? HouseNumbers,
int Index, int OkatoCode, int TaxCode, Guid CityId);
public record StreetDtoForUpdate(string Name, List<int> HouseNumbers,
public record StreetDtoForUpdate(string Name, List<int>? HouseNumbers,
int Index, int OkatoCode, int TaxCode, Guid CityId);
}

View File

@ -0,0 +1,79 @@
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();
}
}
}

View File

@ -0,0 +1,78 @@
using Contracts;
using Microsoft.AspNetCore.Mvc;
using Services.Abstractions;
namespace Presentation.Controllers
{
[ApiController]
[Route("api/regions")]
public class RegionController : ControllerBase
{
private readonly IRegionService _regionService;
public RegionController(IRegionService regionService)
{
_regionService = regionService;
}
[HttpGet]
public async Task<IActionResult> GetAllAsync()
{
var regions = await _regionService.GetAllAsync();
return Ok(regions);
}
[HttpGet("{id:guid}")]
public async Task<IActionResult> GetByIdAsync(Guid id)
{
var region = await _regionService.GetByIdAsync(id);
if (region == null)
{
return NotFound("Регион не найден.");
}
return Ok(region);
}
[HttpPost]
public async Task<IActionResult> CreateAsync([FromBody] RegionDtoForCreate regionDto)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var newRegion = await _regionService.CreateAsync(regionDto);
return Ok(newRegion);
}
[HttpPut("{id:guid}")]
public async Task<IActionResult> UpdateAsync(Guid id, [FromBody] RegionDtoForUpdate regionDto)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
try
{
await _regionService.UpdateAsync(id, regionDto);
}
catch (KeyNotFoundException)
{
return NotFound("Регион не найден.");
}
return NoContent();
}
[HttpDelete("{id:guid}")]
public async Task<IActionResult> DeleteAsync(Guid id)
{
try
{
await _regionService.DeleteAsync(id);
}
catch (KeyNotFoundException)
{
return NotFound("Регион не найден.");
}
return NoContent();
}
}
}

View File

@ -0,0 +1,77 @@
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();
}
}
}