< Summary

Class:WinSolutions.Sveta.Server.Services.Implements.UploadService
Assembly:WinSolutions.Sveta.Server
File(s):/opt/dev/sveta_api_build/WinSolutions.Sveta.Server/Services/Implements/UploadService.cs
Covered lines:0
Uncovered lines:189
Coverable lines:189
Total lines:264
Line coverage:0% (0 of 189)
Covered branches:0
Total branches:22
Branch coverage:0% (0 of 22)

Metrics

MethodLine coverage Branch coverage
.ctor(...)0%100%
GetUpload()0%100%
GetGoodsFromUpload()0%0%
GetGoodsFromUploadCount()0%100%
GetAllPhotos()0%100%
UpdatePhotos()0%0%
PrepareUploadGoodsQuery(...)0%0%
UploadGoods(...)0%0%
UploadPhotos(...)0%100%
RollbackGoods(...)0%100%
CommitGoods(...)0%100%
GetReport(...)0%100%
UploadRests(...)0%0%
UploadDepartmentGoodSettings(...)0%0%
GetRestsTemplate(...)0%100%
GetDepartmentGoodSettingsTemplate(...)0%100%
GetGoodsTemplate(...)0%100%
Load(...)0%100%

File(s)

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

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using Microsoft.Extensions.Logging;
 3using System;
 4using System.Collections.Generic;
 5using System.IO;
 6using System.Linq;
 7using System.Text;
 8using System.Threading.Tasks;
 9using Microsoft.EntityFrameworkCore.Internal;
 10using WinSolutions.Sveta.Server.Data.DataLoading;
 11using WinSolutions.Sveta.Server.Data.DataLoading.Loaders;
 12using WinSolutions.Sveta.Server.Data.DataLoading.Parsers;
 13using WinSolutions.Sveta.Server.Data.DataLoading.Records;
 14using WinSolutions.Sveta.Server.Data.DataModel.Contexts;
 15using WinSolutions.Sveta.Server.Data.DataModel.Entities;
 16using WinSolutions.Sveta.Server.Data.DataModel.Kinds;
 17using WinSolutions.Sveta.Server.Services.Interfaces;
 18using WinSolutions.Sveta.Common;
 19
 20namespace WinSolutions.Sveta.Server.Services.Implements
 21{
 22    public class UploadService : SvetaServiceBase, IUploadService
 23    {
 24        private readonly IGoodService _goodService;
 25        private readonly IBarcodeService _barcodeService;
 26        private readonly ILogger<GoodService> logger;
 27        private readonly SvetaDbContext context;
 28        private readonly ICountryService _countryService;
 29        private readonly SvetaDbContext _db;
 30        private readonly IPhotoService _photoService;
 31
 32        public UploadService(SvetaDbContext context, ILogger<GoodService> logger, IAuthenticationService authenticationS
 33            IBarcodeService barcodeService, ICountryService countryService, SvetaDbContext db, IPhotoService photoServic
 034            : base(authenticationService)
 035        {
 036            this.context = context;
 037            this.logger = logger;
 038            _goodService = goodService;
 039            _barcodeService = barcodeService;
 040            _countryService = countryService;
 041            _db = db;
 042            _photoService = photoService;
 043        }
 44
 45        public async Task<Upload> GetUpload(long id)
 046        {
 047            return await _db.Uploads
 048                .FirstOrDefaultAsync(x => !x.IsDeleted && x.Id == id);
 049        }
 50
 51        public async Task<List<Good>> GetGoodsFromUpload(Upload upload, int page, int limit, string filter, string sort)
 052        {
 053            var qry = PrepareUploadGoodsQuery(upload, filter);
 054            switch ((sort ?? "").ToLower())
 55            {
 056                case "name": qry = qry.OrderBy(x => x.Name); break;
 057                case "name|desc": qry = qry.OrderByDescending(x => x.Name); break;
 058                case "brandName": qry = qry.OrderBy(x => x.Brand.Name); break;
 059                case "brandName|desc": qry = qry.OrderByDescending(x => x.Brand.Name); break;
 060                default: qry = qry.OrderBy(x => x.Name); break;
 61            }
 062            qry = qry.Skip(page * limit).Take(limit);
 063            return await qry.ToListAsync();
 064        }
 65
 66        public async Task<int> GetGoodsFromUploadCount(Upload upload, string filter)
 067        {
 068            var qry = PrepareUploadGoodsQuery(upload, filter);
 069            return await qry.CountAsync();
 070        }
 71
 072        public async Task<List<Photo>> GetAllPhotos() => await _db.Photos.Where(x => !x.IsDeleted).ToListAsync();
 73
 74        public async Task UpdatePhotos(List<Photo> photos)
 075        {
 076            if (photos.Count == 0)
 077                return;
 078            _db.Photos.UpdateRange(photos);
 079            await _db.SaveChangesAsync(CurrentUserId);
 080        }
 81
 82        IQueryable<Good> PrepareUploadGoodsQuery(Upload upload, string filter)
 083        {
 084            var ids = _db.UploadItems
 085                .Where(x => x.UploadId == upload.Id && x.EntityName == "Good")
 086                .Select(x => x.EndityId).ToList();
 87
 088            var query = _db.Goods
 089                .Include(x => x.DefaultBarCode)
 090                .Include(x => x.GoodBarcodes)
 091                .ThenInclude(barcode => barcode.BarCode)
 092                .Include(x => x.Brand)
 093                .Include(x => x.Photos)
 094                .Where(x => ids.Contains(x.Id));
 95
 096            if (!string.IsNullOrWhiteSpace(filter))
 097            {
 098                query = query.Where(x => x.Name.ToUpper().Contains(filter.ToUpper())
 099                                         || x.DefaultBarCode.Code.ToUpper().Contains(filter.ToUpper())
 0100                                         || x.GoodBarcodes.Any(g => g.BarCode.Code.ToUpper().Contains(filter.ToUpper()))
 0101    }
 102
 0103            return query;
 0104        }
 105
 106        public Task<LoadingResult> UploadGoods(Stream stream, Action<BaseRecord> beforeAddAction, Action<Photo> getPhoto
 0107        {
 0108            return Task.Factory.StartNew(() =>
 0109            {
 0110                IDataLoader loader = null;
 0111
 0112                if (fileType == "csv")
 0113                {
 0114                    loader = new GoodsLoader<CsvParser<ExternalGood>>(CurrentUserId)
 0115                    {
 0116                        GoodService = _goodService,
 0117                        BarcodeService = _barcodeService,
 0118                        CountryService = _countryService
 0119                    };
 0120                }
 0121                else
 0122                {
 0123                    loader = new GoodsLoader<ExcelParser<ExternalGood>>(CurrentUserId)
 0124                    {
 0125                        GoodService = _goodService,
 0126                        BarcodeService = _barcodeService,
 0127                        CountryService = _countryService
 0128                    };
 0129                }
 0130
 0131                return Load(loader, stream);
 0132            });
 0133        }
 134
 135
 136        public Task<LoadingResult> UploadPhotos(Stream stream,Action<BaseRecord> beforeAddAction)
 0137        {
 0138            return Task.Factory.StartNew(() =>
 0139            {
 0140                var loader = new ImagePackageLoader();
 0141                loader.PhotoService = _photoService;
 0142
 0143                loader.BeforeAddRecordAction = beforeAddAction;
 0144
 0145                return loader.Load(context, stream);
 0146            });
 0147        }
 148
 149
 150        public Task RollbackGoods(long id)
 0151        {
 0152            return Task.Factory.StartNew(() =>
 0153            {
 0154
 0155                IDataLoader loader = new GoodsLoader<CsvParser<ExternalGood>> (CurrentUserId);
 0156                loader.Rollback(context, id);
 0157            });
 0158        }
 159
 160        public Task CommitGoods(long id)
 0161        {
 0162            return Task.Factory.StartNew(() =>
 0163            {
 0164
 0165                IDataLoader loader = new GoodsLoader<CsvParser<ExternalGood>> (CurrentUserId);
 0166                loader.Commit(context, id);
 0167            });
 0168        }
 169
 170        public void GetReport(long id,Stream stream)
 0171        {
 0172            var u = context.Uploads.Where(x => x.Id == id).Select(x => x.ResultFile).FirstOrDefault();
 0173            stream.Write(u, 0, u.Length);
 0174            stream.Position = 0;
 0175        }
 176
 177        public Task<LoadingResult> UploadRests(Stream stream, long departmentId, string fileType)
 0178        {
 0179            return Task.Factory.StartNew(() =>
 0180            {
 0181                IDataLoader loader = null;
 0182
 0183                if (fileType == "csv")
 0184                {
 0185                    loader = new RestLoader<CsvParser<ExternalRest>>(CurrentUserId)
 0186                    {
 0187                        DepartmentId = departmentId,
 0188                        GoodService = _goodService
 0189                    };
 0190                }
 0191                else
 0192                {
 0193                    loader = new RestLoader<ExcelParser<ExternalRest>>(CurrentUserId)
 0194                    {
 0195                        DepartmentId = departmentId,
 0196                        GoodService = _goodService
 0197                    };
 0198                }
 0199
 0200                return Load(loader, stream);
 0201            });
 0202        }
 203
 204        public Task<LoadingResult> UploadDepartmentGoodSettings(Stream stream, long departmentId, string fileType)
 0205        {
 0206            return Task.Factory.StartNew(() =>
 0207            {
 0208                IDataLoader loader = null;
 0209
 0210                if (fileType == "csv")
 0211                {
 0212                    loader = new DepartmentGoodSettingLoader<CsvParser<ExternalDepartmentGoodSetting>>(CurrentUserId)
 0213                    {
 0214                        DepartmentId = departmentId,
 0215                        GoodService = _goodService
 0216                    };
 0217                }
 0218                else
 0219                {
 0220                    loader = new DepartmentGoodSettingLoader<ExcelParser<ExternalDepartmentGoodSetting>>(CurrentUserId)
 0221                    {
 0222                        DepartmentId = departmentId,
 0223                        GoodService = _goodService
 0224                    };
 0225                }
 0226
 0227                return Load(loader, stream);
 0228            });
 0229        }
 230
 231        public Task GetRestsTemplate(Stream stream, string fileType)
 0232        {
 0233            return Task.Factory.StartNew(() =>
 0234            {
 0235                var loader = new RestLoader<CsvParser<ExternalRest>> (CurrentUserId);
 0236                loader.GetTemplate(context, stream, fileType == "csv");
 0237            });
 0238        }
 239
 240        public Task GetDepartmentGoodSettingsTemplate(Stream stream, string fileType)
 0241        {
 0242            return Task.Factory.StartNew(() =>
 0243            {
 0244                IDataLoader loader = new DepartmentGoodSettingLoader<CsvParser<ExternalDepartmentGoodSetting>>(CurrentUs
 0245                loader.GetTemplate(context, stream, fileType == "csv");
 0246            });
 0247        }
 248
 249        public Task GetGoodsTemplate(Stream stream, string fileType)
 0250        {
 0251            return Task.Factory.StartNew(() =>
 0252            {
 0253                IDataLoader loader = new GoodsLoader<CsvParser<ExternalGood>> (CurrentUserId);
 0254                loader.GetTemplate(context, stream, fileType == "csv");
 0255            });
 0256        }
 257
 258        private LoadingResult Load(IDataLoader loader, Stream stream)
 0259        {
 0260            var res = loader.Load(context,stream);
 0261            return res;
 0262        }
 263    }
 264}