| | | 1 | | using WinSolutions.Sveta.Common.Extensions; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Microsoft.AspNetCore.Mvc; |
| | | 7 | | using Microsoft.Extensions.Logging; |
| | | 8 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 9 | | using Microsoft.AspNetCore.Authorization; |
| | | 10 | | using SVETA.Api.Data.Domain; |
| | | 11 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 12 | | using SVETA.Api.Data.DTO.DiscountColorDTO; |
| | | 13 | | using SVETA.Api.Data.DTO; |
| | | 14 | | using SVETA.Api.Helpers.Authorize; |
| | | 15 | | using WinSolutions.Sveta.Server.Data.DataModel.Kinds; |
| | | 16 | | using Swashbuckle.AspNetCore.Annotations; |
| | | 17 | | using WinSolutions.Sveta.Server.Domain; |
| | | 18 | | using WinSolutions.Sveta.Common; |
| | | 19 | | using CrmVtbc; |
| | | 20 | | |
| | | 21 | | namespace SVETA.Api.Controllers |
| | | 22 | | { |
| | | 23 | | [Authorize] |
| | | 24 | | [Route("api/v1/GoodLabels")] |
| | | 25 | | [ApiController] |
| | | 26 | | public class GoodLabelsController : SvetaController |
| | | 27 | | { |
| | | 28 | | const string _routeUrl = "api/v1/GoodLabels"; |
| | | 29 | | readonly IGoodLabelService _service; |
| | | 30 | | readonly IAuthenticationService _authUserService; |
| | | 31 | | readonly ILogger<GoodLabelsController> _logger; |
| | 0 | 32 | | public GoodLabelsController(IGoodLabelService service, IAuthenticationService authUserService, ILogger<GoodLabel |
| | 0 | 33 | | { |
| | 0 | 34 | | _service = service; |
| | 0 | 35 | | _authUserService = authUserService; |
| | 0 | 36 | | _logger = logger; |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Возвращает все ярлыки |
| | | 41 | | /// </summary> |
| | | 42 | | /// <remarks>author i.rebenok</remarks> |
| | | 43 | | /// <param name="page">Любое значение ниже нуля изменится на 1, пагинация: номер страницы</param> |
| | | 44 | | /// <param name="limit">Любое значение ниже нуля изменится на 10, пагинация: размер страницы</param> |
| | | 45 | | /// <param name="sort">Сортировка по имени и id: name, name|desc, id, id|desc. По умолчанию name</param> |
| | | 46 | | /// <param name="filter">Поиск по названию ярлыка</param> |
| | | 47 | | /// <param name="showcaseVisible">Вдимость на витрине. True - только видимые, false - только невидимые, null - в |
| | | 48 | | [HttpGet("")] |
| | | 49 | | [SwaggerResponse(200, "Успешно", typeof(BaseResponseDTO<GoodLabelResponseDTO>))] |
| | | 50 | | [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))] |
| | | 51 | | [SwaggerResponse(403, "Не разрешено для этого пользователя", typeof(ErrorDTO))] |
| | | 52 | | [Authorize(Roles = Role.SystemAdmin + "," + Role.SystemOperator + "," + Role.SupplierOwner + "," + Role.Supplier |
| | | 53 | | public async Task<IActionResult> GetGoodLabels(int page = 1, int limit = 10, string sort = default, string filte |
| | 0 | 54 | | { |
| | 0 | 55 | | filter = filter.NormalizeName(); |
| | 0 | 56 | | page = page < 1 ? 1 : page; |
| | 0 | 57 | | limit = limit < 1 ? 10 : limit; |
| | 0 | 58 | | var result = await _service.GetGoodLabels(page - 1, limit, sort, filter, showcaseVisible); |
| | 0 | 59 | | var param = $""; |
| | 0 | 60 | | var response = new BaseResponseDTO<GoodLabelResponseDTO>(_routeUrl, page, limit, result.TotalFilteredCount, |
| | 0 | 61 | | { |
| | 0 | 62 | | Data = result.Result.Select(x => new GoodLabelResponseDTO(x)).ToList(), |
| | 0 | 63 | | }; |
| | 0 | 64 | | return Ok(response); |
| | 0 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Возвращает ярлык по id |
| | | 69 | | /// </summary> |
| | | 70 | | /// <remarks>author i.rebenok</remarks> |
| | | 71 | | /// <param name="id">id ярлыка</param> |
| | | 72 | | [HttpGet("{id}")] |
| | | 73 | | [SwaggerResponse(200, "Успешно", typeof(IEnumerable<GoodLabelResponseDTO>))] |
| | | 74 | | [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))] |
| | | 75 | | [SwaggerResponse(403, "Не разрешено для этого пользователя", typeof(ErrorDTO))] |
| | | 76 | | [Authorize(Roles = Role.SystemAdmin + "," + Role.SystemOperator + "," + Role.SupplierOwner + "," + Role.Supplier |
| | | 77 | | public async Task<IActionResult> GetGoodLabel([SwaggerParameter(Required = true)] long id) |
| | 0 | 78 | | { |
| | 0 | 79 | | var result = await _service.GetGoodLabel(id); |
| | 0 | 80 | | return Ok(new GoodLabelResponseDTO(result)); |
| | 0 | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Создает запись с ярлыком и цветом. Цвет состоит из 6 символов RGB модели |
| | | 85 | | /// </summary> |
| | | 86 | | /// <remarks>author i.rebenok</remarks> |
| | | 87 | | /// <param name="data">объект GoodLabelRequestDTO</param> |
| | | 88 | | [HttpPost("")] |
| | | 89 | | [SwaggerResponse(201, "Успешно создано", typeof(GoodLabelRequestDTO))] |
| | | 90 | | [SwaggerResponse(404, "Нет записей", typeof(ErrorDTO))] |
| | | 91 | | [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))] |
| | | 92 | | [SwaggerResponse(400, "Некорректные входные данные", typeof(ErrorDTO))] |
| | | 93 | | [SwaggerResponse(403, "Не разрешено для этого пользователя", typeof(ErrorDTO))] |
| | | 94 | | [Authorize(Roles = Role.SystemAdmin + "," + Role.SystemOperator + "," + Role.SupplierOwner + "," + Role.Supplier |
| | | 95 | | public async Task<IActionResult> CreateGoodLabel([FromBody][SwaggerParameter(Required = true)] GoodLabelRequestD |
| | 0 | 96 | | { |
| | 0 | 97 | | if (!ModelState.IsValid) |
| | 0 | 98 | | return BadRequestResult("Некорректные входные данные"); |
| | 0 | 99 | | var existLabel = await _service.GetGoodLabelByName(data.Name); |
| | 0 | 100 | | if (existLabel != null) |
| | 0 | 101 | | return BadRequestResult($"Ярлык с именем {data.Name} уже существует"); |
| | 0 | 102 | | var label = new GoodLabel() |
| | 0 | 103 | | { |
| | 0 | 104 | | Name = data.Name, |
| | 0 | 105 | | Priority = data.Priority, |
| | 0 | 106 | | LabelColor = data.LabelColor, |
| | 0 | 107 | | TextColor = data.TextColor, |
| | 0 | 108 | | ShowcaseVisible = data.ShowcaseVisible |
| | 0 | 109 | | }; |
| | 0 | 110 | | await _service.Create(label); |
| | 0 | 111 | | return CreatedAtAction("GetGoodLabel", new { id = label.Id }, new GoodLabelResponseDTO(label)); |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Обновляет ярлык по id. Цвет состоит из 6 символов RGB модели |
| | | 116 | | /// </summary> |
| | | 117 | | /// <remarks>author i.rebenok</remarks> |
| | | 118 | | /// <param name="id">id записи</param> |
| | | 119 | | /// <param name="data">обюект GoodLabelRequestDTO</param> |
| | | 120 | | [HttpPut("{id}")] |
| | | 121 | | [SwaggerResponse(200, "Успешно обновлено", typeof(GoodLabelRequestDTO))] |
| | | 122 | | [SwaggerResponse(404, "Нет записей", typeof(ErrorDTO))] |
| | | 123 | | [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))] |
| | | 124 | | [SwaggerResponse(400, "Некорректные входные данные", typeof(ErrorDTO))] |
| | | 125 | | [SwaggerResponse(403, "Не разрешено для этого пользователя", typeof(ErrorDTO))] |
| | | 126 | | [Authorize(Roles = Role.SystemAdmin + "," + Role.SystemOperator + "," + Role.SupplierOwner + "," + Role.Supplier |
| | | 127 | | public async Task<IActionResult> UpdateGoodLabel([SwaggerParameter(Required = true)] long id, [FromBody][Swagger |
| | 0 | 128 | | { |
| | 0 | 129 | | if (!ModelState.IsValid) |
| | 0 | 130 | | return BadRequestResult("Некорректные входные данные"); |
| | 0 | 131 | | var label = await _service.GetGoodLabel(id); |
| | 0 | 132 | | if (label == null) |
| | 0 | 133 | | return NotFoundResult($"Ярлык с id={id} не найден"); |
| | 0 | 134 | | var existLabel = await _service.GetGoodLabelByName(data.Name); |
| | 0 | 135 | | if (existLabel?.Id != id) |
| | 0 | 136 | | return BadRequestResult($"Ярлык с именем {data.Name} уже существует"); |
| | 0 | 137 | | label.Name = data.Name; |
| | 0 | 138 | | label.Priority = data.Priority; |
| | 0 | 139 | | label.LabelColor = data.LabelColor; |
| | 0 | 140 | | label.TextColor = data.TextColor; |
| | 0 | 141 | | label.ShowcaseVisible = data.ShowcaseVisible; |
| | 0 | 142 | | await _service.Update(label); |
| | 0 | 143 | | return Ok(new GoodLabelResponseDTO(label)); |
| | 0 | 144 | | } |
| | | 145 | | |
| | | 146 | | /// <summary> |
| | | 147 | | /// Удаляет ярлык по id |
| | | 148 | | /// </summary> |
| | | 149 | | /// <remarks>author i.rebenok</remarks> |
| | | 150 | | /// <param name="id">id записи</param> |
| | | 151 | | [HttpDelete("{id}")] |
| | | 152 | | [SwaggerResponse(200, "Успешно удалено", typeof(EmptyResult))] |
| | | 153 | | [SwaggerResponse(404, "Нет записей", typeof(ErrorDTO))] |
| | | 154 | | [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))] |
| | | 155 | | [SwaggerResponse(403, "Не разрешено для этого пользователя", typeof(ErrorDTO))] |
| | | 156 | | [Authorize(Roles = Role.SystemAdmin + "," + Role.SystemOperator + "," + Role.SupplierOwner + "," + Role.Supplier |
| | | 157 | | public async Task<IActionResult> DeleteGoodLabel([SwaggerParameter(Required = true)] long id) |
| | 0 | 158 | | { |
| | 0 | 159 | | await _service.Delete(id); |
| | 0 | 160 | | return Ok(); |
| | 0 | 161 | | } |
| | | 162 | | } |
| | | 163 | | } |