-
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.
- Loading branch information
Showing
11 changed files
with
152 additions
and
64 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
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
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 |
---|---|---|
@@ -1,25 +1,27 @@ | ||
using MongoDB.Bson; | ||
using MongoDB.Driver; | ||
using Mimir.Models.Avatar; | ||
using Mimir.Services; | ||
using MongoDB.Bson; | ||
using MongoDB.Driver; | ||
|
||
namespace Mimir.Repositories; | ||
|
||
public class AvatarRepository(MongoDBCollectionService mongoDBCollectionService) | ||
public class AvatarRepository : BaseRepository<BsonDocument> | ||
{ | ||
private readonly IMongoCollection<BsonDocument> _avatarsCollection = | ||
mongoDBCollectionService.GetCollection<BsonDocument>("avatars"); | ||
public AvatarRepository(MongoDBCollectionService mongoDBCollectionService) | ||
: base(mongoDBCollectionService) { } | ||
|
||
public Inventory? GetInventory(string avatarAddress) | ||
protected override string GetCollectionName() | ||
{ | ||
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"]); | ||
return "avatars"; | ||
} | ||
|
||
public Inventory? GetInventory(string network, string avatarAddress) | ||
{ | ||
var collection = GetCollection(network); | ||
var filter = Builders<BsonDocument>.Filter.Eq("Avatar.address", avatarAddress); | ||
var projection = Builders<BsonDocument>.Projection.Include("Avatar.inventory.Equipments"); | ||
var document = collection.Find(filter).Project(projection).FirstOrDefault(); | ||
|
||
return document is null ? null : 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System.Collections.Generic; | ||
using Mimir.Services; | ||
using MongoDB.Bson; | ||
using MongoDB.Driver; | ||
|
||
namespace Mimir.Repositories; | ||
|
||
public abstract class BaseRepository<T> | ||
{ | ||
private readonly MongoDBCollectionService _mongoDBCollectionService; | ||
private readonly Dictionary<string, IMongoCollection<T>> _collections = new(); | ||
|
||
protected BaseRepository(MongoDBCollectionService mongoDBCollectionService) | ||
{ | ||
_mongoDBCollectionService = mongoDBCollectionService; | ||
|
||
var networks = new[] { "heimdall", "odin" }; | ||
var collectionName = GetCollectionName(); | ||
|
||
foreach (var network in networks) | ||
{ | ||
string databaseName = network switch | ||
{ | ||
"heimdall" => "heimdall", | ||
"odin" => "odin", | ||
_ => throw new ArgumentException("Invalid network name", nameof(network)) | ||
}; | ||
|
||
var collection = _mongoDBCollectionService.GetCollection<T>( | ||
collectionName, | ||
databaseName | ||
); | ||
_collections[network] = collection; | ||
} | ||
} | ||
|
||
protected abstract string GetCollectionName(); | ||
|
||
protected IMongoCollection<T> GetCollection(string network) | ||
{ | ||
if (_collections.TryGetValue(network, out var collection)) | ||
{ | ||
return collection; | ||
} | ||
|
||
throw new ArgumentException("Invalid network name", nameof(network)); | ||
} | ||
} |
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