Skip to content

Commit

Permalink
Add table sheets apis (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
Atralupus authored May 10, 2024
1 parent 10d21d6 commit fe555e0
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Mimir/Controllers/TableSheetsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Libplanet.Crypto;
using Microsoft.AspNetCore.Mvc;
using Mimir.Models.Agent;
using Mimir.Repositories;
using Mimir.Services;
using Mimir.Util;

namespace Mimir.Controllers;

[ApiController]
[Route("{network}/sheets")]
public class TableSheetsController(TableSheetsRepository tableSheetsRepository) : ControllerBase
{
[HttpGet("names")]
public async Task<string[]> GetSheetNames(
string network
)
{
var sheetNames = tableSheetsRepository.GetSheetNames(network);

return sheetNames;
}

[HttpGet("{sheetName}")]
public async Task<ContentResult> GetSheet(
string network,
string sheetName
)
{
var sheet = tableSheetsRepository.GetSheet(network, sheetName);

return Content(sheet, "application/json");;
}
}
1 change: 1 addition & 0 deletions Mimir/Mimir.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.1" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.3.1" />
<PackageReference Include="MongoDB.Driver" Version="2.24.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Lib9c" Version="1.8.0-dev.c6a17ceb8856f4c7c6bdc7a7288be58479d9d006" />
<PackageReference Include="Libplanet" Version="4.1.0-dev.20242745721" />
Expand Down
1 change: 1 addition & 0 deletions Mimir/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
builder.Services.AddSingleton<IStateService, HeadlessStateService>();
builder.Services.AddSingleton<MongoDBCollectionService>();
builder.Services.AddSingleton<ArenaRankingRepository>();
builder.Services.AddSingleton<TableSheetsRepository>();
builder.Services.AddSingleton<AvatarRepository>();
builder.Services.AddControllers();
builder.Services.AddHeadlessGQLClient()
Expand Down
56 changes: 56 additions & 0 deletions Mimir/Repositories/TableSheetsRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Mimir.Models.Avatar;
using Mimir.Services;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Driver;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Mimir.Repositories;

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

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

public string[] GetSheetNames(string network)
{
var collection = GetCollection(network);

var projection = Builders<BsonDocument>.Projection.Include("Name").Exclude("_id");
var documents = collection.Find(new BsonDocument()).Project(projection).ToList();

List<string> names = new List<string>();
foreach (var document in documents)
{
if (document.Contains("Name"))
{
names.Add(document["Name"].AsString);
}
}

return names.ToArray();
}

public string GetSheet(string network, string sheetName)
{
var collection = GetCollection(network);

var projection = Builders<BsonDocument>.Projection.Include("Sheet").Exclude("_id");

var filter = Builders<BsonDocument>.Filter.Eq("Name", sheetName);
var document = collection.Find(filter).Project(projection).FirstOrDefault();

if (document == null)
{
return "{}";
}

return document["Sheet"].ToJson(new JsonWriterSettings { OutputMode = JsonOutputMode.Strict });
}
}

0 comments on commit fe555e0

Please sign in to comment.