Skip to content

Commit

Permalink
RPC call for adding UTXOTags (#381)
Browse files Browse the repository at this point in the history
* feat: add tags call

* feat: method for rpc call

* Update src/Proto/nodeguard.proto

Co-authored-by: José A.P <[email protected]>

* Update src/Rpc/NodeGuardService.cs

Co-authored-by: José A.P <[email protected]>

* added unique constraint to tag-outpoint

* upsert when adding tags from api endpoint

---------

Co-authored-by: Rodrigo <[email protected]>
Co-authored-by: José A.P <[email protected]>
  • Loading branch information
3 people authored Jul 1, 2024
1 parent 867bd8a commit 4392b2d
Show file tree
Hide file tree
Showing 8 changed files with 1,432 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/Data/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
);
}

modelBuilder.Entity<UTXOTag>()
.HasIndex(u => new { u.Key, u.Outpoint })
.IsUnique();

base.OnModelCreating(modelBuilder);
}

Expand Down Expand Up @@ -114,4 +118,4 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

public DbSet<APIToken> ApiTokens { get; set; }
}
}
}
4 changes: 3 additions & 1 deletion src/Data/Repositories/Interfaces/IUTXOTagRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ public interface IUTXOTagRepository
Task<(bool, string?)> AddAsync(UTXOTag type);

Task<(bool, string?)> AddRangeAsync(List<UTXOTag> type);

(bool, string?) Remove(UTXOTag type);

(bool, string?) RemoveRange(List<UTXOTag> types);

(bool, string?) Update(UTXOTag type);

Task<(bool, string?)> UpsertRangeAsync(List<UTXOTag> type);
}
45 changes: 44 additions & 1 deletion src/Data/Repositories/UTXOTagRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,53 @@ public async Task<List<UTXOTag>> GetByKeyValue(string key, string value)
public async Task<(bool, string?)> AddRangeAsync(List<UTXOTag> type)
{
await using var applicationDbContext = await _dbContextFactory.CreateDbContextAsync();


foreach (var t in type)
{
t.SetCreationDatetime();
}

return await _repository.AddRangeAsync(type, applicationDbContext);
}

public async Task<(bool, string?)> UpsertRangeAsync(List<UTXOTag> entities)
{
await using var dbContext = await _dbContextFactory.CreateDbContextAsync();

// Load existing entities based on composite keys in a single query
var tags = entities.Select(e => e.Key).ToList();
var utxoIds = entities.Select(e => e.Outpoint).ToList();

var existingEntities = await dbContext.UTXOTags
.Where(e => tags.Contains(e.Key) && utxoIds.Contains(e.Outpoint))
.ToListAsync();

foreach (var entity in entities)
{
var existingEntity = existingEntities
.FirstOrDefault(e => e.Key == entity.Key && e.Outpoint == entity.Outpoint);

if (existingEntity == null)
{
// New entity, set CreatedAt
entity.SetCreationDatetime();
dbContext.UTXOTags.Add(entity);
}
else
{
// Existing entity, set UpdatedAt and update properties
existingEntity.SetUpdateDatetime();
existingEntity.Key = entity.Key;
existingEntity.Value = entity.Value;
existingEntity.Outpoint = entity.Outpoint;
dbContext.Entry(existingEntity).State = EntityState.Modified;
}
}

var result = await dbContext.SaveChangesAsync();
return result > 0 ? (true, null) : (false, "Failed to save changes");
}

public (bool, string?) Remove(UTXOTag type)
{
using var applicationDbContext = _dbContextFactory.CreateDbContext();
Expand Down
Loading

0 comments on commit 4392b2d

Please sign in to comment.