1
+ using Azure . Data . Tables ;
2
+
3
+ using AzureOpenAIProxy . ApiApp . Configurations ;
4
+ using AzureOpenAIProxy . ApiApp . Models ;
5
+
6
+ namespace AzureOpenAIProxy . ApiApp . Repositories ;
7
+
8
+ /// <summary>
9
+ /// This provides interfaces to the <see cref="AdminResourceRepository"/> class.
10
+ /// </summary>
11
+ public interface IAdminResourceRepository
12
+ {
13
+ /// <summary>
14
+ /// Creates a new record of resource details.
15
+ /// </summary>
16
+ /// <param name="resourceDetails">Resource details instance.</param>
17
+ /// <returns>Returns the resource details instance created.</returns>
18
+ Task < AdminResourceDetails > CreateResource ( AdminResourceDetails resourceDetails ) ;
19
+ }
20
+
21
+ /// <summary>
22
+ /// This represents the repository entity for the admin resource.
23
+ /// </summary>
24
+ public class AdminResourceRepository ( TableServiceClient tableServiceClient , StorageAccountSettings storageAccountSettings ) : IAdminResourceRepository
25
+ {
26
+ private readonly TableServiceClient _tableServiceClient = tableServiceClient ?? throw new ArgumentNullException ( nameof ( tableServiceClient ) ) ;
27
+ private readonly StorageAccountSettings _storageAccountSettings = storageAccountSettings ?? throw new ArgumentNullException ( nameof ( storageAccountSettings ) ) ;
28
+
29
+ /// <inheritdoc />
30
+ public async Task < AdminResourceDetails > CreateResource ( AdminResourceDetails resourceDetails )
31
+ {
32
+ TableClient tableClient = await GetTableClientAsync ( ) ;
33
+
34
+ await tableClient . AddEntityAsync ( resourceDetails ) . ConfigureAwait ( false ) ;
35
+
36
+ return resourceDetails ;
37
+ }
38
+
39
+ private async Task < TableClient > GetTableClientAsync ( )
40
+ {
41
+ TableClient tableClient = _tableServiceClient . GetTableClient ( _storageAccountSettings . TableStorage . TableName ) ;
42
+
43
+ await tableClient . CreateIfNotExistsAsync ( ) . ConfigureAwait ( false ) ;
44
+
45
+ return tableClient ;
46
+ }
47
+ }
48
+
49
+ /// <summary>
50
+ /// This represents the extension class for <see cref="IServiceCollection"/>
51
+ /// </summary>
52
+ public static class AdminResourceRepositoryExtensions
53
+ {
54
+ /// <summary>
55
+ /// Adds the <see cref="AdminResourceRepository"/> instance to the service collection.
56
+ /// </summary>
57
+ /// <param name="services"><see cref="IServiceCollection"/> instance.</param>
58
+ /// <returns>Returns <see cref="IServiceCollection"/> instance.</returns>
59
+ public static IServiceCollection AddAdminResourceRepository ( this IServiceCollection services )
60
+ {
61
+ services . AddScoped < IAdminResourceRepository , AdminResourceRepository > ( ) ;
62
+
63
+ return services ;
64
+ }
65
+ }
0 commit comments