< Summary

Class:SVETA.Api.Controllers.TaxSystemsController
Assembly:SVETA.Api
File(s):/opt/dev/sveta_api_build/SVETA.Api/Controllers/TaxSystemsController.cs
Covered lines:0
Uncovered lines:63
Coverable lines:63
Total lines:164
Line coverage:0% (0 of 63)
Covered branches:0
Total branches:12
Branch coverage:0% (0 of 12)

Metrics

MethodLine coverage Branch coverage
.ctor(...)0%100%
GetTaxSystem()0%0%
GetTaxSystems()0%0%
CreateTaxSystem()0%0%
UpdateTaxSystem()0%0%
DeleteTaxSystem()0%100%
ToDtoMapper()0%0%

File(s)

/opt/dev/sveta_api_build/SVETA.Api/Controllers/TaxSystemsController.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Threading.Tasks;
 5using AutoMapper;
 6using Microsoft.AspNetCore.Http;
 7using Microsoft.AspNetCore.Mvc;
 8using Microsoft.Extensions.Logging;
 9using Newtonsoft.Json;
 10using SVETA.Api.Data.DTO;
 11using SVETA.Api.Data.DTO.TaxSystem;
 12using Swashbuckle.AspNetCore.Annotations;
 13using WinSolutions.Sveta.Server.Data.DataModel.Entities;
 14using WinSolutions.Sveta.Server.Data.DataModel.Kinds;
 15using WinSolutions.Sveta.Server.Services.Interfaces;
 16using WinSolutions.Sveta.Common.Extensions;
 17
 18
 19namespace 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
 029        public TaxSystemsController(ITaxSystemService taxSystemService, IDirectoriesService dirService,ILogger<TaxSystem
 030        {
 031            _logger = logger;
 032            _dirService = dirService;
 033            _taxSystemService = taxSystemService;
 034        }
 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)
 047        {
 048            _logger.LogDebug($"Get taxSystem by id {id}");
 049            var tax = await _taxSystemService.GetTaxSystem(id);
 050            if (tax == null)
 051            {
 052                _logger.LogDebug($"TaxSystem #{id} not found");
 053                return NotFoundResult();
 54            }
 55
 056            _logger.LogDebug($"TaxSystem #{id} found and mapped");
 057            return Ok(ToDtoMapper().Map<TaxSystemResponseDTO>(tax));
 058        }
 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)
 073        {
 074            filter = filter.NormalizeName();
 075            limit = limit < 1 ? 10 : limit;
 76
 077            _logger.LogDebug($"Get list tax systems: page {page}, limit {limit}, filter {filter}, sort {sort}");
 078            var taxes = await _taxSystemService.GetTaxSystems(page, limit, filter, sort);
 79
 080            _logger.LogDebug($"TaxSystem have {taxes.Count} count. Create map");
 081            return Ok(ToDtoMapper().Map<List<TaxSystemResponseDTO>>(taxes));
 082        }
 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)
 094        {
 095            _logger.LogDebug($"Create taxSystem {JsonConvert.SerializeObject(taxIn)}");
 096            TaxSystem tax = new TaxSystem()
 097            {
 098                Name = taxIn.Name,
 099                Code = taxIn.Code,
 0100                Description = taxIn.Description,
 0101                RecState = await _dirService.GetRecordState((long)RecordState.Active)
 0102            };
 0103            await _taxSystemService.CreateTaxSystem(tax);
 0104            _logger.LogDebug($"Tax system created with Id {tax.Id}");
 0105            return Ok(ToDtoMapper().Map<TaxSystemResponseDTO>(tax));
 0106        }
 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)
 0120        {
 0121            _logger.LogDebug($"Update tax system: {JsonConvert.SerializeObject(taxIn)}");
 0122            TaxSystem tax = await _taxSystemService.GetTaxSystem(id);
 0123            if (tax == null)
 0124            {
 0125                _logger.LogDebug($"TaxSystem with #{id} not found");
 0126                return NotFoundResult($"Не найден объект #{id} для обновления");
 127            }
 0128            tax.Name = taxIn.Name;
 0129            tax.Code = taxIn.Code;
 0130            tax.Description = taxIn.Description;
 0131            await _taxSystemService.UpdateTaxSystem(tax);
 132
 0133            _logger.LogDebug($"Tax system {tax.Id} updated successefully");
 134
 0135            return Ok(ToDtoMapper().Map<TaxSystemResponseDTO>(tax));
 0136        }
 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)
 0149        {
 0150            _logger.LogDebug($"Delete recored #{id}");
 0151            await _taxSystemService.DeleteTaxSystem(id);
 0152            return Ok();
 0153        }
 154
 155        private static IMapper ToDtoMapper()
 0156        {
 0157            var config = new MapperConfiguration(cfg =>
 0158            {
 0159                cfg.CreateMap<TaxSystem, TaxSystemResponseDTO>();
 0160            });
 0161            return config.CreateMapper();
 0162        }
 163    }
 164}