184 lines
6.6 KiB
C#
184 lines
6.6 KiB
C#
using System.Linq.Expressions;
|
|
using ldap_cesi.Context;
|
|
using ldap_cesi.Models;
|
|
using ldap_cesi.Repository.Services;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ldap_cesi.Repository;
|
|
|
|
public class RepositoryBase<TEntity> : IRepositoryBase<TEntity> where TEntity : class
|
|
{
|
|
protected readonly PgContext _context;
|
|
protected readonly DbSet<TEntity> _dbSet;
|
|
public RepositoryBase(PgContext context)
|
|
{
|
|
_context = context ?? throw new ArgumentNullException(nameof(context));
|
|
_dbSet = context.Set<TEntity>();
|
|
}
|
|
public virtual async Task<TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
_context.Set<TEntity>().Add(entity);
|
|
await SaveChangesAsync(cancellationToken);
|
|
return entity;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("Erreur pendant l'ajout de l'entité.", ex);
|
|
}
|
|
}
|
|
|
|
public virtual async Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
|
|
{
|
|
return await _context.Set<TEntity>().AnyAsync(predicate, cancellationToken);
|
|
}
|
|
|
|
public virtual async Task<TEntity?> GetByIdAsync<TId>(TId id, CancellationToken cancellationToken = default) where TId : notnull
|
|
{
|
|
try
|
|
{
|
|
return await _context.FindAsync<TEntity>(id, cancellationToken);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception($"Erreur lors de la récupération avec l'id : {id}.", ex);
|
|
}
|
|
}
|
|
|
|
|
|
public virtual async Task<(List<TEntity> Data, int TotalPages, int TotalItems)> GetAllAsync(int pageNumber = 1, int pageSize = 10, CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
var totalCount = await _dbSet.CountAsync(cancellationToken);
|
|
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
|
|
|
|
var data = await _dbSet
|
|
.Skip((pageNumber - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
return (data, totalPages, totalCount);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("Erreur pendant la récupération des entités.", ex);
|
|
}
|
|
}
|
|
|
|
public virtual async Task<TEntity?> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
|
|
{
|
|
return await _context.Set<TEntity>().FirstOrDefaultAsync(predicate, cancellationToken);
|
|
}
|
|
|
|
public virtual async Task<bool> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
_context.Set<TEntity>().Update(entity);
|
|
await SaveChangesAsync(cancellationToken);
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("Erreur pendant la mis à jour", ex);
|
|
}
|
|
}
|
|
|
|
public virtual async Task<(List<TEntity> Data, int TotalPages, int TotalItems)> GetAllWithRelationsAsync(int pageNumber = 1, int pageSize = 10, params Expression<Func<TEntity, object>>[] relationInclues)
|
|
{
|
|
IQueryable<TEntity> query = _dbSet;
|
|
|
|
foreach (var relationInclue in relationInclues)
|
|
{
|
|
query = query.Include(relationInclue);
|
|
}
|
|
|
|
var totalCount = await query.CountAsync();
|
|
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
|
|
|
|
var data = await query
|
|
.Skip((pageNumber - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync();
|
|
|
|
return (data, totalPages, totalCount);
|
|
}
|
|
|
|
public virtual async Task<bool> DeleteAsync(TEntity entity, CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
_context.Set<TEntity>().Remove(entity);
|
|
await SaveChangesAsync(cancellationToken);
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("Erreur pendant la suppression de l'entité", ex);
|
|
}
|
|
}
|
|
|
|
protected async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
try
|
|
{
|
|
return await _context.SaveChangesAsync(cancellationToken);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("Erreur pendant le sauvegarde en base de donnése.", ex);
|
|
}
|
|
}
|
|
|
|
|
|
public virtual async Task<TEntity> GetWithRelationsAsync(int id, params Expression<Func<TEntity, object>>[] relationInclues)
|
|
{
|
|
IQueryable<TEntity> query = _dbSet;
|
|
|
|
foreach (var relationInclue in relationInclues)
|
|
{
|
|
query = query.Include(relationInclue);
|
|
}
|
|
|
|
return await query.FirstOrDefaultAsync(e => EF.Property<int>(e, "Id") == id);
|
|
}
|
|
|
|
public virtual async Task<(List<TEntity> Data, int TotalPages, int TotalItems)> SearchAsync(
|
|
Expression<Func<TEntity, bool>> predicate,
|
|
int pageNumber = 1,
|
|
int pageSize = 10,
|
|
params Expression<Func<TEntity, object>>[] relationsAInclude)
|
|
{
|
|
IQueryable<TEntity> query = _dbSet;
|
|
|
|
foreach (var relationInclue in relationsAInclude)
|
|
{
|
|
query = query.Include(relationInclue);
|
|
}
|
|
|
|
query = query.Where(predicate);
|
|
|
|
var totalCount = await query.CountAsync();
|
|
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
|
|
|
|
var data = await query
|
|
.Skip((pageNumber - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync();
|
|
|
|
return (data, totalPages, totalCount);
|
|
}
|
|
|
|
public virtual async Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
|
|
{
|
|
return await _dbSet.CountAsync(predicate, cancellationToken);
|
|
}
|
|
|
|
public virtual async Task<int> CountRelatedEntitiesAsync<TRelated>(int id, Expression<Func<TRelated, bool>> predicate) where TRelated : class
|
|
{
|
|
return await _context.Set<TRelated>().CountAsync(predicate);
|
|
}
|
|
|
|
} |