Skip to content

Commit

Permalink
improve inventory api
Browse files Browse the repository at this point in the history
  • Loading branch information
boscohyun committed May 13, 2024
1 parent 0d9d0c3 commit 1a12f70
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions Mimir/Controllers/AvatarController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
using System.Numerics;
using Bencodex;
using Libplanet.Common;
using Libplanet.Crypto;
using Microsoft.AspNetCore.Mvc;
using Mimir.Models.Avatar;
using Mimir.Repositories;
using Mimir.Services;
using Mimir.Util;
using MongoDB.Bson;
using Nekoyume.Model.State;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Mimir.Controllers;

Expand Down Expand Up @@ -90,15 +100,37 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer
#endregion snippets

[HttpGet("{avatarAddress}/inventory")]
public Inventory? GetInventory(string network, string avatarAddress)
public async Task<Inventory?> GetInventory(
string network,
string avatarAddress,
IStateService stateService)
{
var inventory = avatarRepository.GetInventory(network, avatarAddress);
if (inventory is null)
if (inventory is not null)
{
return inventory;
}

var stateGetter = new StateGetter(stateService);
var inventoryState = await stateGetter.GetInventoryStateAsync(new Address(avatarAddress));
if (inventoryState is null)
{
Response.StatusCode = StatusCodes.Status404NotFound;
return null;
}

try
{
var jsonString = JsonConvert.SerializeObject(inventoryState, JsonSerializerSettings);
var bsonDocument = BsonDocument.Parse(jsonString);
inventory = new Inventory(bsonDocument);
}
catch
{
Response.StatusCode = StatusCodes.Status500InternalServerError;
return null;
}

return inventory;
}
}

0 comments on commit 1a12f70

Please sign in to comment.