| | 1 | | using DnsClient.Internal; |
| | 2 | | using Microsoft.Extensions.Logging; |
| | 3 | | using MongoDB.Bson; |
| | 4 | | using MongoDB.Bson.Serialization; |
| | 5 | | using MongoDB.Driver; |
| | 6 | | using MRA.Infrastructure.Settings; |
| | 7 | | using MRA.Infrastructure.Database.Documents.Interfaces; |
| | 8 | | using MRA.Infrastructure.Database.Documents.MongoDb; |
| | 9 | | using MRA.Infrastructure.Database.Providers.Interfaces; |
| | 10 | |
|
| | 11 | | namespace MRA.Infrastructure.Database.Providers; |
| | 12 | |
|
| | 13 | | public class MongoDbDatabase : IDocumentsDatabase |
| | 14 | | { |
| | 15 | | internal const string ID_FIELD = "_id"; |
| | 16 | |
|
| | 17 | | private readonly AppSettings _appConfiguration; |
| | 18 | | private readonly DocumentTypeRegistry _typeRegistry; |
| | 19 | | private readonly ILogger<MongoDbDatabase> _logger; |
| | 20 | |
|
| 1 | 21 | | private readonly object _initLock = new object(); |
| | 22 | | private static IMongoDatabase? _database = null; |
| | 23 | |
|
| | 24 | | private IMongoDatabase Database |
| | 25 | | { |
| | 26 | | get |
| | 27 | | { |
| 0 | 28 | | if (_database == null) |
| | 29 | | { |
| 0 | 30 | | lock (_initLock) |
| | 31 | | { |
| 0 | 32 | | if (_database == null) |
| | 33 | | { |
| 0 | 34 | | InitializeMongoDb(); |
| | 35 | | } |
| 0 | 36 | | } |
| | 37 | | } |
| | 38 | |
|
| 0 | 39 | | return _database; |
| | 40 | | } |
| | 41 | | } |
| | 42 | |
|
| 1 | 43 | | public MongoDbDatabase(AppSettings appConfig, ILogger<MongoDbDatabase> logger) |
| | 44 | | { |
| 1 | 45 | | _appConfiguration = appConfig; |
| 1 | 46 | | _logger = logger; |
| 1 | 47 | | _typeRegistry = new DocumentTypeRegistry(appConfig); |
| 1 | 48 | | } |
| | 49 | |
|
| | 50 | | private void InitializeMongoDb() |
| | 51 | | { |
| 0 | 52 | | _logger.LogInformation("Initializing MongoDb Database. Registering document classes."); |
| 0 | 53 | | RegisterBsonClass(typeof(InspirationMongoDocument)); |
| 0 | 54 | | RegisterBsonClass(typeof(DrawingMongoDocument)); |
| 0 | 55 | | RegisterBsonClass(typeof(CollectionMongoDocument)); |
| | 56 | |
|
| 0 | 57 | | var mongoClient = new MongoClient(_appConfiguration.AzureCosmosDb.ConnectionString); |
| 0 | 58 | | _database = mongoClient.GetDatabase(_appConfiguration.AzureCosmosDb.DatabaseName); |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | private void RegisterBsonClass(Type documentType) |
| | 62 | | { |
| 0 | 63 | | if (!BsonClassMap.IsClassMapRegistered(documentType)) |
| | 64 | | { |
| 0 | 65 | | var classMap = new BsonClassMap(documentType); |
| 0 | 66 | | classMap.AutoMap(); |
| 0 | 67 | | classMap.SetIgnoreExtraElements(true); |
| 0 | 68 | | BsonClassMap.RegisterClassMap(classMap); |
| | 69 | | } |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | public async Task<IEnumerable<IDocument>> GetAllDocumentsAsync<IDocument>(string collection) |
| | 73 | | { |
| 0 | 74 | | var mongoCollection = Database.GetCollection<object>(collection); |
| 0 | 75 | | var documents = await mongoCollection.Find(FilterDefinition<object>.Empty).ToListAsync(); |
| | 76 | |
|
| 0 | 77 | | var deserializedDocuments = new List<IDocument>(); |
| 0 | 78 | | foreach (var doc in documents) |
| | 79 | | { |
| 0 | 80 | | var deserialized = (IDocument)BsonSerializer.Deserialize(doc.ToBson(), GetDocumentType(collection)); |
| 0 | 81 | | deserializedDocuments.Add(deserialized); |
| | 82 | | } |
| | 83 | |
|
| 0 | 84 | | return deserializedDocuments; |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | public async Task<IDocument> GetDocumentAsync<IDocument>(string collection, string documentId) |
| | 88 | | { |
| 0 | 89 | | var mongoCollection = Database.GetCollection<object>(collection); |
| | 90 | |
|
| 0 | 91 | | var filter = Builders<object>.Filter.Eq(ID_FIELD, documentId); |
| 0 | 92 | | var document = await mongoCollection.Find(filter).FirstOrDefaultAsync(); |
| | 93 | |
|
| 0 | 94 | | if (document == null) |
| 0 | 95 | | return default; |
| | 96 | |
|
| 0 | 97 | | return (IDocument)BsonSerializer.Deserialize(document.ToBson(), GetDocumentType(collection)); |
| 0 | 98 | | } |
| | 99 | |
|
| 0 | 100 | | private Type GetDocumentType(string collection) => _typeRegistry.GetDocumentType(collection); |
| | 101 | |
|
| | 102 | |
|
| | 103 | | public async Task<bool> DocumentExistsAsync(string collection, string documentId) |
| | 104 | | { |
| 0 | 105 | | var mongoCollection = Database.GetCollection<object>(collection); |
| 0 | 106 | | var filter = Builders<object>.Filter.Eq(ID_FIELD, documentId); |
| 0 | 107 | | return await mongoCollection.Find(filter).AnyAsync(); |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | public async Task<bool> SetDocumentAsync(string collection, string documentId, IDocument document) |
| | 111 | | { |
| 0 | 112 | | if(document is not IMongoDocument mongoDocument) |
| | 113 | | { |
| 0 | 114 | | throw new InvalidOperationException($"Document type mismatch. Can't save to MongoDb"); |
| | 115 | | } |
| | 116 | |
|
| 0 | 117 | | var result = await mongoDocument.SetDocumentAsync(Database, collection, documentId); |
| 0 | 118 | | return result.ModifiedCount > 0 || result.UpsertedId != null; |
| 0 | 119 | | } |
| | 120 | |
|
| | 121 | | public async Task<bool> DeleteDocumentAsync(string collection, string id) |
| | 122 | | { |
| 0 | 123 | | var mongoCollection = Database.GetCollection<object>(collection); |
| 0 | 124 | | var filter = Builders<object>.Filter.Eq(ID_FIELD, id); |
| 0 | 125 | | var result = await mongoCollection.DeleteOneAsync(filter); |
| | 126 | |
|
| 0 | 127 | | return result.DeletedCount > 0; |
| 0 | 128 | | } |
| | 129 | | } |