feat: work on performing searches

This commit is contained in:
Stevan Freeborn
2024-08-31 21:08:33 -05:00
parent dc4d4ff1d3
commit 78748cc104
8 changed files with 68 additions and 4 deletions
@@ -3,5 +3,5 @@ namespace SanctionsSearch.Worker.Interfaces;
interface IRepository<T> where T : Entity
{
Task Upsert(T entity);
Task<IEnumerable<T>> Find(Expression<Func<T, bool>> predicate);
Task<IEnumerable<T>> Find(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[]? includes);
}
@@ -0,0 +1,6 @@
namespace SanctionsSearch.Worker.Interfaces;
interface ISearchService
{
Task<SearchResult> PerformSearchAsync(SearchRequest request);
}
@@ -9,4 +9,9 @@ class Address : Entity
public string Remarks { get; set; } = string.Empty;
public virtual Sdn Sdn { get; set; } = default!;
public override string ToString()
{
return $"{StreetAddress}, {CityProvincePostal}, {Country}";
}
}
+11
View File
@@ -17,4 +17,15 @@ class Sdn : Entity
public virtual ICollection<Address> Addresses { get; set; } = [];
public virtual ICollection<Alias> Aliases { get; set; } = [];
public virtual ICollection<Comment> Comments { get; set; } = [];
public Hit ToHit()
{
return new Hit
{
Name = Name,
Address = Addresses.FirstOrDefault()?.ToString() ?? string.Empty,
Type = Type,
Programs = [.. Program.Split("] [")]
};
}
}
@@ -9,4 +9,9 @@ class SearchRequest
public string State { get; set; } = string.Empty;
public string Zip { get; set; } = string.Empty;
public string Country { get; set; } = string.Empty;
public Expression<Func<Sdn, bool>> ToSdnFilter()
{
return sdn => EF.Functions.Like(sdn.Name, $"%{Name}%");
}
}
@@ -2,5 +2,20 @@ namespace SanctionsSearch.Worker.Models;
class SearchResult
{
// TODO: Add properties to represent the search result
public int SearchRequestId { get; init; }
public List<Hit> Hits { get; init; } = [];
public SearchResult(int searchRequestId, List<Hit> hits)
{
SearchRequestId = searchRequestId;
Hits = hits;
}
}
class Hit
{
public string Name { get; init; } = string.Empty;
public string Address { get; init; } = string.Empty;
public string Type { get; init; } = string.Empty;
public List<string> Programs { get; init; } = [];
}
@@ -11,11 +11,18 @@ class EfRepository<T> : IRepository<T> where T : Entity
_logger = logger;
}
public async Task<IEnumerable<T>> Find(Expression<Func<T, bool>> predicate)
public async Task<IEnumerable<T>> Find(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[]? includes)
{
try
{
return await _context.Set<T>().Where(predicate).ToListAsync();
var query = _context.Set<T>().AsQueryable();
if (includes is not null)
{
query = includes.Aggregate(query, (current, include) => current.Include(include));
}
return await query.Where(predicate).ToListAsync();
}
catch (Exception ex)
{
@@ -0,0 +1,15 @@
namespace SanctionsSearch.Worker.Services;
class SearchService(IUnitOfWork uow) : ISearchService
{
private readonly IUnitOfWork _uow = uow;
public async Task<SearchResult> PerformSearchAsync(SearchRequest request)
{
var results = await _uow.Sdns.Find(request.ToSdnFilter(), sdn => sdn.Addresses);
var hits = results.Select(sdn => sdn.ToHit()).ToList();
return new(request.Id, hits);
}
}