< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Repositories.Database.Bitcoin.UtxoDbRepository
Assembly: NLightning.Infrastructure.Repositories
File(s): /home/runner/work/NLightning/NLightning/src/NLightning.Infrastructure.Repositories/Database/Bitcoin/UtxoDbRepository.cs
Tag: 57_24045730253
Line coverage
0%
Covered lines: 0
Uncovered lines: 51
Coverable lines: 51
Total lines: 84
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
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%
Add(...)100%210%
Add(...)100%210%
Spend(...)100%210%
Spend(...)100%210%
Update(...)100%210%
Update(...)100%210%
GetUnspentAsync()0%620%
GetUnspentAsync()0%620%
GetByIdAsync()0%2040%
GetByIdAsync()0%2040%
MapDomainToEntity(...)100%210%
MapDomainToEntity(...)100%210%
MapEntityToModel(...)0%620%
MapEntityToModel(...)0%620%

File(s)

/home/runner/work/NLightning/NLightning/src/NLightning.Infrastructure.Repositories/Database/Bitcoin/UtxoDbRepository.cs

#LineLine coverage
 1using System.Linq.Expressions;
 2using Microsoft.EntityFrameworkCore;
 3
 4namespace NLightning.Infrastructure.Repositories.Database.Bitcoin;
 5
 6using Domain.Bitcoin.Interfaces;
 7using Domain.Bitcoin.ValueObjects;
 8using Domain.Bitcoin.Wallet.Models;
 9using Domain.Money;
 10using Persistence.Contexts;
 11using Persistence.Entities.Bitcoin;
 12
 13public class UtxoDbRepository(NLightningDbContext context)
 014    : BaseDbRepository<UtxoEntity>(context), IUtxoDbRepository
 15{
 16    public void Add(UtxoModel utxoModel)
 017    {
 018        var utxoEntity = MapDomainToEntity(utxoModel);
 019        Insert(utxoEntity);
 020    }
 21
 22    public void Spend(UtxoModel utxoModel)
 023    {
 024        var utxoEntity = MapDomainToEntity(utxoModel);
 025        Delete(utxoEntity);
 026    }
 27
 28    public void Update(UtxoModel utxoModel)
 029    {
 030        var utxoEntity = MapDomainToEntity(utxoModel);
 031        Update(utxoEntity);
 032    }
 33
 34    public async Task<IEnumerable<UtxoModel>> GetUnspentAsync(bool includeWalletAddress = false)
 035    {
 036        var query = Get(asNoTracking: true).AsQueryable();
 037        if (includeWalletAddress)
 038            query = query.Include(x => x.WalletAddress);
 39
 040        var utxoSet = await query.ToListAsync();
 41
 042        return utxoSet.Select(MapEntityToModel);
 043    }
 44
 45    public async Task<UtxoModel?> GetByIdAsync(TxId txId, uint index, bool includeWalletAddress = false)
 046    {
 047        Expression<Func<UtxoEntity, object>>? include = includeWalletAddress
 048                                                            ? entity => entity.WalletAddress!
 049                                                            : null;
 050        var utxoEntity = await GetByIdAsync(new { txId, index }, true, include);
 051        return utxoEntity is null
 052                   ? null
 053                   : MapEntityToModel(utxoEntity);
 054    }
 55
 56    private UtxoEntity MapDomainToEntity(UtxoModel model)
 057    {
 058        return new UtxoEntity
 059        {
 060            TransactionId = model.TxId,
 061            Index = model.Index,
 062            AmountSats = model.Amount.Satoshi,
 063            BlockHeight = model.BlockHeight,
 064            AddressIndex = model.AddressIndex,
 065            IsAddressChange = model.IsAddressChange,
 066            AddressType = model.AddressType
 067        };
 068    }
 69
 70    private UtxoModel MapEntityToModel(UtxoEntity entity)
 071    {
 072        var utxoModel = new UtxoModel(entity.TransactionId, entity.Index, LightningMoney.Satoshis(entity.AmountSats),
 073                                      entity.BlockHeight, entity.AddressIndex, entity.IsAddressChange,
 074                                      entity.AddressType);
 75
 076        if (entity.WalletAddress is null)
 077            return utxoModel;
 78
 079        var walletAddressModel = WalletAddressesDbRepository.MapEntityToModel(entity.WalletAddress);
 080        utxoModel.SetWalletAddress(walletAddressModel);
 81
 082        return utxoModel;
 083    }
 84}