| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | |
| | | 3 | | namespace NLightning.Infrastructure.Repositories.Database.Bitcoin; |
| | | 4 | | |
| | | 5 | | using Domain.Bitcoin.Interfaces; |
| | | 6 | | using Domain.Bitcoin.ValueObjects; |
| | | 7 | | using Persistence.Contexts; |
| | | 8 | | using Persistence.Entities.Bitcoin; |
| | | 9 | | |
| | | 10 | | public class BlockchainStateDbRepository(NLightningDbContext context) |
| | 0 | 11 | | : BaseDbRepository<BlockchainStateEntity>(context), IBlockchainStateDbRepository |
| | | 12 | | { |
| | | 13 | | public void Add(BlockchainState blockchainState) |
| | 0 | 14 | | { |
| | 0 | 15 | | var entity = MapDomainToEntity(blockchainState); |
| | 0 | 16 | | Insert(entity); |
| | 0 | 17 | | } |
| | | 18 | | |
| | | 19 | | public void Update(BlockchainState blockchainState) |
| | 0 | 20 | | { |
| | 0 | 21 | | var entity = MapDomainToEntity(blockchainState); |
| | 0 | 22 | | Update(entity); |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | public async Task<BlockchainState?> GetStateAsync() |
| | 0 | 26 | | { |
| | 0 | 27 | | var entity = await DbSet.AsNoTracking().FirstOrDefaultAsync(); |
| | 0 | 28 | | return entity is null ? null : MapEntityToDomain(entity); |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | private static BlockchainStateEntity MapDomainToEntity(BlockchainState blockchainState) |
| | 0 | 32 | | { |
| | 0 | 33 | | return new BlockchainStateEntity |
| | 0 | 34 | | { |
| | 0 | 35 | | Id = blockchainState.Id, |
| | 0 | 36 | | LastProcessedHeight = blockchainState.LastProcessedHeight, |
| | 0 | 37 | | LastProcessedBlockHash = blockchainState.LastProcessedBlockHash, |
| | 0 | 38 | | LastProcessedAt = blockchainState.LastProcessedAt |
| | 0 | 39 | | }; |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | private static BlockchainState MapEntityToDomain(BlockchainStateEntity entity) |
| | 0 | 43 | | { |
| | 0 | 44 | | return new BlockchainState(entity.LastProcessedHeight, entity.LastProcessedBlockHash, entity.LastProcessedAt) |
| | 0 | 45 | | { |
| | 0 | 46 | | Id = entity.Id |
| | 0 | 47 | | }; |
| | 0 | 48 | | } |
| | | 49 | | } |