Files
Rafał Tyński b3756238a3 feat(backend): restructure API into layers with OpenAPI, versioning and error handling
Split the backend into API host, Domain, Application and Infrastructure projects. Add Swagger/OpenAPI per API version, Asp.Versioning (query/header), ProblemDetails with a global exception handler and SurrealQL migrations applied at startup (namespace/database ensured). Infrastructure owns the SurrealDB client, settings and DI.
2026-10-04 01:49:51 +02:00

59 lines
1.7 KiB
C#

using Microsoft.Extensions.Options;
using SurrealDb.Net;
using TarninaHub.Application.Abstractions;
using TarninaHub.Application.Dtos;
using TarninaHub.Infrastructure.Settings;
namespace TarninaHub.Infrastructure.Persistence;
public sealed class StorageService : IStorageService
{
private const string ProbeTable = "storage_probe";
private readonly ISurrealDbClient _client;
private readonly SurrealDbSettings _settings;
private readonly string _dataDirectory;
public StorageService(ISurrealDbClient client, IOptions<SurrealDbSettings> settings)
{
_client = client;
_settings = settings.Value;
_dataDirectory = Path.Combine(AppContext.BaseDirectory, _settings.DataDirectory);
}
public async Task InitializeAsync(CancellationToken cancellationToken)
{
await _client.RawQuery(
$"DEFINE TABLE IF NOT EXISTS {ProbeTable} SCHEMALESS;",
cancellationToken: cancellationToken);
}
public StorageInfo GetInfo()
{
var isRemote = !string.IsNullOrWhiteSpace(_settings.RemoteUrl);
return new StorageInfo(
isRemote ? "remote" : "rocksdb",
isRemote ? _settings.RemoteUrl! : _dataDirectory,
_settings.Namespace,
_settings.Database);
}
public async Task<int> GetProbeCountAsync(CancellationToken cancellationToken)
{
var probes = await _client.Select<StorageProbe>(ProbeTable, cancellationToken);
return probes.Count();
}
public Task<StorageProbe> AddProbeAsync(CancellationToken cancellationToken)
{
var probe = new StorageProbe
{
CreatedAt = DateTimeOffset.UtcNow,
};
return _client.Create(ProbeTable, probe, cancellationToken);
}
}