< Summary

Class:WinSolutions.Sveta.Server.Services.Implements.TaxSystemService
Assembly:WinSolutions.Sveta.Server
File(s):/opt/dev/sveta_api_build/WinSolutions.Sveta.Server/Services/Implements/TaxSystemService.cs
Covered lines:0
Uncovered lines:55
Coverable lines:55
Total lines:87
Line coverage:0% (0 of 55)
Covered branches:0
Total branches:36
Branch coverage:0% (0 of 36)

Metrics

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

File(s)

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

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using Microsoft.Extensions.Logging;
 3using System;
 4using System.Collections.Generic;
 5using System.Linq;
 6using System.Linq.Expressions;
 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.Common;
 12
 13namespace WinSolutions.Sveta.Server.Services.Implements
 14{
 15    public class TaxSystemService : SvetaServiceBase, ITaxSystemService
 16    {
 17        private readonly SvetaDbContext _db;
 18        private readonly ILogger<TaxSystemService> _logger;
 019        public TaxSystemService(SvetaDbContext db, ILogger<TaxSystemService> logger, IAuthenticationService authenticati
 020        {
 021            _db = db;
 022            _logger = logger;
 023        }
 24        public async Task CreateTaxSystem(TaxSystem taxSystem)
 025        {
 026            if (taxSystem.RecState != null)
 027                _db.Entry(taxSystem.RecState).State = taxSystem.RecState.Id == 0 ? EntityState.Detached : EntityState.Un
 028            await _db.AddAsync(taxSystem);
 029            await _db.SaveChangesAsync(CurrentUserId);
 030        }
 31
 32        public async Task DeleteTaxSystem(long id)
 033        {
 034            var taxSystem = await _db.TaxSystems
 035                .Where(d => !d.IsDeleted)
 036                .FirstAsync(t=>t.Id == id);
 037            if (taxSystem == null)
 038                throw new KeyNotFoundException($"Запись #{id} не найдена");
 039            taxSystem.IsDeleted = true;
 040            await _db.SaveChangesAsync(CurrentUserId);
 041        }
 42
 043        public async Task<TaxSystem> GetTaxSystem(long id) => await _db.TaxSystems
 044            .Include(d => d.RecState)
 045            .FirstOrDefaultAsync(d => !d.IsDeleted && d.Id == id);
 46
 47        public async Task<List<TaxSystem>> GetTaxSystems(int page, int limit, string filter, string sort)
 048        {
 049            string[] sortItems = sort?.Split("|");
 050            string sortColumn = sortItems?.ElementAtOrDefault(0);
 051            string sortOrder = sortItems?.ElementAtOrDefault(1);
 52
 053            var tax = _db.TaxSystems.AsNoTracking()
 054                .Include(d => d.RecState)
 055                .Where(d => !d.IsDeleted);
 056            if (!string.IsNullOrWhiteSpace(filter))
 057            {
 058                tax = tax.Where(d => d.Name.ToUpper().Contains(filter.ToUpper()) || d.Code.ToUpper().Contains(filter.ToU
 059            }
 060            Expression<Func<TaxSystem, object>> sortExpression = sortColumn?.ToLower() switch
 061            {
 062                "code" => e => e.Code,
 063                _ => e => e.Name,
 064            };
 065            if (sortOrder != default && sortOrder.Equals("desc", StringComparison.OrdinalIgnoreCase))
 066                tax = tax.OrderByDescending(sortExpression);
 67            else
 068                tax = tax.OrderBy(sortExpression);
 069            return await tax.Skip((page < 2 ? 0 : page - 1) * limit).Take(limit).ToListAsync();
 070        }
 71
 072        public async Task<bool> ItemsExists(long id) => await _db.TaxSystems
 073            .Where(e => e.Id == id)
 074            .Where(d => !d.IsDeleted)
 075            .AnyAsync();
 76
 77        public async Task UpdateTaxSystem(TaxSystem data)
 078        {
 079            if (!(await ItemsExists(data.Id)))
 080            {
 081                throw new ArgumentException($"Record #{data.Id} not found");
 82            }
 083            _db.TaxSystems.Update(data);
 084            await _db.SaveChangesAsync(CurrentUserId);
 085        }
 86    }
 87}