Skip to content

Commit

Permalink
Merge pull request #398 from ucdavis/srk/search-by-metadata
Browse files Browse the repository at this point in the history
get any txns that match provided metadata
  • Loading branch information
srkirkland authored Nov 6, 2023
2 parents fc61138 + 03afb20 commit 8af9709
Show file tree
Hide file tree
Showing 6 changed files with 1,345 additions and 3 deletions.
29 changes: 29 additions & 0 deletions Sloth.Api/Controllers/v2/TransactionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,35 @@ public async Task<IList<Transaction>> GetByKfsKey(string id)
return transactions;
}

/// <summary>
/// Fetch Transactions by Metadata
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
[HttpGet("metadata/{key}/{value}")]
[ProducesResponseType(typeof(IList<Transaction>), 200)]
public async Task<IList<Transaction>> GetByMetadata(string key, string value)
{
var teamId = GetTeamId();

if (string.IsNullOrWhiteSpace(key) || string.IsNullOrWhiteSpace(value))
{
return new List<Transaction>();
}

var transactions = await _context.Transactions
.Where(t => t.Source.Team.Id == teamId)
.Include(t => t.Creator)
.Include(t => t.Transfers)
.Include(t => t.Metadata)
.Where(t => t.Metadata.Any(m => m.Name == key && m.Value == value))
.AsNoTracking()
.ToListAsync();

return transactions;
}

// TODO: just for testing, remove later

/// <summary>
Expand Down
6 changes: 4 additions & 2 deletions Sloth.Core/Domain/TransactionMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ public class TransactionMetadata
public string Name { get; set; }

[Required]
[MaxLength(450)]
public string Value { get; set; }

public static void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TransactionMetadata>()
.HasIndex(r => r.TransactionId);

// modelBuilder.Entity<TransactionMetadata>()
// .HasIndex(r => r.Name);
// create a covering index for key and value
modelBuilder.Entity<TransactionMetadata>()
.HasIndex(r => new { r.TransactionId, r.Name, r.Value });
}
}
}
Loading

0 comments on commit 8af9709

Please sign in to comment.