| | 1 | | namespace NLightning.Infrastructure.Repositories; |
| | 2 | |
|
| | 3 | | using Database.Bitcoin; |
| | 4 | | using Database.Channel; |
| | 5 | | using Database.Node; |
| | 6 | | using Domain.Bitcoin.Interfaces; |
| | 7 | | using Domain.Channels.Interfaces; |
| | 8 | | using Domain.Channels.Models; |
| | 9 | | using Domain.Crypto.Hashes; |
| | 10 | | using Domain.Node.Interfaces; |
| | 11 | | using Domain.Node.Models; |
| | 12 | | using Domain.Persistence.Interfaces; |
| | 13 | | using Domain.Serialization.Interfaces; |
| | 14 | | using Persistence.Contexts; |
| | 15 | |
|
| | 16 | | public 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 => |
| 0 | 36 | | _blockchainStateDbRepository ??= new BlockchainStateDbRepository(_context); |
| | 37 | |
|
| | 38 | | public IWatchedTransactionDbRepository WatchedTransactionDbRepository => |
| 0 | 39 | | _watchedTransactionDbRepository ??= new WatchedTransactionDbRepository(_context); |
| | 40 | |
|
| | 41 | | public IChannelConfigDbRepository ChannelConfigDbRepository => |
| 0 | 42 | | _channelConfigDbRepository ??= new ChannelConfigDbRepository(_context); |
| | 43 | |
|
| | 44 | | public IChannelDbRepository ChannelDbRepository => |
| 0 | 45 | | _channelDbRepository ??= new ChannelDbRepository(_context, _messageSerializer, _sha256); |
| | 46 | |
|
| | 47 | | public IChannelKeySetDbRepository ChannelKeySetDbRepository => |
| 0 | 48 | | _channelKeySetDbRepository ??= new ChannelKeySetDbRepository(_context); |
| | 49 | |
|
| | 50 | | public IHtlcDbRepository HtlcDbRepository => |
| 0 | 51 | | _htlcDbRepository ??= new HtlcDbRepository(_context, _messageSerializer); |
| | 52 | |
|
| | 53 | | public IPeerDbRepository PeerDbRepository => |
| 0 | 54 | | _peerDbRepository ??= new PeerDbRepository(_context); |
| | 55 | |
|
| 0 | 56 | | public UnitOfWork(NLightningDbContext context, IMessageSerializer messageSerializer, ISha256 sha256) |
| 0 | 57 | | { |
| 0 | 58 | | _context = context ?? throw new ArgumentNullException(nameof(context)); |
| 0 | 59 | | _messageSerializer = messageSerializer; |
| 0 | 60 | | _sha256 = sha256; |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | public async Task<ICollection<PeerModel>> GetPeersForStartupAsync() |
| 0 | 64 | | { |
| 0 | 65 | | var peers = await PeerDbRepository.GetAllAsync(); |
| 0 | 66 | | var peerList = peers.ToList(); |
| 0 | 67 | | foreach (var peer in peerList) |
| 0 | 68 | | { |
| 0 | 69 | | var channels = await ChannelDbRepository.GetByPeerIdAsync(peer.NodeId); |
| 0 | 70 | | var channelList = channels.ToList(); |
| 0 | 71 | | if (channelList.Count > 0) |
| 0 | 72 | | peer.Channels = channelList as List<ChannelModel>; |
| 0 | 73 | | } |
| | 74 | |
|
| 0 | 75 | | return peerList; |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | public void SaveChanges() |
| 0 | 79 | | { |
| 0 | 80 | | _context.SaveChanges(); |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | public Task SaveChangesAsync() |
| 0 | 84 | | { |
| 0 | 85 | | return _context.SaveChangesAsync(); |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | #region Dispose Pattern |
| | 89 | |
|
| | 90 | | private bool _disposed; |
| | 91 | |
|
| | 92 | | protected virtual void Dispose(bool disposing) |
| 0 | 93 | | { |
| 0 | 94 | | if (!_disposed) |
| 0 | 95 | | { |
| 0 | 96 | | if (disposing) |
| 0 | 97 | | _context.Dispose(); |
| 0 | 98 | | } |
| | 99 | |
|
| 0 | 100 | | _disposed = true; |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | public void Dispose() |
| 0 | 104 | | { |
| 0 | 105 | | Dispose(true); |
| 0 | 106 | | GC.SuppressFinalize(this); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | #endregion |
| | 110 | | } |