Compare commits
No commits in common. "4fbca6130ed70993b7759ada0f284b9892f14a43" and "75852eac83e5d07365b360dd0ef9e7e6d9db89bb" have entirely different histories.
4fbca6130e
...
75852eac83
125
Journal.Core/Repositories/InMemoryFragmentRepository.cs
Normal file
125
Journal.Core/Repositories/InMemoryFragmentRepository.cs
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
using Journal.Core.Models;
|
||||||
|
|
||||||
|
namespace Journal.Core.Repositories;
|
||||||
|
|
||||||
|
public class InMemoryFragmentRepository : IFragmentRepository
|
||||||
|
{
|
||||||
|
private readonly List<Fragment> _store = [];
|
||||||
|
private readonly Lock _lock = new();
|
||||||
|
|
||||||
|
public List<Fragment> GetAll()
|
||||||
|
{
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
return [.. _store];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Fragment? GetById(Guid id)
|
||||||
|
{
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
return _store.FirstOrDefault(f => f.Id == id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(Fragment fragment)
|
||||||
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(fragment);
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
if (fragment.Tags != null)
|
||||||
|
{
|
||||||
|
fragment.Tags = [.. fragment.Tags
|
||||||
|
.Where(t => !string.IsNullOrWhiteSpace(t))
|
||||||
|
.Select(t => t!.Trim())];
|
||||||
|
}
|
||||||
|
if (!string.IsNullOrWhiteSpace(fragment.Type)) fragment.Type = fragment.Type.Trim();
|
||||||
|
if (!string.IsNullOrWhiteSpace(fragment.Description)) fragment.Description = fragment.Description.Trim();
|
||||||
|
|
||||||
|
_store.Add(fragment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Remove(Guid id)
|
||||||
|
{
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
var item = _store.FirstOrDefault(f => f.Id == id);
|
||||||
|
if (item is null) return false;
|
||||||
|
return _store.Remove(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Update(Guid id, string? type = null, string? description = null, IEnumerable<string>? tags = null, DateTimeOffset? time = null)
|
||||||
|
{
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
var item = _store.FirstOrDefault(f => f.Id == id);
|
||||||
|
if (item is null) return false;
|
||||||
|
|
||||||
|
if (type != null)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(type)) throw new ArgumentException("Type cannot be empty", nameof(type));
|
||||||
|
item.Type = type.Trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (description != null)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(description)) throw new ArgumentException("Description cannot be empty", nameof(description));
|
||||||
|
item.Description = description.Trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tags != null)
|
||||||
|
{
|
||||||
|
item.Tags = [.. tags
|
||||||
|
.Where(t => !string.IsNullOrWhiteSpace(t))
|
||||||
|
.Select(t => t!.Trim())];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (time.HasValue)
|
||||||
|
item.Time = time.Value;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Fragment> GetByTag(string tag)
|
||||||
|
{
|
||||||
|
var q = tag?.Trim();
|
||||||
|
if (string.IsNullOrWhiteSpace(q)) return [];
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
return [.. _store.Where(f => f.Tags?.Any(t => !string.IsNullOrWhiteSpace(t) && t.Contains(q, StringComparison.OrdinalIgnoreCase)) == true)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Fragment> GetByType(string type)
|
||||||
|
{
|
||||||
|
var q = type?.Trim();
|
||||||
|
if (string.IsNullOrWhiteSpace(q)) return [];
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
return [.. _store.Where(f => !string.IsNullOrWhiteSpace(f.Type) && f.Type.Trim().Contains(q, StringComparison.OrdinalIgnoreCase))];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Fragment> Search(string? type = null, string? tag = null, DateTimeOffset? timeAfter = null)
|
||||||
|
{
|
||||||
|
var results = _store.AsEnumerable();
|
||||||
|
var qType = type?.Trim();
|
||||||
|
var qTag = tag?.Trim();
|
||||||
|
|
||||||
|
lock (_lock)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrWhiteSpace(qType))
|
||||||
|
results = results.Where(f => !string.IsNullOrWhiteSpace(f.Type) && f.Type.Trim().Contains(qType, StringComparison.OrdinalIgnoreCase));
|
||||||
|
if (!string.IsNullOrWhiteSpace(qTag))
|
||||||
|
results = results.Where(f => f.Tags?.Any(t => !string.IsNullOrWhiteSpace(t) && t.Contains(qTag, StringComparison.OrdinalIgnoreCase)) == true);
|
||||||
|
if (timeAfter.HasValue)
|
||||||
|
results = results.Where(f => f.Time > timeAfter.Value);
|
||||||
|
|
||||||
|
return [.. results];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -119,7 +119,7 @@ internal static partial class Program
|
|||||||
Assert(current.EmbeddingApiUrl == "http://127.0.0.1:8086/v1/embeddings", "Config EmbeddingApiUrl default mismatch.");
|
Assert(current.EmbeddingApiUrl == "http://127.0.0.1:8086/v1/embeddings", "Config EmbeddingApiUrl default mismatch.");
|
||||||
Assert(current.SpeechRecognitionEngine == "whisper", "Config SpeechRecognitionEngine default mismatch.");
|
Assert(current.SpeechRecognitionEngine == "whisper", "Config SpeechRecognitionEngine default mismatch.");
|
||||||
Assert(current.WhisperModelSize == "base", "Config WhisperModelSize default mismatch.");
|
Assert(current.WhisperModelSize == "base", "Config WhisperModelSize default mismatch.");
|
||||||
Assert(current.AiProvider == "llamasharp", "Config AiProvider default mismatch.");
|
Assert(current.AiProvider == "none", "Config AiProvider default mismatch.");
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,11 +2,7 @@ internal static partial class Program
|
|||||||
{
|
{
|
||||||
static FragmentService NewService()
|
static FragmentService NewService()
|
||||||
{
|
{
|
||||||
var config = NewConfigService();
|
IFragmentRepository repo = new InMemoryFragmentRepository();
|
||||||
var dbService = new JournalDatabaseService(config);
|
|
||||||
var session = new DatabaseSessionService(dbService);
|
|
||||||
session.SetPassword("smoke-test-password");
|
|
||||||
var repo = new SqliteFragmentRepository(session);
|
|
||||||
return new FragmentService(repo);
|
return new FragmentService(repo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user