Выпиливание рудиментов
This commit is contained in:
@@ -1,103 +0,0 @@
|
||||
using ApplicationSystem.Database.Models;
|
||||
using ApplicationSystem.Database.Models.Interfaces;
|
||||
using ApplicationSystem.Database.Services.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace ApplicationSystem.Database.Services.Implementations
|
||||
{
|
||||
public abstract class AbstractRepository<TEntity, TEntityDto>()
|
||||
: IRepository<TEntity, TEntityDto> where TEntity : class, IEntity where TEntityDto : class
|
||||
{
|
||||
protected IQueryable<TEntity> _entities = null!;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public virtual async Task<int> CountAsync(
|
||||
Expression<Func<TEntity, bool>> expression,
|
||||
CancellationToken cancellationToken)
|
||||
=> await _entities
|
||||
.CountAsync(expression, cancellationToken);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public virtual async Task<TEntityDto?> GetAsync(Guid id, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _entities.FirstOrDefaultAsync(x => x.Id == id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return await MapEntity(entity, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<Pagination<TEntityDto>> GetAsync(
|
||||
Expression<Func<TEntity, bool>> expression,
|
||||
int pageNumber = 1,
|
||||
int pageSize = 100,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (pageNumber < 1 || pageSize < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("Неверные параметры страницы");
|
||||
}
|
||||
|
||||
var query = _entities.Where(expression);
|
||||
|
||||
return await MapEntityList(query, pageNumber, pageSize, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<Pagination<TEntityDto>> GetAsync(
|
||||
int pageNumber = 1,
|
||||
int pageSize = 100,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (pageNumber < 1 || pageSize < 1)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException("Неверные параметры страницы");
|
||||
}
|
||||
|
||||
return await MapEntityList(_entities, pageNumber, pageSize, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public virtual async Task<TEntityDto?> DeleteAsync(Guid id, CancellationToken cancellationToken)
|
||||
{
|
||||
var entity = await _entities.FirstOrDefaultAsync(x => x.Id == id, cancellationToken);
|
||||
|
||||
if (entity == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
entity.DeletedTime = DateTime.UtcNow;
|
||||
entity.UpdatedTime = DateTime.UtcNow;
|
||||
|
||||
await AddAsync(entity, cancellationToken);
|
||||
|
||||
return await MapEntity(entity, cancellationToken);
|
||||
}
|
||||
|
||||
public abstract Task<TEntityDto?> UpdateAsync(
|
||||
TEntityDto newValue,
|
||||
CancellationToken cancellationToken);
|
||||
|
||||
protected abstract Task<TEntityDto> MapEntity(
|
||||
TEntity entity,
|
||||
CancellationToken cancellationToken);
|
||||
|
||||
protected abstract Task<TEntity> MapEntity(
|
||||
TEntityDto entity,
|
||||
CancellationToken cancellationToken);
|
||||
|
||||
protected abstract Task<Pagination<TEntityDto>> MapEntityList(
|
||||
IQueryable<TEntity> entity,
|
||||
int pageNumber,
|
||||
int pageSize,
|
||||
CancellationToken cancellationToken);
|
||||
|
||||
protected abstract Task AddAsync(TEntity entity, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
using ApplicationSystem.Database.Models;
|
||||
using ApplicationSystem.Database.Models.Interfaces;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace ApplicationSystem.Database.Services.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// Репозиторий для взаимодействия c с сущностями бд
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <typeparam name="TEntityDto"></typeparam>
|
||||
public interface IRepository<TEntity, TEntityDto>
|
||||
where TEntity : class, IEntity
|
||||
where TEntityDto : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Получить сущность по ключу
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
Task<TEntityDto?> GetAsync(Guid id, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Получить список сущностей удовлетворяющих условию
|
||||
/// </summary>
|
||||
/// <param name="expression"></param>
|
||||
/// <param name="page"></param>
|
||||
/// <param name="pageSize"></param>
|
||||
/// <returns></returns>
|
||||
Task<Pagination<TEntityDto>> GetAsync(
|
||||
Expression<Func<TEntity, bool>> expression,
|
||||
int page = 1,
|
||||
int pageSize = 100,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Получить список сущностей
|
||||
/// </summary>
|
||||
/// <param name="page"></param>
|
||||
/// <param name="pageSize"></param>
|
||||
/// <returns></returns>
|
||||
Task<Pagination<TEntityDto>> GetAsync(
|
||||
int page = 1,
|
||||
int pageSize = 100,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Количество сущностей удовлетворяющих условию
|
||||
/// </summary>
|
||||
/// <param name="expression"></param>
|
||||
/// <returns></returns>
|
||||
Task<int> CountAsync(
|
||||
Expression<Func<TEntity, bool>> expression,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Обновить сущность
|
||||
/// </summary>
|
||||
/// <param name="newValue"></param>
|
||||
/// <returns></returns>
|
||||
Task<TEntityDto?> UpdateAsync(
|
||||
TEntityDto newValue,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Удалить сущность
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
Task<TEntityDto?> DeleteAsync(
|
||||
Guid id,
|
||||
CancellationToken cancellationToken = default);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user