< Summary

Information
Class: MRA.Infrastructure.Database.Providers.AzureCosmosDbDatabase
Assembly: MRA.Infrastructure
File(s): D:\a\MiguelRomerART\MiguelRomerART\MRA.Infrastructure\Database\Providers\AzureCosmosDbDatabase.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 37
Coverable lines: 37
Total lines: 92
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_CosmosClient()0%620%
.ctor(...)100%210%
InitializeCosmosDb()100%210%
GetAllDocumentsAsync()0%620%
GetDocumentAsync()100%210%
DocumentExistsAsync()100%210%
SetDocumentAsync()0%620%
DeleteDocumentAsync()100%210%

File(s)

D:\a\MiguelRomerART\MiguelRomerART\MRA.Infrastructure\Database\Providers\AzureCosmosDbDatabase.cs

#LineLine coverage
 1using Microsoft.Azure.Cosmos;
 2using MRA.Infrastructure.Settings;
 3using MRA.Infrastructure.Database.Documents.Interfaces;
 4using MRA.Infrastructure.Database.Providers.Interfaces;
 5
 6namespace 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            {
 017                if (_cosmosClient == null)
 018                    InitializeCosmosDb();
 19
 020                return _cosmosClient;
 21            }
 22        }
 23
 024        public AzureCosmosDbDatabase(AppSettings appConfig)
 25        {
 026            _appConfiguration = appConfig;
 027        }
 28
 29        private void InitializeCosmosDb()
 30        {
 031            CosmosClientOptions options =
 032                new CosmosClientOptions
 033                {
 034                    ConnectionMode = ConnectionMode.Gateway,
 035                    RequestTimeout = TimeSpan.FromSeconds(_appConfiguration.AzureCosmosDb.TimeoutSeconds),
 036                };
 37
 038            _cosmosClient = new CosmosClient(_appConfiguration.AzureCosmosDb.ConnectionString, options);
 039        }
 40
 41        public async Task<IEnumerable<IDocument>> GetAllDocumentsAsync<IDocument>(string collection)
 42        {
 043            var query = CosmosClient.GetContainer(_appConfiguration.AzureCosmosDb.DatabaseName, collection).GetItemQuery
 44
 045            var results = new List<IDocument>();
 046            while (query.HasMoreResults)
 47            {
 048                var response = await query.ReadNextAsync();
 049                results.AddRange(response);
 50            }
 051            return results;
 052        }
 53
 54        public async Task<IDocument> GetDocumentAsync<IDocument>(string collection, string documentId)
 55        {
 56            try
 57            {
 058                var response = await CosmosClient.GetContainer(_appConfiguration.AzureCosmosDb.DatabaseName, collection)
 059                return response.Resource;
 60            }
 061            catch (CosmosException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
 62            {
 063                return default;
 64            }
 065        }
 66
 67        public async Task<bool> DocumentExistsAsync(string collection, string documentId)
 68        {
 069            var document = await GetDocumentAsync<IDocument>(collection, documentId);
 070            return document != null;
 071        }
 72
 73        public async Task<bool> SetDocumentAsync(string collection, string documentId, IDocument document)
 74        {
 075            var response = await CosmosClient.GetContainer(_appConfiguration.AzureCosmosDb.DatabaseName, collection).Ups
 076            return response.StatusCode == System.Net.HttpStatusCode.OK || response.StatusCode == System.Net.HttpStatusCo
 077        }
 78
 79        public async Task<bool> DeleteDocumentAsync(string collection, string id)
 80        {
 81            try
 82            {
 083                var response = await CosmosClient.GetContainer(_appConfiguration.AzureCosmosDb.DatabaseName, collection)
 084                return response.StatusCode == System.Net.HttpStatusCode.NoContent;
 85            }
 086            catch (CosmosException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
 87            {
 088                return false;
 89            }
 090        }
 91    }
 92}