| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using AutoMapper; |
| | | 6 | | using Microsoft.AspNetCore.Http; |
| | | 7 | | using Microsoft.AspNetCore.Mvc; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | using Newtonsoft.Json; |
| | | 10 | | using SVETA.Api.Data.DTO; |
| | | 11 | | using SVETA.Api.Data.DTO.TaxSystem; |
| | | 12 | | using Swashbuckle.AspNetCore.Annotations; |
| | | 13 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 14 | | using WinSolutions.Sveta.Server.Data.DataModel.Kinds; |
| | | 15 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 16 | | using WinSolutions.Sveta.Common.Extensions; |
| | | 17 | | |
| | | 18 | | |
| | | 19 | | namespace SVETA.Api.Controllers |
| | | 20 | | { |
| | | 21 | | [Route("api/v1/TaxSystems")] |
| | | 22 | | [ApiController] |
| | | 23 | | public class TaxSystemsController : SvetaController |
| | | 24 | | { |
| | | 25 | | private readonly ILogger<TaxSystemsController> _logger; |
| | | 26 | | private readonly ITaxSystemService _taxSystemService; |
| | | 27 | | private readonly IDirectoriesService _dirService; |
| | | 28 | | |
| | 0 | 29 | | public TaxSystemsController(ITaxSystemService taxSystemService, IDirectoriesService dirService,ILogger<TaxSystem |
| | 0 | 30 | | { |
| | 0 | 31 | | _logger = logger; |
| | 0 | 32 | | _dirService = dirService; |
| | 0 | 33 | | _taxSystemService = taxSystemService; |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Возвращает систему налогообложения по id |
| | | 38 | | /// </summary> |
| | | 39 | | /// <remarks>auth: aabelentsov</remarks> |
| | | 40 | | /// <param name="id">Идентификатор системы налогообложения</param> |
| | | 41 | | /// <returns></returns> |
| | | 42 | | [HttpGet("{id}")] |
| | | 43 | | [SwaggerResponse(200, "Успешно", typeof(TaxSystemResponseDTO))] |
| | | 44 | | [SwaggerResponse(404, "Успешно, нет данных для отображения", typeof(ErrorDTO))] |
| | | 45 | | [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))] |
| | | 46 | | public async Task<IActionResult> GetTaxSystem(long id) |
| | 0 | 47 | | { |
| | 0 | 48 | | _logger.LogDebug($"Get taxSystem by id {id}"); |
| | 0 | 49 | | var tax = await _taxSystemService.GetTaxSystem(id); |
| | 0 | 50 | | if (tax == null) |
| | 0 | 51 | | { |
| | 0 | 52 | | _logger.LogDebug($"TaxSystem #{id} not found"); |
| | 0 | 53 | | return NotFoundResult(); |
| | | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | _logger.LogDebug($"TaxSystem #{id} found and mapped"); |
| | 0 | 57 | | return Ok(ToDtoMapper().Map<TaxSystemResponseDTO>(tax)); |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Возвращает список типов систем налогообложения |
| | | 62 | | /// </summary> |
| | | 63 | | /// <remarks>auth: aabelentsov</remarks> |
| | | 64 | | /// <param name="page">Любое значение ниже нуля изменится на 1, Текущая страница</param> |
| | | 65 | | /// <param name="limit">Любое значение ниже нуля изменится на 10, Количество для отбора</param> |
| | | 66 | | /// <param name="filter">фильтр по полям - Name, Code</param> |
| | | 67 | | /// <param name="sort">Сортировка по полям - name - по умолчанию, name|desc, code, code|desc</param> |
| | | 68 | | /// <returns></returns> |
| | | 69 | | [HttpGet()] |
| | | 70 | | [SwaggerResponse(200, "Успешно", typeof(List<TaxSystemResponseDTO>))] |
| | | 71 | | [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))] |
| | | 72 | | public async Task<IActionResult> GetTaxSystems(string filter, string sort, int page = 1, int limit = 10) |
| | 0 | 73 | | { |
| | 0 | 74 | | filter = filter.NormalizeName(); |
| | 0 | 75 | | limit = limit < 1 ? 10 : limit; |
| | | 76 | | |
| | 0 | 77 | | _logger.LogDebug($"Get list tax systems: page {page}, limit {limit}, filter {filter}, sort {sort}"); |
| | 0 | 78 | | var taxes = await _taxSystemService.GetTaxSystems(page, limit, filter, sort); |
| | | 79 | | |
| | 0 | 80 | | _logger.LogDebug($"TaxSystem have {taxes.Count} count. Create map"); |
| | 0 | 81 | | return Ok(ToDtoMapper().Map<List<TaxSystemResponseDTO>>(taxes)); |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Создает систему налогообложения |
| | | 86 | | /// </summary> |
| | | 87 | | /// <remarks>auth: aabelentsov</remarks> |
| | | 88 | | /// <param name="taxIn">Объект системы налогообложения</param> |
| | | 89 | | /// <returns></returns> |
| | | 90 | | [HttpPost()] |
| | | 91 | | [SwaggerResponse(200, "Успешно", typeof(TaxSystemResponseDTO))] |
| | | 92 | | [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))] |
| | | 93 | | public async Task<IActionResult> CreateTaxSystem(TaxSystemRequestDTO taxIn) |
| | 0 | 94 | | { |
| | 0 | 95 | | _logger.LogDebug($"Create taxSystem {JsonConvert.SerializeObject(taxIn)}"); |
| | 0 | 96 | | TaxSystem tax = new TaxSystem() |
| | 0 | 97 | | { |
| | 0 | 98 | | Name = taxIn.Name, |
| | 0 | 99 | | Code = taxIn.Code, |
| | 0 | 100 | | Description = taxIn.Description, |
| | 0 | 101 | | RecState = await _dirService.GetRecordState((long)RecordState.Active) |
| | 0 | 102 | | }; |
| | 0 | 103 | | await _taxSystemService.CreateTaxSystem(tax); |
| | 0 | 104 | | _logger.LogDebug($"Tax system created with Id {tax.Id}"); |
| | 0 | 105 | | return Ok(ToDtoMapper().Map<TaxSystemResponseDTO>(tax)); |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Обновление записи системы налогообложения |
| | | 110 | | /// </summary> |
| | | 111 | | /// <remarks>auth: aabelentsov</remarks> |
| | | 112 | | /// <param name="id">Идентификтор объекта для обновления</param> |
| | | 113 | | /// <param name="taxIn">Объект системы для обновления</param> |
| | | 114 | | /// <returns></returns> |
| | | 115 | | [HttpPut("{id}")] |
| | | 116 | | [SwaggerResponse(201, "Успешно", typeof(TaxSystemResponseDTO))] |
| | | 117 | | [SwaggerResponse(404, "Не найден объект для обновления", typeof(ErrorDTO))] |
| | | 118 | | [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))] |
| | | 119 | | public async Task<IActionResult> UpdateTaxSystem(long id, [FromBody] TaxSystemRequestDTO taxIn) |
| | 0 | 120 | | { |
| | 0 | 121 | | _logger.LogDebug($"Update tax system: {JsonConvert.SerializeObject(taxIn)}"); |
| | 0 | 122 | | TaxSystem tax = await _taxSystemService.GetTaxSystem(id); |
| | 0 | 123 | | if (tax == null) |
| | 0 | 124 | | { |
| | 0 | 125 | | _logger.LogDebug($"TaxSystem with #{id} not found"); |
| | 0 | 126 | | return NotFoundResult($"Не найден объект #{id} для обновления"); |
| | | 127 | | } |
| | 0 | 128 | | tax.Name = taxIn.Name; |
| | 0 | 129 | | tax.Code = taxIn.Code; |
| | 0 | 130 | | tax.Description = taxIn.Description; |
| | 0 | 131 | | await _taxSystemService.UpdateTaxSystem(tax); |
| | | 132 | | |
| | 0 | 133 | | _logger.LogDebug($"Tax system {tax.Id} updated successefully"); |
| | | 134 | | |
| | 0 | 135 | | return Ok(ToDtoMapper().Map<TaxSystemResponseDTO>(tax)); |
| | 0 | 136 | | } |
| | | 137 | | |
| | | 138 | | /// <summary> |
| | | 139 | | /// Удаление записи системы налогообложения |
| | | 140 | | /// </summary> |
| | | 141 | | /// <remarks>auth: aabelentsov</remarks> |
| | | 142 | | /// <param name="id">Идентификатор записи</param> |
| | | 143 | | /// <returns></returns> |
| | | 144 | | [HttpDelete("{id}")] |
| | | 145 | | [SwaggerResponse(200, "Успешно", typeof(OkObjectResult))] |
| | | 146 | | [SwaggerResponse(404, "Не найден объект для удаления", typeof(ErrorDTO))] |
| | | 147 | | [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))] |
| | | 148 | | public async Task<IActionResult> DeleteTaxSystem(long id) |
| | 0 | 149 | | { |
| | 0 | 150 | | _logger.LogDebug($"Delete recored #{id}"); |
| | 0 | 151 | | await _taxSystemService.DeleteTaxSystem(id); |
| | 0 | 152 | | return Ok(); |
| | 0 | 153 | | } |
| | | 154 | | |
| | | 155 | | private static IMapper ToDtoMapper() |
| | 0 | 156 | | { |
| | 0 | 157 | | var config = new MapperConfiguration(cfg => |
| | 0 | 158 | | { |
| | 0 | 159 | | cfg.CreateMap<TaxSystem, TaxSystemResponseDTO>(); |
| | 0 | 160 | | }); |
| | 0 | 161 | | return config.CreateMapper(); |
| | 0 | 162 | | } |
| | | 163 | | } |
| | | 164 | | } |