Skip to content

Commit

Permalink
handle 404 of avatars/{avatarAddress}/inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
boscohyun committed May 7, 2024
1 parent 3db137c commit a5afe5e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
13 changes: 11 additions & 2 deletions NineChroniclesUtilBackend/Controllers/AvatarController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ namespace NineChroniclesUtilBackend.Controllers;
public class AvatarController(AvatarRepository avatarRepository) : ControllerBase
{
[HttpGet("{avatarAddress}/inventory")]
public Inventory GetInventory(string avatarAddress) =>
avatarRepository.GetInventory(avatarAddress);
public Inventory? GetInventory(string avatarAddress)
{
var inventory = avatarRepository.GetInventory(avatarAddress);
if (inventory is null)
{
Response.StatusCode = StatusCodes.Status404NotFound;
return null;
}

return inventory;
}
}
7 changes: 6 additions & 1 deletion NineChroniclesUtilBackend/Repositories/AvatarRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ public class AvatarRepository(MongoDBCollectionService mongoDBCollectionService)
private readonly IMongoCollection<BsonDocument> _avatarsCollection =
mongoDBCollectionService.GetCollection<BsonDocument>("avatars");

public Inventory GetInventory(string avatarAddress)
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();
if (document is null)
{
return null;
}

return new Inventory(document["Avatar"]["inventory"]);
}
}

0 comments on commit a5afe5e

Please sign in to comment.