Skip to content

Commit

Permalink
Merge pull request #568 from Atralupus/feat/item-slot-query
Browse files Browse the repository at this point in the history
Implement item slot query
  • Loading branch information
Atralupus authored Dec 22, 2024
2 parents 12e2f4b + f539b69 commit e35d564
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
project: [Mimir.Worker.Tests, Lib9c.Models.Tests, Mimir.MongoDB.Tests]
project: [Mimir.Worker.Tests, Lib9c.Models.Tests, Mimir.MongoDB.Tests, Mimir.Tests]
steps:
-
name: Checkout
Expand Down
20 changes: 9 additions & 11 deletions Lib9c.Models/States/ItemSlotState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@ public record ItemSlotState : IBencodable
public List<Guid> Costumes { get; init; }
public List<Guid> Equipments { get; init; }

public ItemSlotState() { }

[BsonIgnore, GraphQLIgnore, JsonIgnore]
public IValue Bencoded => List.Empty
.Add(BattleType.Serialize())
.Add(Costumes
.OrderBy(x => x)
.Select(x => x.Serialize())
.Serialize())
.Add(Equipments
.OrderBy(x => x)
.Select(x => x.Serialize())
.Serialize());
public IValue Bencoded =>
List
.Empty.Add(BattleType.Serialize())
.Add(Costumes.OrderBy(x => x).Select(x => x.Serialize()).Serialize())
.Add(Equipments.OrderBy(x => x).Select(x => x.Serialize()).Serialize());

public ItemSlotState(IValue bencoded)
{
Expand All @@ -38,7 +35,8 @@ public ItemSlotState(IValue bencoded)
throw new UnsupportedArgumentValueException<ValueKind>(
nameof(bencoded),
new[] { ValueKind.List },
bencoded.Kind);
bencoded.Kind
);
}

BattleType = l[0].ToEnum<BattleType>();
Expand Down
18 changes: 14 additions & 4 deletions Mimir.MongoDB/Repositories/ItemSlotRepository.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
using Libplanet.Crypto;
using Mimir.MongoDB.Exceptions;
using Mimir.MongoDB.Bson;
using Mimir.MongoDB.Exceptions;
using Mimir.MongoDB.Services;
using MongoDB.Driver;
using Nekoyume.Model.EnumType;

namespace Mimir.MongoDB.Repositories;

public interface IItemSlotRepository
{
Task<ItemSlotDocument> GetByAddressAsync(Address avatarAddress, BattleType battleType);
}

public class ItemSlotRepository(IMongoDbService dbService)
{
public async Task<ItemSlotDocument> GetByAddressAsync(Address avatarAddress, BattleType battleType)
public async Task<ItemSlotDocument> GetByAddressAsync(
Address avatarAddress,
BattleType battleType
)
{
var itemSlotAddress = Nekoyume.Model.State.ItemSlotState.DeriveAddress(
avatarAddress,
battleType);
battleType
);
var collectionName = CollectionNames.GetCollectionName<ItemSlotDocument>();
var collection = dbService.GetCollection<ItemSlotDocument>(collectionName);
var filter = Builders<ItemSlotDocument>.Filter.Eq("_id", itemSlotAddress.ToHex());
Expand All @@ -22,7 +31,8 @@ public async Task<ItemSlotDocument> GetByAddressAsync(Address avatarAddress, Bat
{
throw new DocumentNotFoundInMongoCollectionException(
collection.CollectionNamespace.CollectionName,
$"'Address' equals to '{itemSlotAddress.ToHex()}'");
$"'Address' equals to '{itemSlotAddress.ToHex()}'"
);
}

return document;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"data": {
"itemSlot": {
"battleType": "ADVENTURE",
"costumes": [
"00000000-0000-0000-0000-000000000000"
],
"equipments": [
"00000000-0000-0000-0000-000000000000"
]
}
}
}
48 changes: 48 additions & 0 deletions Mimir.Tests/QueryTests/ItemSlotTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Lib9c.Models.States;
using Libplanet.Crypto;
using Mimir.MongoDB.Bson;
using Mimir.MongoDB.Repositories;
using Moq;

namespace Mimir.Tests.QueryTests;

public class ItemSlotTest
{
[Fact]
public async Task GraphQL_Query_ItemSlot_Returns_CorrectValue()
{
var address = new Address("0x0000000001000000000200000000030000000004");
var state = new ItemSlotState
{
BattleType = Nekoyume.Model.EnumType.BattleType.Adventure,
Costumes = new List<Guid> { default },
Equipments = new List<Guid> { default }
};
var mockRepo = new Mock<IItemSlotRepository>();
mockRepo
.Setup(repo =>
repo.GetByAddressAsync(
It.IsAny<Address>(),
Nekoyume.Model.EnumType.BattleType.Adventure
)
)
.ReturnsAsync(new ItemSlotDocument(1, address, state));
var serviceProvider = TestServices.Builder.With(mockRepo.Object).Build();
var query = $$"""
query {
itemSlot(address: "{{address}}", battleType: ADVENTURE) {
battleType,
costumes,
equipments
}
}
""";

var result = await TestServices.ExecuteRequestAsync(
serviceProvider,
b => b.SetDocument(query)
);

await Verify(result);
}
}
Loading

0 comments on commit e35d564

Please sign in to comment.