Skip to content

Commit

Permalink
Merge pull request #59 from moreal/tip
Browse files Browse the repository at this point in the history
feat(api): provide tip index
  • Loading branch information
moreal authored May 16, 2024
2 parents 9883016 + c3ba9fa commit 859182e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Mimir/Controllers/MetadataController.cs
Original file line number Diff line number Diff line change
@@ -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<GetTipResponse> GetTip(
string network,
MetadataRepository metadataRepository
)
{
return new GetTipResponse(await metadataRepository.GetLatestBlockIndex(network));
}
}
5 changes: 5 additions & 0 deletions Mimir/Models/Metadata/GetTipResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Mimir.Models.Metadata;

public record GetTipResponse(
long Index
);
1 change: 1 addition & 0 deletions Mimir/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
builder.Services.AddSingleton<ArenaRankingRepository>();
builder.Services.AddSingleton<TableSheetsRepository>();
builder.Services.AddSingleton<AvatarRepository>();
builder.Services.AddSingleton<MetadataRepository>();
builder.Services.AddControllers();
builder.Services.AddHeadlessGQLClient()
.ConfigureHttpClient((provider, client) =>
Expand Down
26 changes: 26 additions & 0 deletions Mimir/Repositories/MetadataRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Mimir.Models.Avatar;
using Mimir.Services;
using MongoDB.Bson;
using MongoDB.Driver;

namespace Mimir.Repositories;

public class MetadataRepository : BaseRepository<BsonDocument>
{
public MetadataRepository(MongoDBCollectionService mongoDBCollectionService)
: base(mongoDBCollectionService)
{
}

protected override string GetCollectionName()
{
return "metadata";
}

public async Task<long> GetLatestBlockIndex(string network)
{
var filter = Builders<BsonDocument>.Filter.Eq("_id", "SyncContext");
var doc = await GetCollection(network).FindSync(filter).FirstAsync();
return doc.GetValue("LatestBlockIndex").AsInt64;
}
}

0 comments on commit 859182e

Please sign in to comment.