| | 1 | | using Microsoft.EntityFrameworkCore; |
| | 2 | |
|
| | 3 | | namespace NLightning.Infrastructure.Repositories.Database.Bitcoin; |
| | 4 | |
|
| | 5 | | using Domain.Bitcoin.Interfaces; |
| | 6 | | using Domain.Bitcoin.Transactions.Models; |
| | 7 | | using Domain.Bitcoin.ValueObjects; |
| | 8 | | using Persistence.Contexts; |
| | 9 | | using Persistence.Entities.Bitcoin; |
| | 10 | |
|
| | 11 | | public class WatchedTransactionDbRepository(NLightningDbContext context) |
| 0 | 12 | | : BaseDbRepository<WatchedTransactionEntity>(context), IWatchedTransactionDbRepository |
| | 13 | | { |
| | 14 | | public void Add(WatchedTransactionModel watchedTransactionModel) |
| 0 | 15 | | { |
| 0 | 16 | | var watchedTransactionEntity = MapDomainToEntity(watchedTransactionModel); |
| | 17 | |
|
| 0 | 18 | | if (watchedTransactionEntity.CreatedAt.Equals(DateTime.MinValue)) |
| 0 | 19 | | watchedTransactionEntity.CreatedAt = DateTime.UtcNow; |
| | 20 | |
|
| 0 | 21 | | Insert(watchedTransactionEntity); |
| 0 | 22 | | } |
| | 23 | |
|
| | 24 | | public void Update(WatchedTransactionModel watchedTransactionModel) |
| 0 | 25 | | { |
| 0 | 26 | | var watchedTransactionEntity = MapDomainToEntity(watchedTransactionModel); |
| 0 | 27 | | Update(watchedTransactionEntity); |
| 0 | 28 | | } |
| | 29 | |
|
| | 30 | | public async Task<IEnumerable<WatchedTransactionModel>> GetAllPendingAsync() |
| 0 | 31 | | { |
| 0 | 32 | | var entities = await DbSet |
| 0 | 33 | | .AsNoTracking() |
| 0 | 34 | | .Where(x => x.CompletedAt == null) |
| 0 | 35 | | .ToListAsync(); |
| | 36 | |
|
| 0 | 37 | | return entities.Select(entity => MapEntityToDomain(entity)); |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | public async Task<WatchedTransactionModel?> GetByTransactionIdAsync(TxId transactionId) |
| 0 | 41 | | { |
| 0 | 42 | | var entity = await GetByIdAsync(transactionId); |
| 0 | 43 | | return entity == null ? null : MapEntityToDomain(entity); |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | private static WatchedTransactionEntity MapDomainToEntity(WatchedTransactionModel watchedTransactionModel) |
| 0 | 47 | | { |
| 0 | 48 | | return new WatchedTransactionEntity |
| 0 | 49 | | { |
| 0 | 50 | | ChannelId = watchedTransactionModel.ChannelId, |
| 0 | 51 | | TransactionId = watchedTransactionModel.TransactionId, |
| 0 | 52 | | RequiredDepth = watchedTransactionModel.RequiredDepth, |
| 0 | 53 | | FirstSeenAtHeight = watchedTransactionModel.FirstSeenAtHeight, |
| 0 | 54 | | TransactionIndex = watchedTransactionModel.TransactionIndex, |
| 0 | 55 | | CompletedAt = watchedTransactionModel.IsCompleted ? DateTime.UtcNow : null |
| 0 | 56 | | }; |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | private static WatchedTransactionModel MapEntityToDomain(WatchedTransactionEntity entity) |
| 0 | 60 | | { |
| 0 | 61 | | var model = new WatchedTransactionModel(entity.ChannelId, entity.TransactionId, entity.RequiredDepth); |
| | 62 | |
|
| 0 | 63 | | if (entity.CompletedAt.HasValue) |
| 0 | 64 | | model.MarkAsCompleted(); |
| | 65 | |
|
| 0 | 66 | | if (entity.FirstSeenAtHeight.HasValue && entity.TransactionIndex.HasValue) |
| 0 | 67 | | model.SetHeightAndIndex(entity.FirstSeenAtHeight.Value, entity.TransactionIndex.Value); |
| | 68 | |
|
| 0 | 69 | | return model; |
| 0 | 70 | | } |
| | 71 | | } |