From 6d3717db74d807b281686adbd5d379fb1c4ff6cb Mon Sep 17 00:00:00 2001 From: BuzzLeclair Date: Sat, 1 Mar 2025 13:20:28 +0100 Subject: [PATCH 1/2] =?UTF-8?q?-=20Ajout=20de=20toutes=20les=20classes=20R?= =?UTF-8?q?epository=20n=C3=A9cessaires=20au=20projet=20-=20Ajout=20d'une?= =?UTF-8?q?=20classe=20et=20d'une=20interface=20Repo=20g=C3=A9n=C3=A9rique?= =?UTF-8?q?s=20pour=20=C3=A9viter=20la=20r=C3=A9=C3=A9criture=20des=20fonc?= =?UTF-8?q?tions=20CRUD=20de=20base?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ldap-cesi/Repository/RepositoryBase.cs | 114 ++++++++++++++++++ ldap-cesi/Repository/RoleRepository.cs | 13 ++ ldap-cesi/Repository/SalarieRepository.cs | 13 ++ ldap-cesi/Repository/ServiceRepository.cs | 13 ++ .../Repository/Services/IRepositoryBase.cs | 15 +++ .../Repository/Services/IRepositoryRole.cs | 8 ++ .../Repository/Services/IRepositorySalarie.cs | 8 ++ .../Repository/Services/IRepositoryService.cs | 8 ++ .../Repository/Services/IRepositorySite.cs | 8 ++ .../Services/IRepositoryUtilisateur.cs | 8 ++ ldap-cesi/Repository/SiteRepository.cs | 13 ++ ldap-cesi/Repository/UtilisateurRepository.cs | 13 ++ ldap-cesi/ldap-cesi.csproj | 1 - 13 files changed, 234 insertions(+), 1 deletion(-) create mode 100644 ldap-cesi/Repository/RepositoryBase.cs create mode 100644 ldap-cesi/Repository/RoleRepository.cs create mode 100644 ldap-cesi/Repository/SalarieRepository.cs create mode 100644 ldap-cesi/Repository/ServiceRepository.cs create mode 100644 ldap-cesi/Repository/Services/IRepositoryBase.cs create mode 100644 ldap-cesi/Repository/Services/IRepositoryRole.cs create mode 100644 ldap-cesi/Repository/Services/IRepositorySalarie.cs create mode 100644 ldap-cesi/Repository/Services/IRepositoryService.cs create mode 100644 ldap-cesi/Repository/Services/IRepositorySite.cs create mode 100644 ldap-cesi/Repository/Services/IRepositoryUtilisateur.cs create mode 100644 ldap-cesi/Repository/SiteRepository.cs create mode 100644 ldap-cesi/Repository/UtilisateurRepository.cs 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/Services/IRepositoryBase.cs b/ldap-cesi/Repository/Services/IRepositoryBase.cs new file mode 100644 index 0000000..1057e98 --- /dev/null +++ b/ldap-cesi/Repository/Services/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/Services/IRepositoryRole.cs b/ldap-cesi/Repository/Services/IRepositoryRole.cs new file mode 100644 index 0000000..e504b27 --- /dev/null +++ b/ldap-cesi/Repository/Services/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/Services/IRepositorySalarie.cs b/ldap-cesi/Repository/Services/IRepositorySalarie.cs new file mode 100644 index 0000000..c281f5f --- /dev/null +++ b/ldap-cesi/Repository/Services/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/Services/IRepositoryService.cs b/ldap-cesi/Repository/Services/IRepositoryService.cs new file mode 100644 index 0000000..aba697b --- /dev/null +++ b/ldap-cesi/Repository/Services/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/Services/IRepositorySite.cs b/ldap-cesi/Repository/Services/IRepositorySite.cs new file mode 100644 index 0000000..cc098f9 --- /dev/null +++ b/ldap-cesi/Repository/Services/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/Services/IRepositoryUtilisateur.cs b/ldap-cesi/Repository/Services/IRepositoryUtilisateur.cs new file mode 100644 index 0000000..4235770 --- /dev/null +++ b/ldap-cesi/Repository/Services/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/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 @@ - From 31292344b0f4760c02ee826af14b0541112d92ff Mon Sep 17 00:00:00 2001 From: BuzzLeclair Date: Sat, 1 Mar 2025 13:21:05 +0100 Subject: [PATCH 2/2] - Correction du nom du package.... --- ldap-cesi/Repository/{Services => Interfaces}/IRepositoryBase.cs | 0 ldap-cesi/Repository/{Services => Interfaces}/IRepositoryRole.cs | 0 .../Repository/{Services => Interfaces}/IRepositorySalarie.cs | 0 .../Repository/{Services => Interfaces}/IRepositoryService.cs | 0 ldap-cesi/Repository/{Services => Interfaces}/IRepositorySite.cs | 0 .../Repository/{Services => Interfaces}/IRepositoryUtilisateur.cs | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename ldap-cesi/Repository/{Services => Interfaces}/IRepositoryBase.cs (100%) rename ldap-cesi/Repository/{Services => Interfaces}/IRepositoryRole.cs (100%) rename ldap-cesi/Repository/{Services => Interfaces}/IRepositorySalarie.cs (100%) rename ldap-cesi/Repository/{Services => Interfaces}/IRepositoryService.cs (100%) rename ldap-cesi/Repository/{Services => Interfaces}/IRepositorySite.cs (100%) rename ldap-cesi/Repository/{Services => Interfaces}/IRepositoryUtilisateur.cs (100%) diff --git a/ldap-cesi/Repository/Services/IRepositoryBase.cs b/ldap-cesi/Repository/Interfaces/IRepositoryBase.cs similarity index 100% rename from ldap-cesi/Repository/Services/IRepositoryBase.cs rename to ldap-cesi/Repository/Interfaces/IRepositoryBase.cs diff --git a/ldap-cesi/Repository/Services/IRepositoryRole.cs b/ldap-cesi/Repository/Interfaces/IRepositoryRole.cs similarity index 100% rename from ldap-cesi/Repository/Services/IRepositoryRole.cs rename to ldap-cesi/Repository/Interfaces/IRepositoryRole.cs diff --git a/ldap-cesi/Repository/Services/IRepositorySalarie.cs b/ldap-cesi/Repository/Interfaces/IRepositorySalarie.cs similarity index 100% rename from ldap-cesi/Repository/Services/IRepositorySalarie.cs rename to ldap-cesi/Repository/Interfaces/IRepositorySalarie.cs diff --git a/ldap-cesi/Repository/Services/IRepositoryService.cs b/ldap-cesi/Repository/Interfaces/IRepositoryService.cs similarity index 100% rename from ldap-cesi/Repository/Services/IRepositoryService.cs rename to ldap-cesi/Repository/Interfaces/IRepositoryService.cs diff --git a/ldap-cesi/Repository/Services/IRepositorySite.cs b/ldap-cesi/Repository/Interfaces/IRepositorySite.cs similarity index 100% rename from ldap-cesi/Repository/Services/IRepositorySite.cs rename to ldap-cesi/Repository/Interfaces/IRepositorySite.cs diff --git a/ldap-cesi/Repository/Services/IRepositoryUtilisateur.cs b/ldap-cesi/Repository/Interfaces/IRepositoryUtilisateur.cs similarity index 100% rename from ldap-cesi/Repository/Services/IRepositoryUtilisateur.cs rename to ldap-cesi/Repository/Interfaces/IRepositoryUtilisateur.cs