From c3ba9fa85a620950329b9b378e0c360c1fac8bae Mon Sep 17 00:00:00 2001 From: moreal Date: Thu, 16 May 2024 04:12:40 +0900 Subject: [PATCH] feat(api): provide tip index --- Mimir/Controllers/MetadataController.cs | 19 +++++++++++++++++ Mimir/Models/Metadata/GetTipResponse.cs | 5 +++++ Mimir/Program.cs | 1 + Mimir/Repositories/MetadataRepository.cs | 26 ++++++++++++++++++++++++ 4 files changed, 51 insertions(+) create mode 100644 Mimir/Controllers/MetadataController.cs create mode 100644 Mimir/Models/Metadata/GetTipResponse.cs create mode 100644 Mimir/Repositories/MetadataRepository.cs 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; + } +}