< Summary

Class:WinSolutions.Sveta.Server.Services.Implements.BarcodeService
Assembly:WinSolutions.Sveta.Server
File(s):/opt/dev/sveta_api_build/WinSolutions.Sveta.Server/Services/Implements/BarcodeService.cs
Covered lines:36
Uncovered lines:11
Coverable lines:47
Total lines:91
Line coverage:76.5% (36 of 47)
Covered branches:10
Total branches:16
Branch coverage:62.5% (10 of 16)

Metrics

MethodLine coverage Branch coverage
.ctor(...)100%100%
CreateBarcode()100%66.66%
GetBarcode()100%100%
GetOrCreateBarcode()100%66.66%
GetBarcode()100%100%
UpdateGoodBarcodes()0%0%
UpdateGoodBarcode()0%100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Threading.Tasks;
 5using Microsoft.EntityFrameworkCore;
 6using Microsoft.Extensions.Logging;
 7using WinSolutions.Sveta.Server.Data.DataModel.Contexts;
 8using WinSolutions.Sveta.Server.Data.DataModel.Entities;
 9using WinSolutions.Sveta.Server.Services.Interfaces;
 10using WinSolutions.Sveta.Common;
 11using WinSolutions.Sveta.Server.Data.DataModel.Kinds;
 12using WinSolutions.Sveta.Common.Extensions;
 13
 14namespace WinSolutions.Sveta.Server.Services.Implements
 15{
 16    public class BarcodeService : SvetaServiceBase, IBarcodeService
 17    {
 18        private readonly SvetaDbContext _db;
 19        private readonly ILogger<BarcodeService> _logger;
 20
 21        public BarcodeService(SvetaDbContext db, ILogger<BarcodeService> logger, IAuthenticationService authenticationSe
 1022            : base(authenticationService)
 1023        {
 1024            _db = db;
 1025            _logger = logger;
 1026        }
 27
 28        public async Task<BarCode> CreateBarcode(string code)
 1029        {
 1030            if(_db.BarCodes.Where(x => !x.IsDeleted && x.Code.ToLower() == code.NormalizeName().ToLower()).Any())
 231            {
 232                throw new Exception($"Баркод '{code}' уже существует");
 33            }
 34
 835            var barcode = new BarCode()
 836            {
 837                Code = code
 838            };
 839            await _db.BarCodes.AddAsync(barcode);
 840            await _db.SaveChangesAsync(CurrentUserId);
 41
 842            return barcode;
 843        }
 44
 45        public async Task<BarCode> GetBarcode(long id)
 446        {
 447            return await _db.BarCodes.Where(e => !e.IsDeleted).FirstAsync(b=>b.Id == id);
 248        }
 49
 50        public async Task<BarCode> GetOrCreateBarcode(string code)
 251        {
 252            BarCode barcode = _db.BarCodes.FirstOrDefault(d => d.Code.ToUpper().Equals(code.NormalizeName().ToUpper()));
 253            if (barcode == null)
 154            {
 155                barcode = new BarCode
 156                {
 157                    Code = code
 158                };
 159                await _db.BarCodes.AddAsync(barcode);
 160                await _db.SaveChangesAsync(CurrentUserId);
 161            }
 62
 263            return barcode;
 264        }
 65
 66        public async Task<BarCode> GetBarcode(string code) =>
 467            await _db.BarCodes
 468                .FirstOrDefaultAsync(x => !x.IsDeleted && x.Code.ToUpper() == code.NormalizeName().ToUpper());
 69
 70        public async Task UpdateGoodBarcodes(List<GoodBarcode> goodBarcodes)
 071        {
 072            for (int i = 0; i < goodBarcodes.Count; i++)
 073            {
 074                _db.GoodBarcodes.Update(goodBarcodes[i]);
 075            }
 76
 077            await _db.SaveChangesAsync(CurrentUserId);
 078        }
 79
 80        public async Task<List<BarCode>> FindBarcodesByCodes(IEnumerable<string> codes)
 81        {
 782            var cds = codes.Select(x => x.NormalizeName().ToLower()).ToList();
 83            return await _db.BarCodes.Where(x => !x.IsDeleted && cds.Contains(x.Code.ToLower())).ToListAsync();
 84        }
 85        public async Task UpdateGoodBarcode(GoodBarcode goodBarcodes)
 086        {
 087            _db.GoodBarcodes.Update(goodBarcodes);
 088            await _db.SaveChangesAsync(CurrentUserId);
 089        }
 90    }
 91}