< Summary

Class:SVETA.Api.Controllers.TelegramNotificationsController
Assembly:SVETA.Api
File(s):/opt/dev/sveta_api_build/SVETA.Api/Controllers/TelegramNotificationsController.cs
Covered lines:0
Uncovered lines:46
Coverable lines:46
Total lines:132
Line coverage:0% (0 of 46)
Covered branches:0
Total branches:20
Branch coverage:0% (0 of 20)

Metrics

MethodLine coverage Branch coverage
.ctor(...)0%100%
GetNotifications()0%0%
GetNotify()0%0%
DeactivateNotify()0%0%
ActivateNotify()0%0%

File(s)

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

#LineLine coverage
 1using WinSolutions.Sveta.Common.Extensions;
 2using System;
 3using System.Collections.Generic;
 4using System.Linq;
 5using System.Threading.Tasks;
 6using Microsoft.AspNetCore.Mvc;
 7using Microsoft.Extensions.Logging;
 8using WinSolutions.Sveta.Server.Data.DataModel.Entities;
 9using Microsoft.AspNetCore.Authorization;
 10using SVETA.Api.Data.Domain;
 11using WinSolutions.Sveta.Server.Services.Interfaces;
 12using SVETA.Api.Data.DTO;
 13using SVETA.Api.Helpers.Authorize;
 14using WinSolutions.Sveta.Server.Data.DataModel.Kinds;
 15using Swashbuckle.AspNetCore.Annotations;
 16using WinSolutions.Sveta.Server.Domain;
 17using WinSolutions.Sveta.Common;
 18
 19namespace SVETA.Api.Controllers
 20{
 21    [Authorize]
 22    [Route("api/v1/TelegramNotifications")]
 23    [ApiController]
 24    public class TelegramNotificationsController : SvetaController
 25    {
 26        const string _routeUrl = "api/v1/TelegramNotifications";
 27        readonly ITelegramNotificationService _service;
 28        readonly IDirectoriesService _dirService;
 29        readonly ILogger<TelegramNotificationsController> _logger;
 030        public TelegramNotificationsController(ITelegramNotificationService service, ILogger<TelegramNotificationsContro
 031        {
 032            _service = service;
 033            _logger = logger;
 034            _dirService = dirService;
 035        }
 36
 37        /// <summary>
 38        /// Возвращает все хештэги для телеграмма
 39        /// </summary>
 40        /// <remarks>author i.rebenok</remarks>
 41        /// <param name="page">Любое значение ниже нуля изменится на 1, пагинация: номер страницы</param>
 42        /// <param name="limit">Любое значение ниже нуля изменится на 10, пагинация: размер страницы</param>
 43        /// <param name="filter">фильтр по значимым полям: хештэг</param>
 44        /// <param name="sortByHashTagDesc">сортировать по хештэгу DESC - true or false</param>
 45        /// <param name="active">true, если выводить только активные хештэги. По умолчанию false</param>
 46        [HttpGet("")]
 47        [SwaggerResponse(200, "Успешно", typeof(IEnumerable<TelegramNotifyResponseDTO>))]
 48        [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))]
 49        [SwaggerResponse(403, "Не разрешено для этого пользователя", typeof(ErrorDTO))]
 50        [Authorize(Roles = Role.SystemAdmin)]
 51        public async Task<IActionResult> GetNotifications(int page = 1, int limit = 10, string filter = null, bool sortB
 052        {
 053            filter = filter.NormalizeName();
 054            page = page < 1 ? 1 : page;
 055            limit = limit < 1 ? 10 : limit;
 056            var result = await _service.GetNotifications(page - 1, limit, filter, sortByHashTagDesc, active);
 057            var param = $"sortByHashTagDesc={sortByHashTagDesc}&active={active}";
 058            var response = new BaseResponseDTO<TelegramNotifyResponseDTO>(_routeUrl, page, limit, result.TotalFilteredCo
 059            {
 060                Data = result.Result.Select(x => new TelegramNotifyResponseDTO(x)).ToList(),
 061            };
 062            return Ok(response);
 063        }
 64
 65        /// <summary>
 66        /// Получить хештэг по ID
 67        /// </summary>
 68        /// <remarks>author i.rebenok</remarks>
 69        /// <param name="id">id хештэга</param>
 70        [HttpGet("{id}")]
 71        [SwaggerResponse(200, "Успешно", typeof(TelegramNotifyResponseDTO))]
 72        [SwaggerResponse(404, "Нет записей", typeof(ErrorDTO))]
 73        [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))]
 74        [SwaggerResponse(403, "Не разрешено для этого пользователя", typeof(ErrorDTO))]
 75        [Authorize(Roles = Role.SystemAdmin)]
 76        public async Task<IActionResult> GetNotify([SwaggerParameter(Required = true)] long id)
 077        {
 078            var result = await _service.GetNotification(id);
 079            if (result == null)
 080                return NotFoundResult($"Хештэг с id={id} не найден");
 081            return Ok(new TelegramNotifyResponseDTO(result));
 082        }
 83
 84        /// <summary>
 85        /// Деактивирует хештэг по id
 86        /// </summary>
 87        /// <remarks>author i.rebenok</remarks>
 88        /// <param name="id">id хештэга</param>
 89        [HttpPatch("{id}/Deactivate")]
 90        [SwaggerResponse(200, "Успешно", typeof(EmptyResult))]
 91        [SwaggerResponse(404, "Нет записей", typeof(ErrorDTO))]
 92        [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))]
 93        [SwaggerResponse(403, "Не разрешено для этого пользователя", typeof(ErrorDTO))]
 94        [Authorize(Roles = Role.SystemAdmin)]
 95        public async Task<IActionResult> DeactivateNotify([SwaggerParameter(Required = true)] long id)
 096        {
 097            var result = await _service.GetNotification(id);
 098            if (result == null)
 099                return NotFoundResult($"Хештэг с id={id} не найден");
 0100            if (result.IsActive)
 0101            {
 0102                result.IsActive = false;
 0103                await _service.UpdateNotifications(result);
 0104            }
 0105            return Ok();
 0106        }
 107
 108        /// <summary>
 109        /// Активирует хештэг по id
 110        /// </summary>
 111        /// <remarks>author i.rebenok</remarks>
 112        /// <param name="id">id хештэга</param>
 113        [HttpPatch("{id}/Activate")]
 114        [SwaggerResponse(200, "Успешно", typeof(EmptyResult))]
 115        [SwaggerResponse(404, "Нет записей", typeof(ErrorDTO))]
 116        [SwaggerResponse(500, "Ошибка на стороне сервера", typeof(ErrorDTO))]
 117        [SwaggerResponse(403, "Не разрешено для этого пользователя", typeof(ErrorDTO))]
 118        [Authorize(Roles = Role.SystemAdmin)]
 119        public async Task<IActionResult> ActivateNotify([SwaggerParameter(Required = true)] long id)
 0120        {
 0121            var result = await _service.GetNotification(id);
 0122            if (result == null)
 0123                return NotFoundResult($"Хештэг с id={id} не найден");
 0124            if (!result.IsActive)
 0125            {
 0126                result.IsActive = true;
 0127                await _service.UpdateNotifications(result);
 0128            }
 0129            return Ok();
 0130        }
 131    }
 132}