Implements Data Access Layer

This commit is contained in:
BinaryAlley committed 2024-08-26 23:32:29 +03:00
1 parent b69e59a3d8
commit 8b729cc16c
24 files changed
+1721 -9

No files matched your search

@@ -0,0 +1,58 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Repositories.Books;
using Lumina.Application.Common.Models.Books;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.Common.Errors;
using Microsoft.EntityFrameworkCore;
#endregion
namespace Lumina.DataAccess.Core.Repositories.Books;
/// <summary>
/// Repository for books.
/// </summary>
internal sealed class BookRepository : IBookRepository
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly LuminaDbContext _luminaDbContext;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Initializes a new instance of the <see cref="BookRepository"/> class.
/// </summary>
/// <param name="luminaDbContext">Injected Entity Framework DbContext.</param>
public BookRepository(LuminaDbContext luminaDbContext)
{
_luminaDbContext = luminaDbContext;
}
#endregion
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Adds a new book.
/// </summary>
/// <param name="book">The book to add.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{T}"/> representing either a successfull operation, or an error.</returns>
public async Task<ErrorOr<Created>> InsertAsync(BookDto book, CancellationToken cancellationToken)
{
var jobExists = await _luminaDbContext.Books.AnyAsync(_book => _book.Id == book.Id, cancellationToken: cancellationToken).ConfigureAwait(false);
if (jobExists)
return Errors.WrittenContent.BookAlreadyExists;
_luminaDbContext.Books.Add(book);
return Result.Created;
}
/// <summary>
/// Gets all books.
/// </summary>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{T}"/> containing either a collection of <see cref="BookDto"/>, or an error.</returns>
public async Task<ErrorOr<IEnumerable<BookDto>>> GetAllAsync(CancellationToken cancellationToken)
{
return await _luminaDbContext.Books.ToListAsync(cancellationToken).ConfigureAwait(false);
}
#endregion
}
@@ -0,0 +1,16 @@
namespace Lumina.DataAccess.Core.Repositories.Common.Factory;
/// <summary>
/// Repositories abstract factory.
/// </summary>
public interface IRepositoryFactory
{
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Creates a new repository of type <typeparamref name="TResult"/>.
/// </summary>
/// <typeparam name="TResult">The type of repository to create.</typeparam>
/// <returns>A repository of type <typeparamref name="TResult"/>.</returns>
TResult CreateRepository<TResult>() where TResult : notnull;
#endregion
}
@@ -0,0 +1,64 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
#endregion
namespace Lumina.DataAccess.Core.Repositories.Common.Factory;
/// <summary>
/// Custom collection for storing repositories.
/// </summary>
public class RepositoryDictionary
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly List<KeyValuePair<Type, object>> _container = [];
#endregion
#region ==================================================================== PROPERTIES =================================================================================
public int Count { get { return _container.Count; } }
#endregion
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Resets the collection of repositories.
/// </summary>
public void Clear()
{
_container.Clear();
}
/// <summary>
/// Adds a repository of type <typeparamref name="TParam"/> to the collection.
/// </summary>
/// <typeparam name="TParam">The type of the repository to add to the collection.</typeparam>
/// <param name="value">The repository to add.</param>
public void Add<TParam>(TParam value) where TParam : notnull
{
// enforce adding rules
if (value == null)
throw new ArgumentException("Value cannot be null!");
if (_container.Any(e => e.Key == value.GetType()))
throw new ArgumentException("Duplicate values are not allowed!");
// check if the value implements an interface that implements IRepository
bool implementsRepository = value.GetType()
.GetInterfaces()
.Any(e => e.GetInterfaces()
.Any(i => i.IsGenericType &&
i.GetGenericTypeDefinition() == typeof(IRepository<>)));
if (implementsRepository)
_container.Add(new KeyValuePair<Type, object>(value.GetType(), value));
else
throw new ArgumentException("Value must implement IRepository interface!");
}
/// <summary>
/// Gets a repository of type <typeparamref name="TResult"/> from the collection.
/// </summary>
/// <typeparam name="TResult">The type of the repository interface to get from the collection.</typeparam>
/// <param name="key">The concrete type of the repository interface to get.</param>
/// <returns>A repository implementing <typeparamref name="TResult"/>.</returns>
public TResult Get<TResult>(Type key)
{
return (TResult)_container.FirstOrDefault(e => e.Key == key).Value;
}
#endregion
}
@@ -0,0 +1,39 @@
#region ========================================================================= USING =====================================================================================
using Microsoft.Extensions.DependencyInjection;
#endregion
namespace Lumina.DataAccess.Core.Repositories.Common.Factory;
/// <summary>
/// Concrete implementation for the repositories abstract factory.
/// </summary>
public class RepositoryFactory : IRepositoryFactory
{
#region ================================================================== FIELD MEMBERS ================================================================================
private readonly IServiceProvider _serviceProvider;
#endregion
#region ====================================================================== CTOR =====================================================================================
/// <summary>
/// Overload C-tor.
/// </summary>
/// <param name="serviceProvider">The service provider.</param>
public RepositoryFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
#endregion
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Creates a new repository of type <typeparamref name="TResult"/>.
/// </summary>
/// <typeparam name="TResult">The type of repository to create.</typeparam>
/// <returns>A repository of type <typeparamref name="TResult"/>.</returns>
/// <exception cref="ArgumentException">Thrown when the type of the requested repository has not been registered.</exception>
public TResult CreateRepository<TResult>() where TResult : notnull
{
return _serviceProvider.GetRequiredService<TResult>() ?? throw new ArgumentException();
}
#endregion
}