< Summary

Class:WinSolutions.Sveta.Server.Services.Implements.TelegramNotificationService
Assembly:WinSolutions.Sveta.Server
File(s):/opt/dev/sveta_api_build/WinSolutions.Sveta.Server/Services/Implements/TelegramNotificationService.cs
Covered lines:12
Uncovered lines:20
Coverable lines:32
Total lines:100
Line coverage:37.5% (12 of 32)
Covered branches:1
Total branches:8
Branch coverage:12.5% (1 of 8)

Metrics

MethodLine coverage Branch coverage
.ctor(...)100%100%
GetNotifications()0%0%
PrepareNotifications()100%100%
GetNotification()0%100%
GetNotification()0%100%
CanSendToTelegram()100%50%
UpdateNotifications()0%100%

File(s)

/opt/dev/sveta_api_build/WinSolutions.Sveta.Server/Services/Implements/TelegramNotificationService.cs

#LineLine coverage
 1using Clave.Expressionify;
 2using Microsoft.EntityFrameworkCore;
 3using Microsoft.Extensions.Logging;
 4using System.Collections.Generic;
 5using System.Linq;
 6using WinSolutions.Sveta.Server.Data.DataModel.Extensions;
 7using System.Threading.Tasks;
 8using WinSolutions.Sveta.Common;
 9using WinSolutions.Sveta.Server.Data.DataModel.Contexts;
 10using WinSolutions.Sveta.Server.Data.DataModel.Entities;
 11using WinSolutions.Sveta.Server.Data.DataModel.Kinds;
 12using WinSolutions.Sveta.Server.Domain;
 13using WinSolutions.Sveta.Server.Services.Interfaces;
 14using System.Runtime.CompilerServices;
 15
 16namespace WinSolutions.Sveta.Server.Services.Implements
 17{
 18    public class TelegramNotificationService : SvetaServiceBase, ITelegramNotificationService
 19    {
 20        private readonly SvetaDbContext _db;
 21        private readonly IAuthenticationService _authenticationService;
 22        private readonly ILogger<TelegramNotificationService> _logger;
 23
 24        public TelegramNotificationService(SvetaDbContext db, ILogger<TelegramNotificationService> logger, IAuthenticati
 18525            : base(authenticationService)
 18526        {
 18527            _authenticationService = authenticationService;
 18528            _db = db;
 18529            _logger = logger;
 18530        }
 31
 32        /// <summary>
 33        ///получает записи с пагинацией и фильтрацией
 34        /// </summary>
 35        /// <param name="page">номер страницы</param>
 36        /// <param name="limit">размер страницы</param>
 37        /// <param name="filter">фильтр по хэштегу</param>
 38        /// <param name="sortByHashTagDesc">сортировка по хэштегу</param>
 39        /// <param name="activeOnly">только активные</param>
 40        /// <returns></returns>
 41        public async Task<PaginatedData<List<TelegramNotification>>> GetNotifications(int page, int limit, string filter
 042        {
 043            var notifies = PrepareNotifications().AsNoTracking();
 44            // Тотал записей для смертных считаем после прмиенения ограничения роли
 045            int total = await notifies?.CountAsync();
 046            notifies = notifies.Where(x => !activeOnly || x.IsActive)
 047                .Where(x => string.IsNullOrWhiteSpace(filter) || x.HashTag.ToUpper().Contains(filter.ToUpper()));
 048            notifies = !sortByHashTagDesc ? notifies.OrderBy(x => x.HashTag) : notifies.OrderByDescending(x => x.HashTag
 049            int filtered = await notifies?.CountAsync();
 050            return new PaginatedData<List<TelegramNotification>>
 051            {
 052                Result = await notifies.Skip(page * limit).Take(limit).ToListAsync(),
 053                TotalCount = total,
 054                TotalFilteredCount = filtered
 055            };
 056        }
 57
 58        /// <summary>
 59        /// предварительная выборка
 60        /// </summary>
 61        /// <returns></returns>
 56062        private IQueryable<TelegramNotification> PrepareNotifications() => _db.TelegramNotifications
 56063            .Where(x => !x.IsDeleted).AsQueryable();
 64
 65        /// <summary>
 66        /// Получить запись
 67        /// </summary>
 68        /// <param name="hashtag">хэштег</param>
 69        /// <returns></returns>
 070        public async Task<TelegramNotification> GetNotification(string hashtag) => await PrepareNotifications().FirstOrD
 71        /// <summary>
 72        /// Получить запись
 73        /// </summary>
 74        /// <param name="id">id записи</param>
 75        /// <returns></returns>
 076        public async Task<TelegramNotification> GetNotification(long id) => await PrepareNotifications().FirstOrDefaultA
 77        /// <summary>
 78        /// проверяет можно ли по указанному хэштегу отправлять сообщения в телеграм
 79        /// </summary>
 80        /// <param name="hashtag">хэштег</param>
 81        /// <returns></returns>
 82        public async Task<bool> CanSendToTelegram(string hashtag)
 56083        {
 56084            var result = await PrepareNotifications().FirstOrDefaultAsync(x => x.HashTag.ToLower() == hashtag.ToLower())
 56085            return result == null ? false : result.IsActive;
 56086        }
 87
 88        /// <summary>
 89        /// обновить запись
 90        /// </summary>
 91        /// <param name="data">объект TelegramNotification</param>
 92        /// <returns></returns>
 93        public async Task UpdateNotifications(TelegramNotification data)
 094        {
 095            _db.TelegramNotifications.Update(data);
 096            await _db.SaveChangesAsync(CurrentUserId);
 097        }
 98
 99    }
 100}