| | 1 | | using Google.Cloud.Firestore; |
| | 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 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; |
| 0 | 14 | | private string _serviceAccountPath = ""; |
| | 15 | |
|
| | 16 | | private FirestoreDb _firestoreDb; |
| | 17 | | private FirestoreDb FirestoreDb |
| | 18 | | { |
| | 19 | | get |
| | 20 | | { |
| 0 | 21 | | if (_firestoreDb == null) |
| 0 | 22 | | InitializeFirestore(); |
| | 23 | |
|
| 0 | 24 | | return _firestoreDb; |
| | 25 | | } |
| | 26 | | } |
| | 27 | |
|
| 0 | 28 | | public FirestoreDatabase(AppSettings appConfig) |
| | 29 | | { |
| 0 | 30 | | _projectId = appConfig.Firebase.ProjectID; |
| 0 | 31 | | _serviceAccountPath = appConfig.Firebase.CredentialsPath; |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | |
|
| | 35 | | public void InitializeFirestore() |
| | 36 | | { |
| 0 | 37 | | LoadCredentials(); |
| 0 | 38 | | Create(); |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | private void LoadCredentials() |
| | 42 | | { |
| 0 | 43 | | var googleCredentialsJson = Environment.GetEnvironmentVariable(ENV_GOOGLE_CREDENTIALS_AZURE); |
| 0 | 44 | | if (!string.IsNullOrEmpty(googleCredentialsJson)) |
| | 45 | | { |
| 0 | 46 | | var tempCredentialPath = Path.Combine(Path.GetTempPath(), "firebase-credentials.json"); |
| 0 | 47 | | File.WriteAllText(tempCredentialPath, googleCredentialsJson); |
| | 48 | |
|
| 0 | 49 | | _serviceAccountPath = tempCredentialPath; |
| | 50 | | } |
| | 51 | |
|
| 0 | 52 | | Environment.SetEnvironmentVariable(ENV_GOOGLE_CREDENTIALS, _serviceAccountPath); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | private void Create() |
| | 56 | | { |
| 0 | 57 | | _firestoreDb = FirestoreDb.Create(_projectId); |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | public async Task<IEnumerable<IDocument>> GetAllDocumentsAsync<IDocument>(string collection) => |
| 0 | 61 | | (await FirestoreDb.Collection(collection).GetSnapshotAsync()) |
| 0 | 62 | | .Documents.Select(s => s.ConvertTo<IDocument>()); |
| | 63 | |
|
| | 64 | | public async Task<bool> DocumentExistsAsync(string collection, string documentId) |
| | 65 | | { |
| 0 | 66 | | DocumentReference docRef = FirestoreDb.Collection(collection).Document(documentId); |
| 0 | 67 | | DocumentSnapshot snapshot = await docRef.GetSnapshotAsync(); |
| 0 | 68 | | return snapshot.Exists; |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | public async Task<IDocument> GetDocumentAsync<IDocument>(string collection, string documentId) |
| | 72 | | { |
| 0 | 73 | | DocumentReference docRef = FirestoreDb.Collection(collection).Document(documentId); |
| 0 | 74 | | DocumentSnapshot snapshot = await docRef.GetSnapshotAsync(); |
| 0 | 75 | | return snapshot.ConvertTo<IDocument>(); |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | public async Task<bool> SetDocumentAsync(string collection, string documentId, IDocument document) |
| | 79 | | { |
| | 80 | | try |
| | 81 | | { |
| 0 | 82 | | DocumentReference docRef = FirestoreDb.Collection(collection).Document(documentId); |
| 0 | 83 | | await docRef.SetAsync(document); |
| 0 | 84 | | return true; |
| 0 | 85 | | }catch(Exception ex) |
| | 86 | | { |
| 0 | 87 | | return false; |
| | 88 | | } |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | public async Task<bool> DeleteDocumentAsync(string collection, string id) |
| | 92 | | { |
| | 93 | | try |
| | 94 | | { |
| 0 | 95 | | DocumentReference docRef = FirestoreDb.Collection(collection).Document(id); |
| 0 | 96 | | await docRef.DeleteAsync(); |
| 0 | 97 | | return true; |
| | 98 | | } |
| 0 | 99 | | catch (Exception ex) |
| | 100 | | { |
| 0 | 101 | | return false; |
| | 102 | | } |
| 0 | 103 | | } |
| | 104 | | } |
| | 105 | | } |