Changed domains, configured links between VisaApplication and Applicant,created generic repository and repositories for domains

This commit is contained in:
2024-08-14 18:49:42 +03:00
parent e0f9a0f10f
commit e3d0d05355
35 changed files with 237 additions and 80 deletions

View File

@@ -0,0 +1,38 @@
using Domains;
using Infrastructure.Database.GeneralExceptions;
using Microsoft.EntityFrameworkCore;
namespace Infrastructure.Database.Generic
{
public abstract class GenericRepository<T>(IGenericWriter writer, IUnitOfWork unitOfWork) : IGenericRepository<T>
where T : class, IEntity
{
public async Task<List<T>> GetAllAsync(CancellationToken cancellationToken)
=> await LoadDomain().ToListAsync(cancellationToken);
public async Task<T> GetOneAsync(Guid id, CancellationToken cancellationToken)
{
var result = await LoadDomain().SingleOrDefaultAsync(a => a.Id == id, cancellationToken);
return result ?? throw new EntityNotFoundException<T>(id);
}
public async Task AddAsync(T entity, CancellationToken cancellationToken)
=> await writer.AddAsync(entity, cancellationToken);
public async Task UpdateAsync(T entity, CancellationToken cancellationToken)
{
await GetOneAsync(entity.Id, cancellationToken);
writer.Update(entity);
}
public void Remove(T entity)
{
writer.Remove(entity);
}
public async Task SaveAsync(CancellationToken cancellationToken)
=> await unitOfWork.SaveAsync(cancellationToken);
protected abstract IQueryable<T> LoadDomain();
}
}

View File

@@ -0,0 +1,11 @@
using Domains;
namespace Infrastructure.Database.Generic
{
public interface IGenericReader
{
/// Get all entities of type <typeparamref name="T"/> stored in storage
/// <typeparam name="T">Entity type to seek in storage</typeparam>
IQueryable<T> GetAll<T>() where T : class, IEntity;
}
}

View File

@@ -0,0 +1,19 @@
using Domains;
namespace Infrastructure.Database.Generic
{
public interface IGenericRepository<T> where T : class, IEntity
{
Task<List<T>> GetAllAsync(CancellationToken cancellationToken);
Task<T> GetOneAsync(Guid id, CancellationToken cancellationToken);
Task AddAsync(T entity, CancellationToken cancellationToken);
Task UpdateAsync(T entity, CancellationToken cancellationToken);
void Remove(T entity);
Task SaveAsync(CancellationToken cancellationToken);
}
}

View File

@@ -0,0 +1,25 @@
using Domains;
namespace Infrastructure.Database.Generic
{
/// Writes data to data storage
/// <remarks><see cref="IUnitOfWork"/> should be used to save changes</remarks>
public interface IGenericWriter
{
/// Add <paramref name="entity"/> to data storage
/// <param name="entity">Entity to add</param>
/// <param name="cancellationToken">Cancellation Token</param>
/// <typeparam name="T">Entity type</typeparam>
Task AddAsync<T>(T entity, CancellationToken cancellationToken) where T : class, IEntity;
/// Update <paramref name="entity"/> in data storage
/// <param name="entity">Entity to update</param>
/// <typeparam name="T">Entity type</typeparam>
void Update<T>(T entity) where T : class, IEntity;
/// Remove <paramref name="entity"/> from data storage
/// <param name="entity">Entity to remove</param>
/// <typeparam name="T">Entity type</typeparam>
void Remove<T>(T entity) where T : class, IEntity;
}
}