Skip to content

Commit

Permalink
refactor GetStorageSettings to AddStorageAccountSettings
Browse files Browse the repository at this point in the history
Related to: #319
  • Loading branch information
sikutisa committed Sep 14, 2024
1 parent 29b9486 commit 7e5a9a5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ public static class StorageSettingsExtensions
/// </summary>
/// <param name="services"><see cref="IServiceCollection"/> instance.</param>
/// <returns>Returns <see cref="StorageAccountSettings"/> instance.</returns>
public static StorageAccountSettings GetStorageSettings(this IServiceCollection services)
public static IServiceCollection AddStorageAccountSettings(this IServiceCollection services)
{
var configuration = services.BuildServiceProvider().GetService<IConfiguration>()
?? throw new InvalidOperationException($"{nameof(IConfiguration)} service is not registered.");
services.AddSingleton<StorageAccountSettings>(sp => {
var configuration = sp.GetService<IConfiguration>()
?? throw new InvalidOperationException($"{nameof(IConfiguration)} service is not registered.");
var settings = configuration.GetSection(StorageAccountSettings.Name).Get<StorageAccountSettings>()
?? throw new InvalidOperationException($"{nameof(StorageAccountSettings)} could not be retrieved from the configuration.");
var settings = configuration.GetSection(StorageAccountSettings.Name).Get<StorageAccountSettings>()
?? throw new InvalidOperationException($"{nameof(StorageAccountSettings)} could not be retrieved from the configuration.");
return settings;
return settings;
});

return services;
}
}
3 changes: 3 additions & 0 deletions src/AzureOpenAIProxy.ApiApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
// Add OpenAPI service
builder.Services.AddOpenApiService();

// Add Storage Account settings
builder.Services.AddStorageAccountSettings();

// Add TableStorageClient
builder.Services.AddTableStorageService();

Expand Down
11 changes: 7 additions & 4 deletions src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using AzureOpenAIProxy.ApiApp.Configurations;
using Azure.Data.Tables;

using AzureOpenAIProxy.ApiApp.Configurations;
using AzureOpenAIProxy.ApiApp.Extensions;
using AzureOpenAIProxy.ApiApp.Models;

Expand Down Expand Up @@ -41,9 +43,10 @@ public interface IAdminEventRepository
/// <summary>
/// This represents the repository entity for the admin event.
/// </summary>
public class AdminEventRepository(IServiceCollection sc) : IAdminEventRepository
public class AdminEventRepository(TableServiceClient tableServiceClient, StorageAccountSettings storageAccountSettings) : IAdminEventRepository
{
private readonly StorageAccountSettings _storageSettings = sc.GetStorageSettings();
private readonly TableServiceClient _tableServiceClient = tableServiceClient;
private readonly StorageAccountSettings _storageAccountSettings = storageAccountSettings;

/// <inheritdoc />
public async Task<AdminEventDetails> CreateEvent(AdminEventDetails eventDetails)
Expand Down Expand Up @@ -82,7 +85,7 @@ public static class AdminEventRepositoryExtensions
/// <returns>Returns <see cref="IServiceCollection"/> instance.</returns>
public static IServiceCollection AddAdminEventRepository(this IServiceCollection services)
{
services.AddScoped<IAdminEventRepository>(p => new AdminEventRepository(services));
services.AddScoped<IAdminEventRepository, AdminEventRepository>();

return services;
}
Expand Down

0 comments on commit 7e5a9a5

Please sign in to comment.