Skip to content

Commit f487b59

Browse files
justinyoojhmin99
authored andcommitted
[Admin] Add repository layer for admin events (aliencube#287)
1 parent dae27b3 commit f487b59

File tree

5 files changed

+196
-9
lines changed

5 files changed

+196
-9
lines changed

src/AzureOpenAIProxy.ApiApp/Program.cs

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using AzureOpenAIProxy.ApiApp.Endpoints;
22
using AzureOpenAIProxy.ApiApp.Extensions;
3+
using AzureOpenAIProxy.ApiApp.Repositories;
34
using AzureOpenAIProxy.ApiApp.Services;
45

56
var builder = WebApplication.CreateBuilder(args);
@@ -18,6 +19,9 @@
1819
// Add admin services
1920
builder.Services.AddAdminEventService();
2021

22+
// Add admin repositories
23+
builder.Services.AddAdminEventRepository();
24+
2125
var app = builder.Build();
2226

2327
app.MapDefaultEndpoints();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using AzureOpenAIProxy.ApiApp.Models;
2+
3+
namespace AzureOpenAIProxy.ApiApp.Repositories;
4+
5+
/// <summary>
6+
/// This provides interfaces to the <see cref="AdminEventRepository"/> class.
7+
/// </summary>
8+
public interface IAdminEventRepository
9+
{
10+
/// <summary>
11+
/// Creates a new record of event details.
12+
/// </summary>
13+
/// <param name="eventDetails">Event details instance.</param>
14+
/// <returns>Returns the event details instance created.</returns>
15+
Task<AdminEventDetails> CreateEvent(AdminEventDetails eventDetails);
16+
17+
/// <summary>
18+
/// Gets the list of events.
19+
/// </summary>
20+
/// <returns>Returns the list of events.</returns>
21+
Task<List<AdminEventDetails>> GetEvents();
22+
23+
/// <summary>
24+
/// Gets the event details.
25+
/// </summary>
26+
/// <param name="eventId">Event ID.</param>
27+
/// <returns>Returns the event details record.</returns>
28+
Task<AdminEventDetails> GetEvent(Guid eventId);
29+
30+
/// <summary>
31+
/// Updates the event details.
32+
/// </summary>
33+
/// <param name="eventId">Event ID.</param>
34+
/// <param name="eventDetails">Event details instance.</param>
35+
/// <returns>Returns the updated record of the event details.</returns>
36+
Task<AdminEventDetails> UpdateEvent(Guid eventId, AdminEventDetails eventDetails);
37+
}
38+
39+
/// <summary>
40+
/// This represents the repository entity for the admin event.
41+
/// </summary>
42+
public class AdminEventRepository : IAdminEventRepository
43+
{
44+
/// <inheritdoc />
45+
public async Task<AdminEventDetails> CreateEvent(AdminEventDetails eventDetails)
46+
{
47+
throw new NotImplementedException();
48+
}
49+
50+
/// <inheritdoc />
51+
public async Task<List<AdminEventDetails>> GetEvents()
52+
{
53+
throw new NotImplementedException();
54+
}
55+
56+
/// <inheritdoc />
57+
public async Task<AdminEventDetails> GetEvent(Guid eventId)
58+
{
59+
throw new NotImplementedException();
60+
}
61+
62+
/// <inheritdoc />
63+
public async Task<AdminEventDetails> UpdateEvent(Guid eventId, AdminEventDetails eventDetails)
64+
{
65+
throw new NotImplementedException();
66+
}
67+
}
68+
69+
/// <summary>
70+
/// This represents the extension class for <see cref="IServiceCollection"/>
71+
/// </summary>
72+
public static class AdminEventRepositoryExtensions
73+
{
74+
/// <summary>
75+
/// Adds the <see cref="AdminEventRepository"/> instance to the service collection.
76+
/// </summary>
77+
/// <param name="services"><see cref="IServiceCollection"/> instance.</param>
78+
/// <returns>Returns <see cref="IServiceCollection"/> instance.</returns>
79+
public static IServiceCollection AddAdminEventRepository(this IServiceCollection services)
80+
{
81+
services.AddScoped<IAdminEventRepository, AdminEventRepository>();
82+
83+
return services;
84+
}
85+
}

src/AzureOpenAIProxy.ApiApp/Services/AdminEventService.cs

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using AzureOpenAIProxy.ApiApp.Models;
2+
using AzureOpenAIProxy.ApiApp.Repositories;
23

34
namespace AzureOpenAIProxy.ApiApp.Services;
45

@@ -39,30 +40,40 @@ public interface IAdminEventService
3940
/// <summary>
4041
/// This represents the service entity for admin event.
4142
/// </summary>
42-
public class AdminEventService : IAdminEventService
43+
public class AdminEventService(IAdminEventRepository repository) : IAdminEventService
4344
{
45+
private readonly IAdminEventRepository _repository = repository ?? throw new ArgumentNullException(nameof(repository));
46+
4447
/// <inheritdoc />
4548
public async Task<AdminEventDetails> CreateEvent(AdminEventDetails eventDetails)
4649
{
47-
throw new NotImplementedException();
50+
var result = await this._repository.CreateEvent(eventDetails).ConfigureAwait(false);
51+
52+
return result;
4853
}
4954

5055
/// <inheritdoc />
5156
public async Task<List<AdminEventDetails>> GetEvents()
5257
{
53-
throw new NotImplementedException();
58+
var result = await this._repository.GetEvents().ConfigureAwait(false);
59+
60+
return result;
5461
}
5562

5663
/// <inheritdoc />
5764
public async Task<AdminEventDetails> GetEvent(Guid eventId)
5865
{
59-
throw new NotImplementedException();
66+
var result = await this._repository.GetEvent(eventId).ConfigureAwait(false);
67+
68+
return result;
6069
}
6170

6271
/// <inheritdoc />
6372
public async Task<AdminEventDetails> UpdateEvent(Guid eventId, AdminEventDetails eventDetails)
6473
{
65-
throw new NotImplementedException();
74+
var result = await this._repository.UpdateEvent(eventId, eventDetails).ConfigureAwait(false);
75+
76+
return result;
6677
}
6778
}
6879

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using AzureOpenAIProxy.ApiApp.Models;
2+
using AzureOpenAIProxy.ApiApp.Repositories;
3+
4+
using FluentAssertions;
5+
6+
using Microsoft.Extensions.DependencyInjection;
7+
8+
namespace AzureOpenAIProxy.ApiApp.Tests.Repositories;
9+
10+
public class AdminEventRepositoryTests
11+
{
12+
[Fact]
13+
public void Given_ServiceCollection_When_AddAdminEventRepository_Invoked_Then_It_Should_Contain_AdminEventRepository()
14+
{
15+
// Arrange
16+
var services = new ServiceCollection();
17+
18+
// Act
19+
services.AddAdminEventRepository();
20+
21+
// Assert
22+
services.SingleOrDefault(p => p.ServiceType == typeof(IAdminEventRepository)).Should().NotBeNull();
23+
}
24+
25+
[Fact]
26+
public void Given_Instance_When_CreateEvent_Invoked_Then_It_Should_Throw_Exception()
27+
{
28+
// Arrange
29+
var eventDetails = new AdminEventDetails();
30+
var repository = new AdminEventRepository();
31+
32+
// Act
33+
Func<Task> func = async () => await repository.CreateEvent(eventDetails);
34+
35+
// Assert
36+
func.Should().ThrowAsync<NotImplementedException>();
37+
}
38+
39+
[Fact]
40+
public void Given_Instance_When_GetEvents_Invoked_Then_It_Should_Throw_Exception()
41+
{
42+
// Arrange
43+
var repository = new AdminEventRepository();
44+
45+
// Act
46+
Func<Task> func = async () => await repository.GetEvents();
47+
48+
// Assert
49+
func.Should().ThrowAsync<NotImplementedException>();
50+
}
51+
52+
[Fact]
53+
public void Given_Instance_When_GetEvent_Invoked_Then_It_Should_Throw_Exception()
54+
{
55+
// Arrange
56+
var eventId = Guid.NewGuid();
57+
var repository = new AdminEventRepository();
58+
59+
// Act
60+
Func<Task> func = async () => await repository.GetEvent(eventId);
61+
62+
// Assert
63+
func.Should().ThrowAsync<NotImplementedException>();
64+
}
65+
66+
[Fact]
67+
public void Given_Instance_When_UpdateEvent_Invoked_Then_It_Should_Throw_Exception()
68+
{
69+
// Arrange
70+
var eventId = Guid.NewGuid();
71+
var eventDetails = new AdminEventDetails();
72+
var repository = new AdminEventRepository();
73+
74+
// Act
75+
Func<Task> func = async () => await repository.UpdateEvent(eventId, eventDetails);
76+
77+
// Assert
78+
func.Should().ThrowAsync<NotImplementedException>();
79+
}
80+
}

test/AzureOpenAIProxy.ApiApp.Tests/Services/AdminEventServiceTests.cs

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using AzureOpenAIProxy.ApiApp.Models;
2+
using AzureOpenAIProxy.ApiApp.Repositories;
23
using AzureOpenAIProxy.ApiApp.Services;
34

45
using FluentAssertions;
56

67
using Microsoft.Extensions.DependencyInjection;
78

9+
using NSubstitute;
10+
811
namespace AzureOpenAIProxy.ApiApp.Tests.Services;
912

1013
public class AdminEventServiceTests
@@ -27,7 +30,8 @@ public void Given_Instance_When_CreateEvent_Invoked_Then_It_Should_Throw_Excepti
2730
{
2831
// Arrange
2932
var eventDetails = new AdminEventDetails();
30-
var service = new AdminEventService();
33+
var repository = Substitute.For<IAdminEventRepository>();
34+
var service = new AdminEventService(repository);
3135

3236
// Act
3337
Func<Task> func = async () => await service.CreateEvent(eventDetails);
@@ -40,7 +44,8 @@ public void Given_Instance_When_CreateEvent_Invoked_Then_It_Should_Throw_Excepti
4044
public void Given_Instance_When_GetEvents_Invoked_Then_It_Should_Throw_Exception()
4145
{
4246
// Arrange
43-
var service = new AdminEventService();
47+
var repository = Substitute.For<IAdminEventRepository>();
48+
var service = new AdminEventService(repository);
4449

4550
// Act
4651
Func<Task> func = async () => await service.GetEvents();
@@ -54,7 +59,8 @@ public void Given_Instance_When_GetEvent_Invoked_Then_It_Should_Throw_Exception(
5459
{
5560
// Arrange
5661
var eventId = Guid.NewGuid();
57-
var service = new AdminEventService();
62+
var repository = Substitute.For<IAdminEventRepository>();
63+
var service = new AdminEventService(repository);
5864

5965
// Act
6066
Func<Task> func = async () => await service.GetEvent(eventId);
@@ -69,7 +75,8 @@ public void Given_Instance_When_UpdateEvent_Invoked_Then_It_Should_Throw_Excepti
6975
// Arrange
7076
var eventId = Guid.NewGuid();
7177
var eventDetails = new AdminEventDetails();
72-
var service = new AdminEventService();
78+
var repository = Substitute.For<IAdminEventRepository>();
79+
var service = new AdminEventService(repository);
7380

7481
// Act
7582
Func<Task> func = async () => await service.UpdateEvent(eventId, eventDetails);

0 commit comments

Comments
 (0)