< Summary

Information
Class: OzonRepositories.Data.BitrixStockUnarchiveJobRepository
Assembly: OzonRepositories
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonRepositories/Data/BitrixStockUnarchiveJobRepository.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 67
Coverable lines: 67
Total lines: 136
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
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%
CreateExecutionAsync()100%210%
UpdateHangfireJobIdAsync()0%620%
GetExecutionByIdAsync()0%620%
UpdateItemResultAsync()0%620%
UpdateExecutionCountersAsync()0%620%
CompleteExecutionAsync()0%620%
GetExecutionsPageAsync()0%2040%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonRepositories/Data/BitrixStockUnarchiveJobRepository.cs

#LineLine coverage
 1using Microsoft.EntityFrameworkCore;
 2using OzonDomains.Models;
 3using OzonRepositories.Context;
 4
 5namespace OzonRepositories.Data;
 6
 7public class BitrixStockUnarchiveJobRepository : IBitrixStockUnarchiveJobRepository
 8{
 9    private readonly ApplicationDbContext _context;
 10
 011    public BitrixStockUnarchiveJobRepository(ApplicationDbContext context)
 12    {
 013        _context = context;
 014    }
 15
 16    public async Task<BitrixStockUnarchiveExecution> CreateExecutionAsync(
 17        string jobId,
 18        string? hangfireJobId,
 19        string? userId,
 20        string? userName,
 21        IReadOnlyList<BitrixStockUnarchiveItem> items,
 22        string triggerType = "Manual",
 23        CancellationToken cancellationToken = default)
 24    {
 025        var execution = new BitrixStockUnarchiveExecution
 026        {
 027            JobId = jobId,
 028            HangfireJobId = hangfireJobId,
 029            Status = BitrixStockUnarchiveExecutionStatus.Running,
 030            TriggerType = triggerType,
 031            StartedAt = DateTime.UtcNow,
 032            TotalItems = items.Count,
 033            TriggeredByUserId = userId,
 034            TriggeredByUserName = userName,
 035            Items = items.ToList()
 036        };
 37
 038        _context.BitrixStockUnarchiveExecutions.Add(execution);
 039        await _context.SaveChangesAsync(cancellationToken);
 040        return execution;
 041    }
 42
 43    public async Task UpdateHangfireJobIdAsync(long executionId, string hangfireJobId,
 44        CancellationToken cancellationToken = default)
 45    {
 046        var execution = await _context.BitrixStockUnarchiveExecutions.FindAsync(new object[] { executionId }, cancellati
 047        if (execution == null)
 48        {
 049            return;
 50        }
 51
 052        execution.HangfireJobId = hangfireJobId;
 053        await _context.SaveChangesAsync(cancellationToken);
 054    }
 55
 56    public async Task<BitrixStockUnarchiveExecution?> GetExecutionByIdAsync(long id, bool includeItems = false,
 57        CancellationToken cancellationToken = default)
 58    {
 059        var query = _context.BitrixStockUnarchiveExecutions.AsQueryable();
 060        if (includeItems)
 61        {
 062            query = query.Include(e => e.Items.OrderBy(i => i.SortOrder));
 63        }
 64
 065        return await query.FirstOrDefaultAsync(e => e.Id == id, cancellationToken);
 066    }
 67
 68    public async Task UpdateItemResultAsync(long itemId, string status, string? errorMessage,
 69        CancellationToken cancellationToken = default)
 70    {
 071        var item = await _context.BitrixStockUnarchiveItems.FindAsync(new object[] { itemId }, cancellationToken);
 072        if (item == null)
 73        {
 074            return;
 75        }
 76
 077        item.Status = status;
 078        item.ErrorMessage = errorMessage;
 079        item.ProcessedAt = DateTime.UtcNow;
 080        await _context.SaveChangesAsync(cancellationToken);
 081    }
 82
 83    public async Task UpdateExecutionCountersAsync(long executionId, int successCount, int failedCount,
 84        CancellationToken cancellationToken = default)
 85    {
 086        var execution = await _context.BitrixStockUnarchiveExecutions.FindAsync(new object[] { executionId }, cancellati
 087        if (execution == null)
 88        {
 089            return;
 90        }
 91
 092        execution.SuccessCount = successCount;
 093        execution.FailedCount = failedCount;
 094        await _context.SaveChangesAsync(cancellationToken);
 095    }
 96
 97    public async Task CompleteExecutionAsync(long executionId, string status, string? errorMessage,
 98        CancellationToken cancellationToken = default)
 99    {
 0100        var execution = await _context.BitrixStockUnarchiveExecutions.FindAsync(new object[] { executionId }, cancellati
 0101        if (execution == null)
 102        {
 0103            return;
 104        }
 105
 0106        execution.Status = status;
 0107        execution.ErrorMessage = errorMessage;
 0108        execution.CompletedAt = DateTime.UtcNow;
 0109        execution.DurationMs = (long)(execution.CompletedAt.Value - execution.StartedAt).TotalMilliseconds;
 0110        await _context.SaveChangesAsync(cancellationToken);
 0111    }
 112
 113    public async Task<(List<BitrixStockUnarchiveExecution> Items, int Total)> GetExecutionsPageAsync(int page, int pageS
 114        CancellationToken cancellationToken = default)
 115    {
 0116        if (page < 1)
 117        {
 0118            page = 1;
 119        }
 120
 0121        if (pageSize < 1)
 122        {
 0123            pageSize = 20;
 124        }
 125
 0126        var query = _context.BitrixStockUnarchiveExecutions.AsNoTracking();
 0127        var total = await query.CountAsync(cancellationToken);
 0128        var items = await query
 0129            .OrderByDescending(e => e.StartedAt)
 0130            .Skip((page - 1) * pageSize)
 0131            .Take(pageSize)
 0132            .ToListAsync(cancellationToken);
 133
 0134        return (items, total);
 0135    }
 136}