-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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");; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} |