diff --git a/Mimir/Controllers/MetadataController.cs b/Mimir/Controllers/MetadataController.cs new file mode 100644 index 00000000..24791fd7 --- /dev/null +++ b/Mimir/Controllers/MetadataController.cs @@ -0,0 +1,19 @@ +using Microsoft.AspNetCore.Mvc; +using Mimir.Models.Metadata; +using Mimir.Repositories; + +namespace Mimir.Controllers; + +[ApiController] +[Route("{network}")] +public class MetadataController : ControllerBase +{ + [HttpGet("tip")] + public async Task GetTip( + string network, + MetadataRepository metadataRepository + ) + { + return new GetTipResponse(await metadataRepository.GetLatestBlockIndex(network)); + } +} diff --git a/Mimir/Models/Metadata/GetTipResponse.cs b/Mimir/Models/Metadata/GetTipResponse.cs new file mode 100644 index 00000000..d4156d2e --- /dev/null +++ b/Mimir/Models/Metadata/GetTipResponse.cs @@ -0,0 +1,5 @@ +namespace Mimir.Models.Metadata; + +public record GetTipResponse( + long Index +); \ No newline at end of file diff --git a/Mimir/Program.cs b/Mimir/Program.cs index aae4bfaf..fa1c2b5a 100644 --- a/Mimir/Program.cs +++ b/Mimir/Program.cs @@ -27,6 +27,7 @@ builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddControllers(); builder.Services.AddHeadlessGQLClient() .ConfigureHttpClient((provider, client) => diff --git a/Mimir/Repositories/MetadataRepository.cs b/Mimir/Repositories/MetadataRepository.cs new file mode 100644 index 00000000..bc274136 --- /dev/null +++ b/Mimir/Repositories/MetadataRepository.cs @@ -0,0 +1,26 @@ +using Mimir.Models.Avatar; +using Mimir.Services; +using MongoDB.Bson; +using MongoDB.Driver; + +namespace Mimir.Repositories; + +public class MetadataRepository : BaseRepository +{ + public MetadataRepository(MongoDBCollectionService mongoDBCollectionService) + : base(mongoDBCollectionService) + { + } + + protected override string GetCollectionName() + { + return "metadata"; + } + + public async Task GetLatestBlockIndex(string network) + { + var filter = Builders.Filter.Eq("_id", "SyncContext"); + var doc = await GetCollection(network).FindSync(filter).FirstAsync(); + return doc.GetValue("LatestBlockIndex").AsInt64; + } +}