| | 1 | | using Microsoft.Azure.Cosmos; |
| | 2 | | using MRA.Infrastructure.Settings; |
| | 3 | | using MRA.Infrastructure.Database.Documents.Interfaces; |
| | 4 | | using MRA.Infrastructure.Database.Providers.Interfaces; |
| | 5 | |
|
| | 6 | | namespace MRA.Infrastructure.Database.Providers |
| | 7 | | { |
| | 8 | | public class AzureCosmosDbDatabase : IDocumentsDatabase |
| | 9 | | { |
| | 10 | | private readonly AppSettings _appConfiguration; |
| | 11 | |
|
| | 12 | | private CosmosClient _cosmosClient; |
| | 13 | | private CosmosClient CosmosClient |
| | 14 | | { |
| | 15 | | get |
| | 16 | | { |
| 0 | 17 | | if (_cosmosClient == null) |
| 0 | 18 | | InitializeCosmosDb(); |
| | 19 | |
|
| 0 | 20 | | return _cosmosClient; |
| | 21 | | } |
| | 22 | | } |
| | 23 | |
|
| 0 | 24 | | public AzureCosmosDbDatabase(AppSettings appConfig) |
| | 25 | | { |
| 0 | 26 | | _appConfiguration = appConfig; |
| 0 | 27 | | } |
| | 28 | |
|
| | 29 | | private void InitializeCosmosDb() |
| | 30 | | { |
| 0 | 31 | | CosmosClientOptions options = |
| 0 | 32 | | new CosmosClientOptions |
| 0 | 33 | | { |
| 0 | 34 | | ConnectionMode = ConnectionMode.Gateway, |
| 0 | 35 | | RequestTimeout = TimeSpan.FromSeconds(_appConfiguration.AzureCosmosDb.TimeoutSeconds), |
| 0 | 36 | | }; |
| | 37 | |
|
| 0 | 38 | | _cosmosClient = new CosmosClient(_appConfiguration.AzureCosmosDb.ConnectionString, options); |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | public async Task<IEnumerable<IDocument>> GetAllDocumentsAsync<IDocument>(string collection) |
| | 42 | | { |
| 0 | 43 | | var query = CosmosClient.GetContainer(_appConfiguration.AzureCosmosDb.DatabaseName, collection).GetItemQuery |
| | 44 | |
|
| 0 | 45 | | var results = new List<IDocument>(); |
| 0 | 46 | | while (query.HasMoreResults) |
| | 47 | | { |
| 0 | 48 | | var response = await query.ReadNextAsync(); |
| 0 | 49 | | results.AddRange(response); |
| | 50 | | } |
| 0 | 51 | | return results; |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | public async Task<IDocument> GetDocumentAsync<IDocument>(string collection, string documentId) |
| | 55 | | { |
| | 56 | | try |
| | 57 | | { |
| 0 | 58 | | var response = await CosmosClient.GetContainer(_appConfiguration.AzureCosmosDb.DatabaseName, collection) |
| 0 | 59 | | return response.Resource; |
| | 60 | | } |
| 0 | 61 | | catch (CosmosException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound) |
| | 62 | | { |
| 0 | 63 | | return default; |
| | 64 | | } |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | public async Task<bool> DocumentExistsAsync(string collection, string documentId) |
| | 68 | | { |
| 0 | 69 | | var document = await GetDocumentAsync<IDocument>(collection, documentId); |
| 0 | 70 | | return document != null; |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | public async Task<bool> SetDocumentAsync(string collection, string documentId, IDocument document) |
| | 74 | | { |
| 0 | 75 | | var response = await CosmosClient.GetContainer(_appConfiguration.AzureCosmosDb.DatabaseName, collection).Ups |
| 0 | 76 | | return response.StatusCode == System.Net.HttpStatusCode.OK || response.StatusCode == System.Net.HttpStatusCo |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | public async Task<bool> DeleteDocumentAsync(string collection, string id) |
| | 80 | | { |
| | 81 | | try |
| | 82 | | { |
| 0 | 83 | | var response = await CosmosClient.GetContainer(_appConfiguration.AzureCosmosDb.DatabaseName, collection) |
| 0 | 84 | | return response.StatusCode == System.Net.HttpStatusCode.NoContent; |
| | 85 | | } |
| 0 | 86 | | catch (CosmosException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound) |
| | 87 | | { |
| 0 | 88 | | return false; |
| | 89 | | } |
| 0 | 90 | | } |
| | 91 | | } |
| | 92 | | } |