| | | 1 | | using FirebaseAdapter.Configuration; |
| | | 2 | | using Google.Cloud.Firestore; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | using Microsoft.Extensions.Options; |
| | | 6 | | using Microsoft.Extensions.Configuration; |
| | | 7 | | using System.Text; |
| | | 8 | | using EHonda.KicktippAi.Core; |
| | | 9 | | |
| | | 10 | | namespace FirebaseAdapter; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Extension methods for configuring Firebase services in dependency injection. |
| | | 14 | | /// </summary> |
| | | 15 | | public static class ServiceCollectionExtensions |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Adds Firebase Firestore database services to the service collection. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="services">The service collection to add services to.</param> |
| | | 21 | | /// <param name="configureOptions">Optional configuration delegate for Firebase options.</param> |
| | | 22 | | /// <param name="community">The community identifier for collection naming.</param> |
| | | 23 | | /// <returns>The service collection for chaining.</returns> |
| | | 24 | | public static IServiceCollection AddFirebaseDatabase( |
| | | 25 | | this IServiceCollection services, |
| | | 26 | | Action<FirebaseOptions>? configureOptions = null, |
| | | 27 | | string? community = null) |
| | | 28 | | { |
| | | 29 | | // Configure options |
| | 1 | 30 | | if (configureOptions != null) |
| | | 31 | | { |
| | 1 | 32 | | services.Configure(configureOptions); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | // Register FirestoreDb as a singleton |
| | 1 | 36 | | services.AddSingleton<FirestoreDb>(serviceProvider => |
| | 1 | 37 | | { |
| | 0 | 38 | | var options = serviceProvider.GetRequiredService<IOptions<FirebaseOptions>>().Value; |
| | 0 | 39 | | var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>(); |
| | 0 | 40 | | var logger = loggerFactory.CreateLogger("FirebaseAdapter.ServiceCollectionExtensions"); |
| | 1 | 41 | | |
| | 1 | 42 | | try |
| | 1 | 43 | | { |
| | 0 | 44 | | options.Validate(); |
| | 1 | 45 | | |
| | 1 | 46 | | FirestoreDb firestoreDb; |
| | 1 | 47 | | |
| | 0 | 48 | | if (!string.IsNullOrWhiteSpace(options.ServiceAccountPath)) |
| | 1 | 49 | | { |
| | 1 | 50 | | // Use service account file path |
| | 0 | 51 | | logger.LogInformation("Initializing Firebase with service account file: {Path}", options.ServiceAcco |
| | 1 | 52 | | |
| | 0 | 53 | | Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", options.ServiceAccountPath); |
| | 0 | 54 | | firestoreDb = FirestoreDb.Create(options.ProjectId); |
| | 1 | 55 | | } |
| | 1 | 56 | | else |
| | 1 | 57 | | { |
| | 1 | 58 | | // Use service account JSON content |
| | 0 | 59 | | logger.LogInformation("Initializing Firebase with service account JSON content for project: {Project |
| | 1 | 60 | | |
| | 1 | 61 | | // Create FirestoreDb from JSON content |
| | 0 | 62 | | var credentialsBytes = Encoding.UTF8.GetBytes(options.ServiceAccountJson); |
| | 0 | 63 | | using var stream = new MemoryStream(credentialsBytes); |
| | 1 | 64 | | |
| | 0 | 65 | | var firestoreDbBuilder = new FirestoreDbBuilder |
| | 0 | 66 | | { |
| | 0 | 67 | | ProjectId = options.ProjectId, |
| | 0 | 68 | | JsonCredentials = options.ServiceAccountJson |
| | 0 | 69 | | }; |
| | 1 | 70 | | |
| | 0 | 71 | | firestoreDb = firestoreDbBuilder.Build(); |
| | 1 | 72 | | } |
| | 1 | 73 | | |
| | 0 | 74 | | logger.LogInformation("Firebase Firestore successfully initialized for project: {ProjectId}", options.Pr |
| | 0 | 75 | | return firestoreDb; |
| | 1 | 76 | | } |
| | 0 | 77 | | catch (Exception ex) |
| | 1 | 78 | | { |
| | 0 | 79 | | logger.LogError(ex, "Failed to initialize Firebase Firestore for project: {ProjectId}", options.ProjectI |
| | 0 | 80 | | throw; |
| | 1 | 81 | | } |
| | 0 | 82 | | }); |
| | | 83 | | |
| | | 84 | | // Register the prediction repository |
| | 1 | 85 | | services.AddScoped<IPredictionRepository>(serviceProvider => |
| | 1 | 86 | | { |
| | 0 | 87 | | var firestoreDb = serviceProvider.GetRequiredService<FirestoreDb>(); |
| | 0 | 88 | | var logger = serviceProvider.GetRequiredService<ILogger<FirebasePredictionRepository>>(); |
| | 0 | 89 | | return new FirebasePredictionRepository(firestoreDb, logger); |
| | 1 | 90 | | }); |
| | | 91 | | |
| | | 92 | | // Register the KPI repository |
| | 1 | 93 | | services.AddScoped<IKpiRepository>(serviceProvider => |
| | 1 | 94 | | { |
| | 0 | 95 | | var firestoreDb = serviceProvider.GetRequiredService<FirestoreDb>(); |
| | 0 | 96 | | var logger = serviceProvider.GetRequiredService<ILogger<FirebaseKpiRepository>>(); |
| | 0 | 97 | | return new FirebaseKpiRepository(firestoreDb, logger); |
| | 1 | 98 | | }); |
| | | 99 | | |
| | | 100 | | // Register the context repository |
| | 1 | 101 | | services.AddScoped<IContextRepository>(serviceProvider => |
| | 1 | 102 | | { |
| | 0 | 103 | | var firestoreDb = serviceProvider.GetRequiredService<FirestoreDb>(); |
| | 0 | 104 | | var logger = serviceProvider.GetRequiredService<ILogger<FirebaseContextRepository>>(); |
| | 0 | 105 | | return new FirebaseContextRepository(firestoreDb, logger); |
| | 1 | 106 | | }); |
| | | 107 | | |
| | | 108 | | // Register the KPI context provider |
| | 1 | 109 | | services.AddScoped<FirebaseKpiContextProvider>(serviceProvider => |
| | 1 | 110 | | { |
| | 0 | 111 | | var kpiRepository = serviceProvider.GetRequiredService<IKpiRepository>(); |
| | 0 | 112 | | var logger = serviceProvider.GetRequiredService<ILogger<FirebaseKpiContextProvider>>(); |
| | 0 | 113 | | return new FirebaseKpiContextProvider(kpiRepository, logger); |
| | 1 | 114 | | }); |
| | | 115 | | |
| | 1 | 116 | | return services; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Adds Firebase Firestore database services with configuration from IConfiguration. |
| | | 121 | | /// </summary> |
| | | 122 | | /// <param name="services">The service collection to add services to.</param> |
| | | 123 | | /// <param name="configuration">The configuration to bind Firebase options from.</param> |
| | | 124 | | /// <returns>The service collection for chaining.</returns> |
| | | 125 | | public static IServiceCollection AddFirebaseDatabase( |
| | | 126 | | this IServiceCollection services, |
| | | 127 | | IConfiguration configuration) |
| | | 128 | | { |
| | 1 | 129 | | services.Configure<FirebaseOptions>(configuration.GetSection(FirebaseOptions.SectionName)); |
| | 1 | 130 | | return services.AddFirebaseDatabase((Action<FirebaseOptions>?)null); |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Adds Firebase Firestore database services with explicit project ID and service account JSON. |
| | | 135 | | /// </summary> |
| | | 136 | | /// <param name="services">The service collection to add services to.</param> |
| | | 137 | | /// <param name="projectId">Firebase project ID.</param> |
| | | 138 | | /// <param name="serviceAccountJson">Service account JSON content.</param> |
| | | 139 | | /// <param name="community">The community identifier for collection naming.</param> |
| | | 140 | | /// <returns>The service collection for chaining.</returns> |
| | | 141 | | public static IServiceCollection AddFirebaseDatabase( |
| | | 142 | | this IServiceCollection services, |
| | | 143 | | string projectId, |
| | | 144 | | string serviceAccountJson, |
| | | 145 | | string community) |
| | | 146 | | { |
| | 1 | 147 | | return services.AddFirebaseDatabase(options => |
| | 1 | 148 | | { |
| | 1 | 149 | | options.ProjectId = projectId; |
| | 1 | 150 | | options.ServiceAccountJson = serviceAccountJson; |
| | 1 | 151 | | }, community); |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Adds Firebase Firestore database services with explicit project ID and service account file path. |
| | | 156 | | /// </summary> |
| | | 157 | | /// <param name="services">The service collection to add services to.</param> |
| | | 158 | | /// <param name="projectId">Firebase project ID.</param> |
| | | 159 | | /// <param name="serviceAccountPath">Path to service account JSON file.</param> |
| | | 160 | | /// <returns>The service collection for chaining.</returns> |
| | | 161 | | public static IServiceCollection AddFirebaseDatabaseWithFile( |
| | | 162 | | this IServiceCollection services, |
| | | 163 | | string projectId, |
| | | 164 | | string serviceAccountPath) |
| | | 165 | | { |
| | 1 | 166 | | return services.AddFirebaseDatabase(options => |
| | 1 | 167 | | { |
| | 1 | 168 | | options.ProjectId = projectId; |
| | 1 | 169 | | options.ServiceAccountPath = serviceAccountPath; |
| | 1 | 170 | | }); |
| | | 171 | | } |
| | | 172 | | } |