diff --git a/ldap-cesi/Repository/Interfaces/IRepositoryBase.cs b/ldap-cesi/Repository/Interfaces/IRepositoryBase.cs new file mode 100644 index 0000000..1057e98 --- /dev/null +++ b/ldap-cesi/Repository/Interfaces/IRepositoryBase.cs @@ -0,0 +1,15 @@ +using System.Linq.Expressions; + +namespace ldap_cesi.Repository.Services; + +public interface IRepositoryBase where TEntity : class +{ + Task AddAsync(TEntity entity, CancellationToken cancellationToken = default); + Task AnyAsync(Expression> predicate, CancellationToken cancellationToken = default); + Task GetByIdAsync(TId id, CancellationToken cancellationToken = default) where TId : notnull; + Task> GetAllAsync(CancellationToken cancellationToken = default); + Task UpdateAsync(TEntity entity, CancellationToken cancellationToken = default); + Task DeleteAsync(TEntity entity, CancellationToken cancellationToken = default); + Task FirstOrDefaultAsync(Expression> predicate, + CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/ldap-cesi/Repository/Interfaces/IRepositoryRole.cs b/ldap-cesi/Repository/Interfaces/IRepositoryRole.cs new file mode 100644 index 0000000..e504b27 --- /dev/null +++ b/ldap-cesi/Repository/Interfaces/IRepositoryRole.cs @@ -0,0 +1,8 @@ +using ldap_cesi.Entities; + +namespace ldap_cesi.Repository.Services; + +public interface IRepositoryRole : IRepositoryBase +{ + +} \ No newline at end of file diff --git a/ldap-cesi/Repository/Interfaces/IRepositorySalarie.cs b/ldap-cesi/Repository/Interfaces/IRepositorySalarie.cs new file mode 100644 index 0000000..c281f5f --- /dev/null +++ b/ldap-cesi/Repository/Interfaces/IRepositorySalarie.cs @@ -0,0 +1,8 @@ +using ldap_cesi.Entities; + +namespace ldap_cesi.Repository.Services; + +public interface IRepositorySalarie : IRepositoryBase +{ + +} \ No newline at end of file diff --git a/ldap-cesi/Repository/Interfaces/IRepositoryService.cs b/ldap-cesi/Repository/Interfaces/IRepositoryService.cs new file mode 100644 index 0000000..aba697b --- /dev/null +++ b/ldap-cesi/Repository/Interfaces/IRepositoryService.cs @@ -0,0 +1,8 @@ +using ldap_cesi.Entities; + +namespace ldap_cesi.Repository.Services; + +public interface IRepositoryService : IRepositoryBase +{ + +} \ No newline at end of file diff --git a/ldap-cesi/Repository/Interfaces/IRepositorySite.cs b/ldap-cesi/Repository/Interfaces/IRepositorySite.cs new file mode 100644 index 0000000..cc098f9 --- /dev/null +++ b/ldap-cesi/Repository/Interfaces/IRepositorySite.cs @@ -0,0 +1,8 @@ +using ldap_cesi.Entities; + +namespace ldap_cesi.Repository.Services; + +public interface IRepositorySite : IRepositoryBase +{ + +} \ No newline at end of file diff --git a/ldap-cesi/Repository/Interfaces/IRepositoryUtilisateur.cs b/ldap-cesi/Repository/Interfaces/IRepositoryUtilisateur.cs new file mode 100644 index 0000000..4235770 --- /dev/null +++ b/ldap-cesi/Repository/Interfaces/IRepositoryUtilisateur.cs @@ -0,0 +1,8 @@ +using ldap_cesi.Entities; + +namespace ldap_cesi.Repository.Services; + +public interface IRepositoryUtilisateur : IRepositoryBase +{ + +} \ No newline at end of file diff --git a/ldap-cesi/Repository/RepositoryBase.cs b/ldap-cesi/Repository/RepositoryBase.cs new file mode 100644 index 0000000..911ddcd --- /dev/null +++ b/ldap-cesi/Repository/RepositoryBase.cs @@ -0,0 +1,114 @@ +using System.Linq.Expressions; +using ldap_cesi.Context; +using ldap_cesi.Repository.Services; +using Microsoft.EntityFrameworkCore; + +namespace ldap_cesi.Repository; + +public class RepositoryBase : IRepositoryBase where TEntity : class +{ + protected readonly PgContext _context; + + public RepositoryBase(PgContext context) + { + _context = context ?? throw new ArgumentNullException(nameof(context)); + } + public virtual async Task AddAsync(TEntity entity, CancellationToken cancellationToken = default) + { + try + { + _context.Set().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 AnyAsync(Expression> predicate, CancellationToken cancellationToken = default) + { + return await _context.Set().AnyAsync(predicate, cancellationToken); + } + + + public virtual async Task> GetAllAsync(CancellationToken cancellationToken = default) + { + try + { + return await _context.Set().ToListAsync(cancellationToken); + } + catch (Exception ex) + { + throw new Exception("Erreur pendant la récupérations des entités.", ex); + } + } + public virtual async Task FirstOrDefaultAsync(Expression> predicate, CancellationToken cancellationToken = default) + { + return await _context.Set().FirstOrDefaultAsync(predicate, cancellationToken); + } + + public virtual async Task UpdateAsync(TEntity entity, CancellationToken cancellationToken = default) + { + try + { + _context.Set().Update(entity); + await SaveChangesAsync(cancellationToken); + } + catch (Exception ex) + { + throw new Exception("Erreur pendant la mis à jour", ex); + } + } + + public virtual async Task GetByIdAsync(TId id, CancellationToken cancellationToken = default) where TId : notnull + { + try + { + return await _context.FindAsync(id, cancellationToken); + } + catch (Exception ex) + { + throw new Exception($"Erreur lors de la récupération avec l'id : {id}.", ex); + } + } + + public virtual async Task DeleteAsync(TEntity entity, CancellationToken cancellationToken = default) + { + try + { + _context.Set().Remove(entity); + await SaveChangesAsync(cancellationToken); + } + catch (Exception ex) + { + throw new Exception("Erreur pendant la suppression de l'entité", ex); + } + } + + protected async Task 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> ListAsync(CancellationToken cancellationToken = default) + { + try + { + return await _context.Set().ToListAsync(cancellationToken); + } + catch (Exception ex) + { + throw new Exception("Erreur qui concerne le listing des entités", ex); + } + } + +} \ No newline at end of file diff --git a/ldap-cesi/Repository/RoleRepository.cs b/ldap-cesi/Repository/RoleRepository.cs new file mode 100644 index 0000000..2d7ee07 --- /dev/null +++ b/ldap-cesi/Repository/RoleRepository.cs @@ -0,0 +1,13 @@ +using ldap_cesi.Context; +using ldap_cesi.Entities; +using ldap_cesi.Repository.Services; + +namespace ldap_cesi.Repository; + +public class RoleRepository : RepositoryBase, IRepositoryRole +{ + public RoleRepository(PgContext context) : base(context) + { + + } +} \ No newline at end of file diff --git a/ldap-cesi/Repository/SalarieRepository.cs b/ldap-cesi/Repository/SalarieRepository.cs new file mode 100644 index 0000000..bb84bb1 --- /dev/null +++ b/ldap-cesi/Repository/SalarieRepository.cs @@ -0,0 +1,13 @@ +using ldap_cesi.Context; +using ldap_cesi.Entities; +using ldap_cesi.Repository.Services; + +namespace ldap_cesi.Repository; + +public class SalarieRepository : RepositoryBase, IRepositorySalarie +{ + public SalarieRepository(PgContext context) : base(context) + { + + } +} \ No newline at end of file diff --git a/ldap-cesi/Repository/ServiceRepository.cs b/ldap-cesi/Repository/ServiceRepository.cs new file mode 100644 index 0000000..e0f15e8 --- /dev/null +++ b/ldap-cesi/Repository/ServiceRepository.cs @@ -0,0 +1,13 @@ +using ldap_cesi.Context; +using ldap_cesi.Entities; +using ldap_cesi.Repository.Services; + +namespace ldap_cesi.Repository; + +public class ServiceRepository : RepositoryBase, IRepositoryService +{ + public ServiceRepository(PgContext context) : base(context) + { + + } +} \ No newline at end of file diff --git a/ldap-cesi/Repository/SiteRepository.cs b/ldap-cesi/Repository/SiteRepository.cs new file mode 100644 index 0000000..3b522b8 --- /dev/null +++ b/ldap-cesi/Repository/SiteRepository.cs @@ -0,0 +1,13 @@ +using ldap_cesi.Context; +using ldap_cesi.Entities; +using ldap_cesi.Repository.Services; + +namespace ldap_cesi.Repository; + +public class SiteRepository : RepositoryBase, IRepositorySite +{ + public SiteRepository(PgContext context) : base(context) + { + + } +} \ No newline at end of file diff --git a/ldap-cesi/Repository/UtilisateurRepository.cs b/ldap-cesi/Repository/UtilisateurRepository.cs new file mode 100644 index 0000000..57cf96c --- /dev/null +++ b/ldap-cesi/Repository/UtilisateurRepository.cs @@ -0,0 +1,13 @@ +using ldap_cesi.Context; +using ldap_cesi.Entities; +using ldap_cesi.Repository.Services; + +namespace ldap_cesi.Repository; + +public class UtilisateurRepository : RepositoryBase, IRepositoryUtilisateur +{ + public UtilisateurRepository(PgContext context) : base(context) + { + + } +} \ No newline at end of file diff --git a/ldap-cesi/ldap-cesi.csproj b/ldap-cesi/ldap-cesi.csproj index b240452..99d7a59 100644 --- a/ldap-cesi/ldap-cesi.csproj +++ b/ldap-cesi/ldap-cesi.csproj @@ -28,7 +28,6 @@ -