Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Admin] Add repository layer for admin events #287

Merged
merged 2 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/AzureOpenAIProxy.ApiApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AzureOpenAIProxy.ApiApp.Endpoints;
using AzureOpenAIProxy.ApiApp.Extensions;
using AzureOpenAIProxy.ApiApp.Repositories;
using AzureOpenAIProxy.ApiApp.Services;

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

// Add admin repositories
builder.Services.AddAdminEventRepository();

var app = builder.Build();

app.MapDefaultEndpoints();
Expand Down
85 changes: 85 additions & 0 deletions src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using AzureOpenAIProxy.ApiApp.Models;

namespace AzureOpenAIProxy.ApiApp.Repositories;

/// <summary>
/// This provides interfaces to the <see cref="AdminEventRepository"/> class.
/// </summary>
public interface IAdminEventRepository
{
/// <summary>
/// Creates a new record of event details.
/// </summary>
/// <param name="eventDetails">Event details instance.</param>
/// <returns>Returns the event details instance created.</returns>
Task<AdminEventDetails> CreateEvent(AdminEventDetails eventDetails);

/// <summary>
/// Gets the list of events.
/// </summary>
/// <returns>Returns the list of events.</returns>
Task<List<AdminEventDetails>> GetEvents();

/// <summary>
/// Gets the event details.
/// </summary>
/// <param name="eventId">Event ID.</param>
/// <returns>Returns the event details record.</returns>
Task<AdminEventDetails> GetEvent(Guid eventId);

/// <summary>
/// Updates the event details.
/// </summary>
/// <param name="eventId">Event ID.</param>
/// <param name="eventDetails">Event details instance.</param>
/// <returns>Returns the updated record of the event details.</returns>
Task<AdminEventDetails> UpdateEvent(Guid eventId, AdminEventDetails eventDetails);
}

/// <summary>
/// This represents the repository entity for the admin event.
/// </summary>
public class AdminEventRepository : IAdminEventRepository
{
/// <inheritdoc />
public async Task<AdminEventDetails> CreateEvent(AdminEventDetails eventDetails)

Check warning on line 45 in src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs

View workflow job for this annotation

GitHub Actions / build-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 45 in src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs

View workflow job for this annotation

GitHub Actions / build-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 45 in src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs

View workflow job for this annotation

GitHub Actions / build-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 45 in src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs

View workflow job for this annotation

GitHub Actions / build-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
throw new NotImplementedException();
}

/// <inheritdoc />
public async Task<List<AdminEventDetails>> GetEvents()

Check warning on line 51 in src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs

View workflow job for this annotation

GitHub Actions / build-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 51 in src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs

View workflow job for this annotation

GitHub Actions / build-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 51 in src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs

View workflow job for this annotation

GitHub Actions / build-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 51 in src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs

View workflow job for this annotation

GitHub Actions / build-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
throw new NotImplementedException();
}

/// <inheritdoc />
public async Task<AdminEventDetails> GetEvent(Guid eventId)

Check warning on line 57 in src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs

View workflow job for this annotation

GitHub Actions / build-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 57 in src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs

View workflow job for this annotation

GitHub Actions / build-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 57 in src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs

View workflow job for this annotation

GitHub Actions / build-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 57 in src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs

View workflow job for this annotation

GitHub Actions / build-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
throw new NotImplementedException();
}

/// <inheritdoc />
public async Task<AdminEventDetails> UpdateEvent(Guid eventId, AdminEventDetails eventDetails)

Check warning on line 63 in src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs

View workflow job for this annotation

GitHub Actions / build-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 63 in src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs

View workflow job for this annotation

GitHub Actions / build-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 63 in src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs

View workflow job for this annotation

GitHub Actions / build-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 63 in src/AzureOpenAIProxy.ApiApp/Repositories/AdminEventRepository.cs

View workflow job for this annotation

GitHub Actions / build-test

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
throw new NotImplementedException();
}
}

/// <summary>
/// This represents the extension class for <see cref="IServiceCollection"/>
/// </summary>
public static class AdminEventRepositoryExtensions
{
/// <summary>
/// Adds the <see cref="AdminEventRepository"/> instance to the service collection.
/// </summary>
/// <param name="services"><see cref="IServiceCollection"/> instance.</param>
/// <returns>Returns <see cref="IServiceCollection"/> instance.</returns>
public static IServiceCollection AddAdminEventRepository(this IServiceCollection services)
{
services.AddScoped<IAdminEventRepository, AdminEventRepository>();

return services;
}
}
21 changes: 16 additions & 5 deletions src/AzureOpenAIProxy.ApiApp/Services/AdminEventService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AzureOpenAIProxy.ApiApp.Models;
using AzureOpenAIProxy.ApiApp.Repositories;

namespace AzureOpenAIProxy.ApiApp.Services;

Expand Down Expand Up @@ -39,30 +40,40 @@ public interface IAdminEventService
/// <summary>
/// This represents the service entity for admin event.
/// </summary>
public class AdminEventService : IAdminEventService
public class AdminEventService(IAdminEventRepository repository) : IAdminEventService
{
private readonly IAdminEventRepository _repository = repository ?? throw new ArgumentNullException(nameof(repository));

/// <inheritdoc />
public async Task<AdminEventDetails> CreateEvent(AdminEventDetails eventDetails)
{
throw new NotImplementedException();
var result = await this._repository.CreateEvent(eventDetails).ConfigureAwait(false);

return result;
}

/// <inheritdoc />
public async Task<List<AdminEventDetails>> GetEvents()
{
throw new NotImplementedException();
var result = await this._repository.GetEvents().ConfigureAwait(false);

return result;
}

/// <inheritdoc />
public async Task<AdminEventDetails> GetEvent(Guid eventId)
{
throw new NotImplementedException();
var result = await this._repository.GetEvent(eventId).ConfigureAwait(false);

return result;
}

/// <inheritdoc />
public async Task<AdminEventDetails> UpdateEvent(Guid eventId, AdminEventDetails eventDetails)
{
throw new NotImplementedException();
var result = await this._repository.UpdateEvent(eventId, eventDetails).ConfigureAwait(false);

return result;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using AzureOpenAIProxy.ApiApp.Models;
using AzureOpenAIProxy.ApiApp.Repositories;

using FluentAssertions;

using Microsoft.Extensions.DependencyInjection;

namespace AzureOpenAIProxy.ApiApp.Tests.Repositories;

public class AdminEventRepositoryTests
{
[Fact]
public void Given_ServiceCollection_When_AddAdminEventRepository_Invoked_Then_It_Should_Contain_AdminEventRepository()
{
// Arrange
var services = new ServiceCollection();

// Act
services.AddAdminEventRepository();

// Assert
services.SingleOrDefault(p => p.ServiceType == typeof(IAdminEventRepository)).Should().NotBeNull();
}

[Fact]
public void Given_Instance_When_CreateEvent_Invoked_Then_It_Should_Throw_Exception()
{
// Arrange
var eventDetails = new AdminEventDetails();
var repository = new AdminEventRepository();

// Act
Func<Task> func = async () => await repository.CreateEvent(eventDetails);

// Assert
func.Should().ThrowAsync<NotImplementedException>();
}

[Fact]
public void Given_Instance_When_GetEvents_Invoked_Then_It_Should_Throw_Exception()
{
// Arrange
var repository = new AdminEventRepository();

// Act
Func<Task> func = async () => await repository.GetEvents();

// Assert
func.Should().ThrowAsync<NotImplementedException>();
}

[Fact]
public void Given_Instance_When_GetEvent_Invoked_Then_It_Should_Throw_Exception()
{
// Arrange
var eventId = Guid.NewGuid();
var repository = new AdminEventRepository();

// Act
Func<Task> func = async () => await repository.GetEvent(eventId);

// Assert
func.Should().ThrowAsync<NotImplementedException>();
}

[Fact]
public void Given_Instance_When_UpdateEvent_Invoked_Then_It_Should_Throw_Exception()
{
// Arrange
var eventId = Guid.NewGuid();
var eventDetails = new AdminEventDetails();
var repository = new AdminEventRepository();

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

// Assert
func.Should().ThrowAsync<NotImplementedException>();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using AzureOpenAIProxy.ApiApp.Models;
using AzureOpenAIProxy.ApiApp.Repositories;
using AzureOpenAIProxy.ApiApp.Services;

using FluentAssertions;

using Microsoft.Extensions.DependencyInjection;

using NSubstitute;

namespace AzureOpenAIProxy.ApiApp.Tests.Services;

public class AdminEventServiceTests
Expand All @@ -27,7 +30,8 @@ public void Given_Instance_When_CreateEvent_Invoked_Then_It_Should_Throw_Excepti
{
// Arrange
var eventDetails = new AdminEventDetails();
var service = new AdminEventService();
var repository = Substitute.For<IAdminEventRepository>();
var service = new AdminEventService(repository);

// Act
Func<Task> func = async () => await service.CreateEvent(eventDetails);
Expand All @@ -40,7 +44,8 @@ public void Given_Instance_When_CreateEvent_Invoked_Then_It_Should_Throw_Excepti
public void Given_Instance_When_GetEvents_Invoked_Then_It_Should_Throw_Exception()
{
// Arrange
var service = new AdminEventService();
var repository = Substitute.For<IAdminEventRepository>();
var service = new AdminEventService(repository);

// Act
Func<Task> func = async () => await service.GetEvents();
Expand All @@ -54,7 +59,8 @@ public void Given_Instance_When_GetEvent_Invoked_Then_It_Should_Throw_Exception(
{
// Arrange
var eventId = Guid.NewGuid();
var service = new AdminEventService();
var repository = Substitute.For<IAdminEventRepository>();
var service = new AdminEventService(repository);

// Act
Func<Task> func = async () => await service.GetEvent(eventId);
Expand All @@ -69,7 +75,8 @@ public void Given_Instance_When_UpdateEvent_Invoked_Then_It_Should_Throw_Excepti
// Arrange
var eventId = Guid.NewGuid();
var eventDetails = new AdminEventDetails();
var service = new AdminEventService();
var repository = Substitute.For<IAdminEventRepository>();
var service = new AdminEventService(repository);

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