-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from planetarium/implment-more-api
Implement `avatars/{address}/inventory` API
- Loading branch information
Showing
10 changed files
with
105 additions
and
32 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
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,14 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using NineChroniclesUtilBackend.Models.Avatar; | ||
using NineChroniclesUtilBackend.Repositories; | ||
|
||
namespace NineChroniclesUtilBackend.Controllers; | ||
|
||
[ApiController] | ||
[Route("avatars")] | ||
public class AvatarController(AvatarRepository avatarRepository) : ControllerBase | ||
{ | ||
[HttpGet("{avatarAddress}/inventory")] | ||
public Inventory GetInventory(string avatarAddress) => | ||
avatarRepository.GetInventory(avatarAddress); | ||
} |
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,11 @@ | ||
using MongoDB.Bson; | ||
using NineChroniclesUtilBackend.Models.Items; | ||
|
||
namespace NineChroniclesUtilBackend.Models.Avatar; | ||
|
||
public class Inventory(BsonValue inventory) | ||
{ | ||
public List<Equipment> Equipments { get; set; } = inventory["Equipments"].AsBsonArray | ||
.Select(e => new Equipment(e.AsBsonDocument)) | ||
.ToList(); | ||
} |
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,8 @@ | ||
using MongoDB.Bson; | ||
|
||
namespace NineChroniclesUtilBackend.Models.Items; | ||
|
||
public class Equipment(BsonValue equipment) : NonFungibleItem(equipment) | ||
{ | ||
public int Level { get; set; } = equipment["level"].AsInt32; | ||
} |
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,9 @@ | ||
using MongoDB.Bson; | ||
using Nekoyume.Model.Item; | ||
|
||
namespace NineChroniclesUtilBackend.Models.Items; | ||
|
||
public class Item(BsonValue item) | ||
{ | ||
public ItemSubType ItemSubType { get; set; } = (ItemSubType)item["ItemSubType"].AsInt32; | ||
} |
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,11 @@ | ||
using MongoDB.Bson; | ||
|
||
namespace NineChroniclesUtilBackend.Models.Items; | ||
|
||
public class NonFungibleItem(BsonValue nonFungibleItem) : Item(nonFungibleItem) | ||
{ | ||
public Guid NonFungibleId { get; set; } = | ||
Guid.TryParse(nonFungibleItem["NonFungibleId"].AsString, out var nonFungibleId) | ||
? nonFungibleId | ||
: Guid.Empty; | ||
} |
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
20 changes: 20 additions & 0 deletions
20
NineChroniclesUtilBackend/Repositories/AvatarRepository.cs
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,20 @@ | ||
using MongoDB.Bson; | ||
using MongoDB.Driver; | ||
using NineChroniclesUtilBackend.Models.Avatar; | ||
using NineChroniclesUtilBackend.Services; | ||
|
||
namespace NineChroniclesUtilBackend.Repositories; | ||
|
||
public class AvatarRepository(MongoDBCollectionService mongoDBCollectionService) | ||
{ | ||
private readonly IMongoCollection<BsonDocument> _avatarsCollection = | ||
mongoDBCollectionService.GetCollection<BsonDocument>("avatars"); | ||
|
||
public Inventory GetInventory(string avatarAddress) | ||
{ | ||
var filter = Builders<BsonDocument>.Filter.Eq(f => f["Avatar"]["address"], avatarAddress); | ||
var projection = Builders<BsonDocument>.Projection.Include(f => f["Avatar"]["inventory"]["Equipments"]); | ||
var document = _avatarsCollection.Find(filter).Project(projection).FirstOrDefault(); | ||
return new Inventory(document["Avatar"]["inventory"]); | ||
} | ||
} |
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