From 0717772516364b8ba6dcd1c705dbdf045f587234 Mon Sep 17 00:00:00 2001 From: romai Date: Thu, 12 Dec 2024 00:18:00 +0400 Subject: [PATCH] =?UTF-8?q?Crud=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= =?UTF-8?q?=D1=8E=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Address Directory/Contracts/CityDTO.cs | 2 +- Address Directory/Contracts/RegionDTO.cs | 4 +- Address Directory/Contracts/StreetDTO.cs | 6 +- .../Web/Controllers/CityController.cs | 79 +++++++++++++++++++ .../Web/Controllers/RegionController.cs | 78 ++++++++++++++++++ .../Web/Controllers/StreetController.cs | 77 ++++++++++++++++++ 6 files changed, 240 insertions(+), 6 deletions(-) create mode 100644 Address Directory/Web/Controllers/CityController.cs create mode 100644 Address Directory/Web/Controllers/RegionController.cs create mode 100644 Address Directory/Web/Controllers/StreetController.cs diff --git a/Address Directory/Contracts/CityDTO.cs b/Address Directory/Contracts/CityDTO.cs index 85ede52..7f5f861 100644 --- a/Address Directory/Contracts/CityDTO.cs +++ b/Address Directory/Contracts/CityDTO.cs @@ -2,7 +2,7 @@ namespace Contracts { - public record CityDto(Guid Id, string Name, Guid RegionId, List Streets); + public record CityDto(Guid Id, string Name, Guid RegionId, List? Streets); public record CityDtoForCreate(string Name, Guid RegionId); public record CityDtoForUpdate(Guid Id, string Name, Guid RegionId); } diff --git a/Address Directory/Contracts/RegionDTO.cs b/Address Directory/Contracts/RegionDTO.cs index b9faff5..d7dbfa8 100644 --- a/Address Directory/Contracts/RegionDTO.cs +++ b/Address Directory/Contracts/RegionDTO.cs @@ -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? Cities); public record RegionDtoForCreate(string Name, int Code); - public record RegionDtoForUpdate(string Name, int Code, List? Cities); + public record RegionDtoForUpdate(string Name, int Code); } diff --git a/Address Directory/Contracts/StreetDTO.cs b/Address Directory/Contracts/StreetDTO.cs index 50fddd1..db4821b 100644 --- a/Address Directory/Contracts/StreetDTO.cs +++ b/Address Directory/Contracts/StreetDTO.cs @@ -1,9 +1,9 @@ namespace Contracts { - public record StreetDto(Guid Id, string Name, List HouseNumbers, + public record StreetDto(Guid Id, string Name, List? HouseNumbers, int Index, int OkatoCode, int TaxCode, Guid CityId); - public record StreetDtoForCreate(string Name, List HouseNumbers, + public record StreetDtoForCreate(string Name, List? HouseNumbers, int Index, int OkatoCode, int TaxCode, Guid CityId); - public record StreetDtoForUpdate(string Name, List HouseNumbers, + public record StreetDtoForUpdate(string Name, List? HouseNumbers, int Index, int OkatoCode, int TaxCode, Guid CityId); } diff --git a/Address Directory/Web/Controllers/CityController.cs b/Address Directory/Web/Controllers/CityController.cs new file mode 100644 index 0000000..aae5ccc --- /dev/null +++ b/Address Directory/Web/Controllers/CityController.cs @@ -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 GetAllAsync() + { + var cities = await _cityService.GetAllAsync(); + return Ok(cities); + } + [HttpGet("{id:guid}")] + public async Task GetByIdAsync(Guid id) + { + var city = await _cityService.GetByIdAsync(id); + if (city == null) + { + return NotFound("Город не найден."); + } + return Ok(city); + } + [HttpPost] + public async Task 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 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 DeleteAsync(Guid id) + { + try + { + await _cityService.DeleteAsync(id); + } + catch (KeyNotFoundException) + { + return NotFound("Город не найден."); + } + + return NoContent(); + } + + } +} diff --git a/Address Directory/Web/Controllers/RegionController.cs b/Address Directory/Web/Controllers/RegionController.cs new file mode 100644 index 0000000..6fec693 --- /dev/null +++ b/Address Directory/Web/Controllers/RegionController.cs @@ -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 GetAllAsync() + { + var regions = await _regionService.GetAllAsync(); + return Ok(regions); + } + [HttpGet("{id:guid}")] + public async Task GetByIdAsync(Guid id) + { + var region = await _regionService.GetByIdAsync(id); + if (region == null) + { + return NotFound("Регион не найден."); + } + return Ok(region); + } + [HttpPost] + public async Task 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 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 DeleteAsync(Guid id) + { + try + { + await _regionService.DeleteAsync(id); + } + catch (KeyNotFoundException) + { + return NotFound("Регион не найден."); + } + + return NoContent(); + } + + } +} diff --git a/Address Directory/Web/Controllers/StreetController.cs b/Address Directory/Web/Controllers/StreetController.cs new file mode 100644 index 0000000..26052c3 --- /dev/null +++ b/Address Directory/Web/Controllers/StreetController.cs @@ -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 GetAllAsync() + { + var cities = await _streetService.GetAllAsync(); + return Ok(cities); + } + [HttpGet("{id:guid}")] + public async Task GetByIdAsync(Guid id) + { + var street = await _streetService.GetByIdAsync(id); + if (street == null) + { + return NotFound("Город не найден."); + } + return Ok(street); + } + [HttpPost] + public async Task 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 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 DeleteAsync(Guid id) + { + try + { + await _streetService.DeleteAsync(id); + } + catch (KeyNotFoundException) + { + return NotFound("Город не найден."); + } + + return NoContent(); + } + } +}