< Summary

Information
Class: MRA.Infrastructure.Database.Providers.FirestoreDatabase
Assembly: MRA.Infrastructure
File(s): D:\a\MiguelRomerART\MiguelRomerART\MRA.Infrastructure\Database\Providers\FirestoreDatabase.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 42
Coverable lines: 42
Total lines: 105
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
.ctor(...)100%210%
get_FirestoreDb()0%620%
InitializeFirestore()100%210%
LoadCredentials()0%620%
Create()100%210%
GetAllDocumentsAsync()0%620%
DocumentExistsAsync()100%210%
GetDocumentAsync()100%210%
SetDocumentAsync()100%210%
DeleteDocumentAsync()100%210%

File(s)

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

#LineLine coverage
 1using Google.Cloud.Firestore;
 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 FirestoreDatabase : IDocumentsDatabase
 9    {
 10        private const string ENV_GOOGLE_CREDENTIALS_AZURE = "GOOGLE_APPLICATION_CREDENTIALS_JSON";
 11        private const string ENV_GOOGLE_CREDENTIALS = "GOOGLE_APPLICATION_CREDENTIALS";
 12
 13        private readonly string _projectId;
 014        private string _serviceAccountPath = "";
 15
 16        private FirestoreDb _firestoreDb;
 17        private FirestoreDb FirestoreDb
 18        {
 19            get
 20            {
 021                if (_firestoreDb == null)
 022                    InitializeFirestore();
 23
 024                return _firestoreDb;
 25            }
 26        }
 27
 028        public FirestoreDatabase(AppSettings appConfig)
 29        {
 030            _projectId = appConfig.Firebase.ProjectID;
 031            _serviceAccountPath = appConfig.Firebase.CredentialsPath;
 032        }
 33
 34
 35        public void InitializeFirestore()
 36        {
 037            LoadCredentials();
 038            Create();
 039        }
 40
 41        private void LoadCredentials()
 42        {
 043            var googleCredentialsJson = Environment.GetEnvironmentVariable(ENV_GOOGLE_CREDENTIALS_AZURE);
 044            if (!string.IsNullOrEmpty(googleCredentialsJson))
 45            {
 046                var tempCredentialPath = Path.Combine(Path.GetTempPath(), "firebase-credentials.json");
 047                File.WriteAllText(tempCredentialPath, googleCredentialsJson);
 48
 049                _serviceAccountPath = tempCredentialPath;
 50            }
 51
 052            Environment.SetEnvironmentVariable(ENV_GOOGLE_CREDENTIALS, _serviceAccountPath);
 053        }
 54
 55        private void Create()
 56        {
 057            _firestoreDb = FirestoreDb.Create(_projectId);
 058        }
 59
 60        public async Task<IEnumerable<IDocument>> GetAllDocumentsAsync<IDocument>(string collection) =>
 061            (await FirestoreDb.Collection(collection).GetSnapshotAsync())
 062                .Documents.Select(s => s.ConvertTo<IDocument>());
 63
 64        public async Task<bool> DocumentExistsAsync(string collection, string documentId)
 65        {
 066            DocumentReference docRef = FirestoreDb.Collection(collection).Document(documentId);
 067            DocumentSnapshot snapshot = await docRef.GetSnapshotAsync();
 068            return snapshot.Exists;
 069        }
 70
 71        public async Task<IDocument> GetDocumentAsync<IDocument>(string collection, string documentId)
 72        {
 073            DocumentReference docRef = FirestoreDb.Collection(collection).Document(documentId);
 074            DocumentSnapshot snapshot = await docRef.GetSnapshotAsync();
 075            return snapshot.ConvertTo<IDocument>();
 076        }
 77
 78        public async Task<bool> SetDocumentAsync(string collection, string documentId, IDocument document)
 79        {
 80            try
 81            {
 082                DocumentReference docRef = FirestoreDb.Collection(collection).Document(documentId);
 083                await docRef.SetAsync(document);
 084                return true;
 085            }catch(Exception ex)
 86            {
 087                return false;
 88            }
 089        }
 90
 91        public async Task<bool> DeleteDocumentAsync(string collection, string id)
 92        {
 93            try
 94            {
 095                DocumentReference docRef = FirestoreDb.Collection(collection).Document(id);
 096                await docRef.DeleteAsync();
 097                return true;
 98            }
 099            catch (Exception ex)
 100            {
 0101                return false;
 102            }
 0103        }
 104    }
 105}