< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Repositories.UnitOfWork
Assembly: NLightning.Infrastructure.Repositories
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Repositories/UnitOfWork.cs
Tag: 36_15743069263
Line coverage
0%
Covered lines: 0
Uncovered lines: 43
Coverable lines: 43
Total lines: 110
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 24
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure.Repositories/UnitOfWork.cs

#LineLine coverage
 1namespace NLightning.Infrastructure.Repositories;
 2
 3using Database.Bitcoin;
 4using Database.Channel;
 5using Database.Node;
 6using Domain.Bitcoin.Interfaces;
 7using Domain.Channels.Interfaces;
 8using Domain.Channels.Models;
 9using Domain.Crypto.Hashes;
 10using Domain.Node.Interfaces;
 11using Domain.Node.Models;
 12using Domain.Persistence.Interfaces;
 13using Domain.Serialization.Interfaces;
 14using Persistence.Contexts;
 15
 16public class UnitOfWork : IUnitOfWork
 17{
 18    private readonly NLightningDbContext _context;
 19    private readonly IMessageSerializer _messageSerializer;
 20    private readonly ISha256 _sha256;
 21
 22    // Bitcoin repositories
 23    private BlockchainStateDbRepository? _blockchainStateDbRepository;
 24    private WatchedTransactionDbRepository? _watchedTransactionDbRepository;
 25
 26    // Channel repositories
 27    private ChannelConfigDbRepository? _channelConfigDbRepository;
 28    private ChannelDbRepository? _channelDbRepository;
 29    private ChannelKeySetDbRepository? _channelKeySetDbRepository;
 30    private HtlcDbRepository? _htlcDbRepository;
 31
 32    // Node repositories
 33    private PeerDbRepository? _peerDbRepository;
 34
 35    public IBlockchainStateDbRepository BlockchainStateDbRepository =>
 036        _blockchainStateDbRepository ??= new BlockchainStateDbRepository(_context);
 37
 38    public IWatchedTransactionDbRepository WatchedTransactionDbRepository =>
 039        _watchedTransactionDbRepository ??= new WatchedTransactionDbRepository(_context);
 40
 41    public IChannelConfigDbRepository ChannelConfigDbRepository =>
 042        _channelConfigDbRepository ??= new ChannelConfigDbRepository(_context);
 43
 44    public IChannelDbRepository ChannelDbRepository =>
 045        _channelDbRepository ??= new ChannelDbRepository(_context, _messageSerializer, _sha256);
 46
 47    public IChannelKeySetDbRepository ChannelKeySetDbRepository =>
 048        _channelKeySetDbRepository ??= new ChannelKeySetDbRepository(_context);
 49
 50    public IHtlcDbRepository HtlcDbRepository =>
 051        _htlcDbRepository ??= new HtlcDbRepository(_context, _messageSerializer);
 52
 53    public IPeerDbRepository PeerDbRepository =>
 054        _peerDbRepository ??= new PeerDbRepository(_context);
 55
 056    public UnitOfWork(NLightningDbContext context, IMessageSerializer messageSerializer, ISha256 sha256)
 057    {
 058        _context = context ?? throw new ArgumentNullException(nameof(context));
 059        _messageSerializer = messageSerializer;
 060        _sha256 = sha256;
 061    }
 62
 63    public async Task<ICollection<PeerModel>> GetPeersForStartupAsync()
 064    {
 065        var peers = await PeerDbRepository.GetAllAsync();
 066        var peerList = peers.ToList();
 067        foreach (var peer in peerList)
 068        {
 069            var channels = await ChannelDbRepository.GetByPeerIdAsync(peer.NodeId);
 070            var channelList = channels.ToList();
 071            if (channelList.Count > 0)
 072                peer.Channels = channelList as List<ChannelModel>;
 073        }
 74
 075        return peerList;
 076    }
 77
 78    public void SaveChanges()
 079    {
 080        _context.SaveChanges();
 081    }
 82
 83    public Task SaveChangesAsync()
 084    {
 085        return _context.SaveChangesAsync();
 086    }
 87
 88    #region Dispose Pattern
 89
 90    private bool _disposed;
 91
 92    protected virtual void Dispose(bool disposing)
 093    {
 094        if (!_disposed)
 095        {
 096            if (disposing)
 097                _context.Dispose();
 098        }
 99
 0100        _disposed = true;
 0101    }
 102
 103    public void Dispose()
 0104    {
 0105        Dispose(true);
 0106        GC.SuppressFinalize(this);
 0107    }
 108
 109    #endregion
 110}