< Summary

Class:WinSolutions.Sveta.Server.Services.Implements.CommonUserService
Assembly:WinSolutions.Sveta.Server
File(s):/opt/dev/sveta_api_build/WinSolutions.Sveta.Server/Services/Implements/CommonUserService.cs
Covered lines:10
Uncovered lines:25
Coverable lines:35
Total lines:104
Line coverage:28.5% (10 of 35)
Covered branches:4
Total branches:12
Branch coverage:33.3% (4 of 12)

Metrics

MethodLine coverage Branch coverage
.ctor(...)100%100%
PrepareUsers()0%100%
GetUser()0%100%
Create()100%66.66%
CanSendToTelegram()0%0%
UserExists()0%100%
Update()0%0%
GetRoles()0%100%

File(s)

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

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using Microsoft.Extensions.Logging;
 3using System;
 4using System.Collections.Generic;
 5using System.Linq;
 6using System.Text;
 7using System.Threading.Tasks;
 8using WinSolutions.Sveta.Server.Data.DataModel.Contexts;
 9using WinSolutions.Sveta.Server.Data.DataModel.Entities;
 10using WinSolutions.Sveta.Server.Services.Interfaces;
 11using WinSolutions.Sveta.Server.Domain;
 12using WinSolutions.Sveta.Common;
 13using CrmVtbc;
 14using WinSolutions.Sveta.Server.Data.DataModel.Kinds;
 15
 16namespace WinSolutions.Sveta.Server.Services.Implements
 17{
 18    public class CommonUserService : ICommonUserService
 19    {
 20        readonly SvetaDbContext _db;
 21
 18622        public CommonUserService(SvetaDbContext db)
 18623        {
 18624            _db = db;
 18625        }
 26
 27        /// <summary>
 28        ///предварительная выборка юзеров
 29        /// </summary>
 30        /// <returns></returns>
 031        private IQueryable<User> PrepareUsers() => _db.Users
 032           .Include(d => d.Contragent)
 033           .ThenInclude(Contragent => Contragent.ContragentsKind)
 034           .Include(d => d.Contragent)
 035           .ThenInclude(Contragent => Contragent.Owner)
 036           .Include(d => d.RecState)
 037           .Where(d => !d.IsDeleted)
 038           .AsQueryable();
 39
 40        /// <summary>
 41        /// получает юзера по extrenalKey
 42        /// </summary>
 43        /// <param name="tid">extrenalKey</param>
 44        /// <returns>юзер</returns>
 045        public async Task<User> GetUser(Guid tid) => await PrepareUsers().FirstOrDefaultAsync(x => x.ExternalKey.Equals(
 46
 47        /// <summary>
 48        /// создает юзера
 49        /// </summary>
 50        /// <param name="data">юзер</param>
 51        /// <param name="userId">id юзера, от кого выполняется</param>
 52        /// <returns></returns>
 53        public async Task Create(User data, long userId)
 57054        {
 57055            if (data.Contragent != null)
 20256                _db.Entry(data.Contragent).State = EntityState.Unchanged;
 57057            await _db.Users.AddAsync(data);
 57058            await _db.SaveChangesAsync(userId);
 57059        }
 60
 61        /// <summary>
 62        /// Проверяет можно ли по данному хэштегу отправлять сообещния в телеграмм
 63        /// </summary>
 64        /// <param name="hashtag">хэштег</param>
 65        /// <returns>значение true/false</returns>
 66        public async Task<bool> CanSendToTelegram(string hashtag)
 067        {
 068            var result = await _db.TelegramNotifications.Where(x=>!x.IsDeleted).FirstOrDefaultAsync(x => x.HashTag.ToLow
 069            return result == null ? false : result.IsActive;
 070        }
 71
 72        /// <summary>
 73        /// проверяет существование юзера
 74        /// </summary>
 75        /// <param name="id">id юзера</param>
 76        /// <returns>значение true/false</returns>
 077        public async Task<bool> UserExists(long id) => await _db.Users
 078            .Where(e => e.Id == id)
 079            .Where(d => !d.IsDeleted)
 080            .AnyAsync();
 81
 82        /// <summary>
 83        /// обновляет юзера
 84        /// </summary>
 85        /// <param name="data">юзер</param>
 86        /// <param name="userId">id юзера, от кого выполняется</param>
 87        /// <returns></returns>
 88        public async Task Update(User data, long userId)
 089        {
 090            if (!(await UserExists(data.Id)))
 091            {
 092                throw new ArgumentException($"Record #{data.Id} not found");
 93            }
 094            _db.Users.Update(data);
 095            await _db.SaveChangesAsync(userId);
 096        }
 97
 98        /// <summary>
 99        ///получает список ролей
 100        /// </summary>
 101        /// <returns>список ролей</returns>
 0102        public async Task<List<Roles>> GetRoles() => await _db.Roles.Where(x => !x.IsDeleted).ToListAsync();
 103    }
 104}