< Summary

Information
Class: Services.LabelServices.OrderScanitStore
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/LabelServices/OrderScanitStore.cs
Line coverage
100%
Covered lines: 27
Uncovered lines: 0
Coverable lines: 27
Total lines: 47
Line coverage: 100%
Branch coverage
83%
Covered branches: 10
Total branches: 12
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
SaveAsync()100%44100%
<SaveAsync()75%88100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/LabelServices/OrderScanitStore.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using OzonRepositories.Context;
 3
 4namespace Services.LabelServices;
 5
 6public class OrderScanitStore : IOrderScanitStore
 7{
 8    private readonly IDbContextFactory<ApplicationDbContext> _dbFactory;
 9
 310    public OrderScanitStore(IDbContextFactory<ApplicationDbContext> dbFactory)
 11    {
 312        _dbFactory = dbFactory;
 313    }
 14
 15    public async Task<int> SaveAsync(IReadOnlyDictionary<string, string> scanitByPosting, CancellationToken ct = default
 16    {
 417        if (scanitByPosting.Count == 0)
 18        {
 119            return 0;
 20        }
 21
 322        await using var db = await _dbFactory.CreateDbContextAsync(ct).ConfigureAwait(false);
 23
 24        // Контекст настроен с EnableRetryOnFailure: пользовательскую транзакцию нужно выполнять
 25        // внутри стратегии выполнения, иначе EF откажется её открывать.
 326        var strategy = db.Database.CreateExecutionStrategy();
 327        return await strategy.ExecuteAsync(async () =>
 328        {
 329            await using var transaction = await db.Database.BeginTransactionAsync(ct).ConfigureAwait(false);
 330
 331            var updated = 0;
 1432            foreach (var (posting, scanit) in scanitByPosting)
 333            {
 434                var childPrefix = posting + "/";
 435                updated += await db.Orders
 436                    .Where(o => o.ShipmentNumber != null
 437                                && (o.ShipmentNumber == posting || o.ShipmentNumber.StartsWith(childPrefix))
 438                                && o.Scanit != scanit)
 439                    .ExecuteUpdateAsync(s => s.SetProperty(o => o.Scanit, scanit), ct)
 440                    .ConfigureAwait(false);
 341            }
 342
 343            await transaction.CommitAsync(ct).ConfigureAwait(false);
 344            return updated;
 345        }).ConfigureAwait(false);
 446    }
 47}