45 lines
1.4 KiB
C#
45 lines
1.4 KiB
C#
|
|
using StellaOps.SbomService.Models;
|
|
using System.Text.Json;
|
|
|
|
namespace StellaOps.SbomService.Repositories;
|
|
|
|
public sealed class FileComponentLookupRepository : IComponentLookupRepository
|
|
{
|
|
private readonly IReadOnlyList<ComponentLookupRecord> _items;
|
|
|
|
public FileComponentLookupRepository(string path)
|
|
{
|
|
if (!File.Exists(path))
|
|
{
|
|
_items = Array.Empty<ComponentLookupRecord>();
|
|
return;
|
|
}
|
|
|
|
using var stream = File.OpenRead(path);
|
|
var items = JsonSerializer.Deserialize<List<ComponentLookupRecord>>(stream, new JsonSerializerOptions
|
|
{
|
|
PropertyNameCaseInsensitive = true
|
|
});
|
|
|
|
_items = items ?? new List<ComponentLookupRecord>();
|
|
}
|
|
|
|
public Task<(IReadOnlyList<ComponentLookupRecord> Items, int Total)> QueryAsync(ComponentLookupQuery query, CancellationToken cancellationToken)
|
|
{
|
|
var filtered = _items
|
|
.Where(c => c.Purl.Equals(query.Purl, StringComparison.OrdinalIgnoreCase))
|
|
.Where(c => query.Artifact is null || c.Artifact.Equals(query.Artifact, StringComparison.OrdinalIgnoreCase))
|
|
.OrderBy(c => c.Artifact)
|
|
.ThenBy(c => c.Purl)
|
|
.ToList();
|
|
|
|
var page = filtered
|
|
.Skip(query.Offset)
|
|
.Take(query.Limit)
|
|
.ToList();
|
|
|
|
return Task.FromResult<(IReadOnlyList<ComponentLookupRecord> Items, int Total)>((page, filtered.Count));
|
|
}
|
|
}
|