< Summary

Information
Class: Services.DataServices.StockDataService
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/DataServices/StockDataService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 35
Coverable lines: 35
Total lines: 79
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 16
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GetStocksDataByOrder()0%272160%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/DataServices/StockDataService.cs

#LineLine coverage
 1using Microsoft.Extensions.Caching.Memory;
 2using Newtonsoft.Json.Linq;
 3using OzonDomains;
 4using OzonDomains.Models;
 5using OzonRepositories.Data;
 6using Services.ParserServices;
 7using Services.ParserServices.HelpDictEnum;
 8using Services.CacheServices.Cache;
 9
 10namespace Services.DataServices;
 11
 12public class StockDataService
 13{
 14    private readonly CsvUrlParser _urlParser;
 15    private readonly StocksCaster _stocksCaster;
 16    private readonly AppCache _appCache;
 17    private readonly SupplierDataServices _supplierDataServices;
 18
 019    public StockDataService(CsvUrlParser urlParser,
 020                            StocksCaster stocksCaster,
 021                            AppCache appCache,
 022                            SupplierDataServices supplierDataServices)
 23    {
 024        _urlParser = urlParser;
 025        _stocksCaster = stocksCaster;
 026        _appCache = appCache;
 027        _supplierDataServices = supplierDataServices;
 028    }
 29
 30    public async Task<List<StocksData>> GetStocksDataByOrder(Order order)
 31    {
 032        List<StocksData> stocksDataResult = new();
 033        if (order.Article != null)
 34        {
 35            try
 36            {
 037                List<Supplier> suppliers = await _supplierDataServices.GetSuppliers();
 038                var cache = _appCache.GetCache();
 039                foreach (Supplier supplier in suppliers)
 40                {
 041                    if (supplier.CsvUrl != null)
 42                    {
 043                        string cacheKey = supplier.CsvUrl;
 044                        if (!cache.TryGetValue(cacheKey, out JArray? jsonStock) || jsonStock == null)
 45                        {
 046                            jsonStock = await _urlParser.ReadFileFromUrl(supplier.CsvUrl);
 47
 048                            if (jsonStock != null)
 49                            {
 050                                var cacheEntryOptions = new MemoryCacheEntryOptions
 051                                {
 052                                    AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10)
 053                                };
 54
 055                                cache.Set(cacheKey, jsonStock, cacheEntryOptions);
 56                            }
 57                        }
 58                        else
 59                        {
 060                            Console.WriteLine($"Stock found in cache [{cacheKey}]");
 61                        }
 62
 063                        if (jsonStock == null)
 64                            continue;
 65
 066                        var searchResult = _stocksCaster.Search(jsonStock, order.Article);
 067                        stocksDataResult.AddRange(await _stocksCaster.ParseToStocksData(searchResult,supplier.Name));
 068                    }
 069                }
 070            }
 71            catch (Exception e)
 72            {
 073                Console.WriteLine(e);
 074                throw;
 75            }
 76        }
 077        return stocksDataResult;
 078    }
 79}