Generic get by id, refacto par Alex

pull/2/head
Clement FERRERE 3 years ago
parent e8beae3b90
commit f1de70eafa
  1. 10
      Collaborateur_Epa_Back/espacecollab.backend.api/Controllers/CollaborateursController.cs
  2. 26
      Collaborateur_Epa_Back/espacecollab.backend.api/Controllers/CollaborateursControllerTestGeneric.cs
  3. 12
      Collaborateur_Epa_Back/espacecollab.backend.api/Startup.cs
  4. 2
      Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/Mappers/EnumGenreMapper.cs
  5. 2
      Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/Mappers/EnumStatutMapper.cs
  6. 20
      Collaborateur_Epa_Back/espacecollab.backend.appservices/CollaborateursServices.cs
  7. 92
      Collaborateur_Epa_Back/espacecollab.backend.appservices/GenericsServices.cs
  8. 9
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/CollaborateurSqlDto.cs
  9. 4
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/PeriodeEssaiSqlDto.cs
  10. 2
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/Values/EnumGenreSql.cs
  11. 2
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/Values/EnumIssueSql.cs
  12. 2
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/Values/EnumStatutSql.cs
  13. 33
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/FakeRepo/FakeCollaborateurRepository.cs
  14. 4
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/SqlRepo/CollaborateurSqlRepository.cs
  15. 7
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/SqlRepo/Interface/IAgenceRepository.cs
  16. 7
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/SqlRepo/Interface/IBusinessUnitRepository.cs
  17. 2
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/SqlRepo/Interface/ICollaborateurRepository.cs
  18. 6
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/SqlRepo/Interface/IGenericRepository.cs
  19. 7
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/SqlRepo/Interface/IPeriodeEssaiRepository.cs
  20. 4
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/SqlRepo/Interface/IProjetRepository.cs
  21. 3
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/SqlRepo/Interface/IReferencementRepository.cs

@ -19,20 +19,16 @@ namespace espacecollab.backend.api.Controllers
[HttpGet] [HttpGet]
public ActionResult<IEnumerable<CollaborateurApiDto>> GetCollaborateurs() public ActionResult<IEnumerable<CollaborateurApiDto>> GetCollaborateurs()
{ {
IEnumerable<CollaborateurApiDto> collaborateurs = CollaborateursServices.GetCollaborateurs(); return Ok(CollaborateursServices.GetAll());
if (!collaborateurs.Any())
{
return NotFound();
}
return Ok(collaborateurs);
} }
[HttpGet("{collaborateurId}")] [HttpGet("{collaborateurId}")]
public ActionResult<CollaborateurApiDto> GetCollaborateurById(int collaborateurId) public ActionResult<CollaborateurApiDto> GetCollaborateurById(int collaborateurId)
{ {
CollaborateurApiDto? collaborateur = CollaborateursServices.GetCollaborateurById(collaborateurId); CollaborateurApiDto? collaborateur = CollaborateursServices.GetById(collaborateurId);
if (collaborateur == null) if (collaborateur == null)
return NotFound(); return NotFound();
return Ok(collaborateur); return Ok(collaborateur);
} }

@ -1,26 +0,0 @@
using espacecollab.backend.appservices;
using espacecollab.backend.appservices.dtos;
using Microsoft.AspNetCore.Mvc;
namespace espacecollab.backend.api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class CollaborateursControllerTestGeneric : ControllerBase
{
private CollaborateursServices CollaborateursServices { get; }
public CollaborateursControllerTestGeneric(CollaborateursServices collaborateursServices)
{
CollaborateursServices = collaborateursServices;
}
// GET: api/collaborateurs
//[HttpGet]
//public ActionResult<IEnumerable<CollaborateurApiDto>> GetCollaborateurs()
//{
// return CollaborateursServices.GetAll();
//}
}
}

@ -19,8 +19,6 @@ namespace Api
// This method gets called by the runtime. Use this method to add services to the container. // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
IConfigurationSection sqlSection = Configuration.GetSection(SqlOption.Key); IConfigurationSection sqlSection = Configuration.GetSection(SqlOption.Key);
services.Configure<SqlOption>(sqlSection); services.Configure<SqlOption>(sqlSection);
SqlOption contextOptions = sqlSection.Get<SqlOption>(); SqlOption contextOptions = sqlSection.Get<SqlOption>();
@ -30,18 +28,14 @@ namespace Api
if (contextOptions.LoadFake) if (contextOptions.LoadFake)
{ {
services.AddScoped<ICollaborateurRepository, FakeCollaborateurRepository>();
} }
else else
{ {
//services.AddScoped<ICollaborateurRepository, CollaborateurRepository>(); services.AddScoped<ICollaborateurRepository, CollaborateurSqlRepository>();
} }
//services.AddScoped<IGenericRepository<CollaborateurSqlDto>, GenericRepository<CollaborateurSqlDto>>();
//services.AddScoped<IGenericRepository<CollaborateurSqlDto>, FakeCollaborateurRepository>();
services.AddScoped<ICollaborateurRepository, FakeCollaborateurRepository>();
services.AddScoped<CollaborateursServices>();
services.AddScoped<CollaborateursServices>();
services.AddScoped<MainDbContext>(); services.AddScoped<MainDbContext>();

@ -1,4 +1,4 @@
using espacecollab.backend.infrastructure.sql.dtos; using espacecollab.backend.infrastructure.sql.dtos.Values;
namespace espacecollab.backend.appservices.dtos.Mappers namespace espacecollab.backend.appservices.dtos.Mappers
{ {

@ -1,4 +1,4 @@
using espacecollab.backend.infrastructure.sql.dtos; using espacecollab.backend.infrastructure.sql.dtos.Values;
namespace espacecollab.backend.appservices.dtos.Mappers namespace espacecollab.backend.appservices.dtos.Mappers
{ {

@ -7,23 +7,14 @@ using espacecollab.backend.infrastructure.sql.SqlRepo.Interface;
namespace espacecollab.backend.appservices namespace espacecollab.backend.appservices
{ {
public class CollaborateursServices public class CollaborateursServices : GenericsServices<CollaborateurSqlDto, CollaborateurApiDto>
{ {
private ICollaborateurRepository CollaborateurRepository { get; } private ICollaborateurRepository CollaborateurRepository { get; }
public CollaborateursServices(ICollaborateurRepository collaborateur) public CollaborateursServices(ICollaborateurRepository collaborateurRepository)
:base(collaborateurRepository, CollaborateurMapper.ToApi, CollaborateurMapper.ToSql)
{ {
CollaborateurRepository = collaborateur; CollaborateurRepository = collaborateurRepository;
}
public IEnumerable<CollaborateurApiDto> GetCollaborateurs()
{
return CollaborateurRepository.GetAll().Select(collaborateurSql => collaborateurSql.ToApi());
}
public CollaborateurApiDto? GetCollaborateurById(int id)
{
return CollaborateurRepository.GetById(id)?.ToApi();
} }
public CollaborateurApiDto? AddCollaborateur(CollaborateurApiDto collaborateurApi) public CollaborateurApiDto? AddCollaborateur(CollaborateurApiDto collaborateurApi)
@ -67,7 +58,8 @@ namespace espacecollab.backend.appservices
{ {
if (string.IsNullOrEmpty(apsideMail)) if (string.IsNullOrEmpty(apsideMail))
return null; return null;
return CollaborateurRepository.GetCollaborateurByApsideMail(apsideMail).ToApi();
return CollaborateurRepository.GetCollaborateurByApsideMail(apsideMail)?.ToApi();
} }
} }
} }

@ -1,49 +1,53 @@
using espacecollab.backend.appservices.dtos; using espacecollab.backend.infrastructure.sql.dtos;
using espacecollab.backend.infrastructure.sql.dtos;
using espacecollab.backend.infrastructure.sql.SqlRepo.Interface; using espacecollab.backend.infrastructure.sql.SqlRepo.Interface;
namespace espacecollab.backend.appservices namespace espacecollab.backend.appservices
{ {
//public abstract class GenericsServices<T, To> where T : class, IGenericSqlDto public abstract class GenericsServices<T, To> where T : class, IGenericSqlDto
// where To : GenericApiDtoBase<T, To>, new() where To : class
//{ {
// private IGenericRepository GenericRepository { get; } Func<T, To> MapperToApiDto { get; }
Func<To, T> MapperToSqlDto { get; }
// public GenericsServices(IGenericRepository genericRepository)
// { private IGenericRepository<T> GenericRepository { get; }
// GenericRepository = genericRepository;
// } public GenericsServices(IGenericRepository<T> genericRepository, Func<T, To> mapperToApiDto, Func<To, T> mapperToSqlDto)
{
// public To? Add(To apiDto) GenericRepository = genericRepository;
// { MapperToApiDto = mapperToApiDto;
// T? entity = GenericRepository.Add(apiDto.ToEntity()); MapperToSqlDto = mapperToSqlDto;
// if (entity == null) }
// return null;
//public To? Add(To apiDto)
// return new To().FromEntity(entity); //{
// } // T? entity = GenericRepository.Add(apiDto.ToEntity());
// if (entity == null)
// public IEnumerable<T> GetAll<T>() where T : class, IGenericSqlDto // return null;
// {
// return GenericRepository.GetAll<T>(); // return new To().FromEntity(entity);
// } //}
// public To? GetById<T>(int id) where T : class, IGenericSqlDto public IEnumerable<To> GetAll()
// { {
// T? entity = GenericRepository.GetById<T>(id); return GenericRepository.GetAll().Select(entity => MapperToApiDto(entity)).ToList();
// if (entity == null) }
// return null;
public To? GetById(int id)
// return new To().FromEntity(entity); {
// } T? entity = GenericRepository.GetById(id);
if (entity == null)
// public To? Update(To apiDto) return null;
// {
// T? entity = GenericRepository.Update<T>(apiDto.ToEntity()); return MapperToApiDto(entity);
// if (entity == null) }
// return null;
//public To? Update(To apiDto)
// return new To().FromEntity(entity); //{
// } // T? entity = GenericRepository.Update<T>(apiDto.ToEntity());
//} // if (entity == null)
// return null;
// return new To().FromEntity(entity);
//}
}
} }

@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis; using espacecollab.backend.infrastructure.sql.dtos.Values;
using System.Diagnostics.CodeAnalysis;
namespace espacecollab.backend.infrastructure.sql.dtos namespace espacecollab.backend.infrastructure.sql.dtos
{ {
@ -22,6 +23,12 @@ namespace espacecollab.backend.infrastructure.sql.dtos
[ExcludeFromCodeCoverage] [ExcludeFromCodeCoverage]
private CollaborateurSqlDto() private CollaborateurSqlDto()
{ {
Name = string.Empty;
FirstName = string.Empty;
Address = string.Empty;
Telephone = string.Empty;
PersonalMail = string.Empty;
ApsideMail = string.Empty;
} }
public CollaborateurSqlDto(int id, string name, string firstName, DateTime birthDate, EnumGenreSql gender, EnumStatutSql status, int childrenNumber, string address, string telephone, string personalMail, string apsideMail, DateTime resignationDate, int referrerId, int businessUnitId) public CollaborateurSqlDto(int id, string name, string firstName, DateTime birthDate, EnumGenreSql gender, EnumStatutSql status, int childrenNumber, string address, string telephone, string personalMail, string apsideMail, DateTime resignationDate, int referrerId, int businessUnitId)

@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis; using espacecollab.backend.infrastructure.sql.dtos.Values;
using System.Diagnostics.CodeAnalysis;
namespace espacecollab.backend.infrastructure.sql.dtos namespace espacecollab.backend.infrastructure.sql.dtos
{ {
@ -15,6 +16,7 @@ namespace espacecollab.backend.infrastructure.sql.dtos
[ExcludeFromCodeCoverage] [ExcludeFromCodeCoverage]
private PeriodeEssaiSqlDto() private PeriodeEssaiSqlDto()
{ {
Comment = string.Empty;
} }
public PeriodeEssaiSqlDto(int id, DateTime startingDate, DateTime plannedEndingDate, DateTime realEndingDate, string comment, EnumIssueSql issue, int collaborateurId) public PeriodeEssaiSqlDto(int id, DateTime startingDate, DateTime plannedEndingDate, DateTime realEndingDate, string comment, EnumIssueSql issue, int collaborateurId)

@ -1,4 +1,4 @@
namespace espacecollab.backend.infrastructure.sql.dtos namespace espacecollab.backend.infrastructure.sql.dtos.Values
{ {
public enum EnumGenreSql public enum EnumGenreSql
{ {

@ -1,4 +1,4 @@
namespace espacecollab.backend.infrastructure.sql.dtos namespace espacecollab.backend.infrastructure.sql.dtos.Values
{ {
public enum EnumIssueSql public enum EnumIssueSql
{ {

@ -1,4 +1,4 @@
namespace espacecollab.backend.infrastructure.sql.dtos namespace espacecollab.backend.infrastructure.sql.dtos.Values
{ {
public enum EnumStatutSql public enum EnumStatutSql
{ {

@ -1,4 +1,5 @@
using espacecollab.backend.infrastructure.sql.dtos; using espacecollab.backend.infrastructure.sql.dtos;
using espacecollab.backend.infrastructure.sql.dtos.Values;
using espacecollab.backend.infrastructure.sql.SqlRepo; using espacecollab.backend.infrastructure.sql.SqlRepo;
using espacecollab.backend.infrastructure.sql.SqlRepo.Interface; using espacecollab.backend.infrastructure.sql.SqlRepo.Interface;
@ -7,25 +8,23 @@ namespace espacecollab.backend.infrastructure.sql.FakeRepo
public class FakeCollaborateurRepository : GenericRepository<CollaborateurSqlDto>, ICollaborateurRepository public class FakeCollaborateurRepository : GenericRepository<CollaborateurSqlDto>, ICollaborateurRepository
{ {
private List<CollaborateurSqlDto> Collaborateurs { get; set; } private IList<CollaborateurSqlDto> Collaborateurs { get; } = new List<CollaborateurSqlDto>
//Context obligatoire car le Generic Repository en a un.
public FakeCollaborateurRepository()
{
Collaborateurs = new List<CollaborateurSqlDto>
{ {
new CollaborateurSqlDto(new int(), "Dupont", "Jean", new DateTime(1980, 12, 10), new CollaborateurSqlDto(1, "Dupont", "Jean", new DateTime(1980, 12, 10),
EnumGenreSql.MASCULIN, EnumStatutSql.NONCADRE, 0, "1 rue du Louvre, 63000, Clermont-Ferrand", "0660258644", EnumGenreSql.MASCULIN, EnumStatutSql.NONCADRE, 0, "1 rue du Louvre, 63000, Clermont-Ferrand", "0660258644",
"jean.dupont@gmail.com", "jean.dupont@apside-groupe.com", new DateTime(2023, 12, 17), new int(), new int()), "jean.dupont@gmail.com", "jean.dupont@apside-groupe.com", new DateTime(2023, 12, 17), 2, 1),
new CollaborateurSqlDto(new int(), "Michel", "Laura", new DateTime(1985, 08, 12),
new CollaborateurSqlDto(2, "Michel", "Laura", new DateTime(1985, 08, 12),
EnumGenreSql.FEMININ, EnumStatutSql.NONCADRE, 0, "5 rue du Louvre, 63000, Clermont-Ferrand", "0660258644", EnumGenreSql.FEMININ, EnumStatutSql.NONCADRE, 0, "5 rue du Louvre, 63000, Clermont-Ferrand", "0660258644",
"laura.michel@gmail.com", "laura.michel@apside-groupe.com", new DateTime(2023, 12, 17), new int(), new int()) "laura.michel@gmail.com", "laura.michel@apside-groupe.com", new DateTime(2023, 12, 17), 1, 1)
}; };
public FakeCollaborateurRepository()
{
} }
//redéfinition des méthodes du GenericRepository pour fonctionner avec Fake #region redéfinition des méthodes du GenericRepository pour fonctionner avec Fake
public new CollaborateurSqlDto? GetById(int id) public new CollaborateurSqlDto? GetById(int id)
{ {
return Collaborateurs.FirstOrDefault(entity => entity.Id == id); return Collaborateurs.FirstOrDefault(entity => entity.Id == id);
@ -61,13 +60,15 @@ namespace espacecollab.backend.infrastructure.sql.FakeRepo
return collaborateur; return collaborateur;
} }
//méthodes spécifiques à l'interface ICollaborateurRepository #endregion
#region méthodes spécifiques à l'interface ICollaborateurRepository
public IList<CollaborateurSqlDto> GetCollaborateursByBusinessUnit(int businessUnitId) public IList<CollaborateurSqlDto> GetCollaborateursByBusinessUnit(int businessUnitId)
{ {
return Collaborateurs.Where(entity => entity.BusinessUnitId == businessUnitId).ToList(); return Collaborateurs.Where(entity => entity.BusinessUnitId == businessUnitId).ToList();
} }
public IList<CollaborateurSqlDto> GetCollaborateursByReferrer(int referrerId) public IList<CollaborateurSqlDto> GetCollaborateursByReferrer(int referrerId)
{ {
return Collaborateurs.Where(entity => entity.ReferrerId == referrerId).ToList(); return Collaborateurs.Where(entity => entity.ReferrerId == referrerId).ToList();
@ -77,5 +78,7 @@ namespace espacecollab.backend.infrastructure.sql.FakeRepo
{ {
return Collaborateurs.First(entity => entity.ApsideMail == apsideMail); return Collaborateurs.First(entity => entity.ApsideMail == apsideMail);
} }
#endregion
} }
} }

@ -3,11 +3,11 @@ using espacecollab.backend.infrastructure.sql.SqlRepo.Interface;
namespace espacecollab.backend.infrastructure.sql.SqlRepo namespace espacecollab.backend.infrastructure.sql.SqlRepo
{ {
public class CollaborateurRepository : GenericRepository<CollaborateurSqlDto>,ICollaborateurRepository public class CollaborateurSqlRepository : GenericRepository<CollaborateurSqlDto>,ICollaborateurRepository
{ {
private MainDbContext Context { get; } private MainDbContext Context { get; }
public CollaborateurRepository(MainDbContext context) : base(context) public CollaborateurSqlRepository(MainDbContext context) : base(context)
{ {
Context = context; Context = context;
} }

@ -2,12 +2,7 @@
namespace espacecollab.backend.infrastructure.sql.SqlRepo.Interface namespace espacecollab.backend.infrastructure.sql.SqlRepo.Interface
{ {
public interface IAgenceRepository public interface IAgenceRepository : IGenericRepository<AgenceSqlDto>
{ {
//inutile !
AgenceSqlDto? AddAgence(AgenceSqlDto newAgence);
IList<AgenceSqlDto>? GetAllAgences();
AgenceSqlDto? GetAgenceById(AgenceSqlDto agence);
AgenceSqlDto UpdateAgence(AgenceSqlDto agence);
} }
} }

@ -2,12 +2,7 @@
namespace espacecollab.backend.infrastructure.sql.SqlRepo.Interface namespace espacecollab.backend.infrastructure.sql.SqlRepo.Interface
{ {
public interface IBusinessUnitRepository public interface IBusinessUnitRepository : IGenericRepository<BusinessUnitSqlDto>
{ {
//inutile !
BusinessUnitSqlDto? AddBusinessUnit(BusinessUnitSqlDto newBusinessUnit);
IList<BusinessUnitSqlDto>? GetAllBusinessUnits();
BusinessUnitSqlDto? GetBusinessUnitById(BusinessUnitSqlDto businessUnit);
BusinessUnitSqlDto UpdateBusinessUnit(BusinessUnitSqlDto businessUnit);
} }
} }

@ -4,7 +4,7 @@ namespace espacecollab.backend.infrastructure.sql.SqlRepo.Interface
{ {
public interface ICollaborateurRepository : IGenericRepository<CollaborateurSqlDto> public interface ICollaborateurRepository : IGenericRepository<CollaborateurSqlDto>
{ {
CollaborateurSqlDto GetCollaborateurByApsideMail(string apsideMail); CollaborateurSqlDto? GetCollaborateurByApsideMail(string apsideMail);
IList<CollaborateurSqlDto> GetCollaborateursByBusinessUnit(int businessUnitId); IList<CollaborateurSqlDto> GetCollaborateursByBusinessUnit(int businessUnitId);
IList<CollaborateurSqlDto> GetCollaborateursByReferrer(int referrerId); IList<CollaborateurSqlDto> GetCollaborateursByReferrer(int referrerId);
} }

@ -9,10 +9,6 @@ namespace espacecollab.backend.infrastructure.sql.SqlRepo.Interface
IEnumerable<T> GetAll(); IEnumerable<T> GetAll();
T? GetById(int id); T? GetById(int id);
T? Update(T entity); T? Update(T entity);
//void GetAll<T>() where T : class, IGenericSqlDto;
} }
} }

@ -2,12 +2,7 @@
namespace espacecollab.backend.infrastructure.sql.SqlRepo.Interface namespace espacecollab.backend.infrastructure.sql.SqlRepo.Interface
{ {
public interface IPeriodeEssaiRepository public interface IPeriodeEssaiRepository : IGenericRepository<PeriodeEssaiSqlDto>
{ {
//inutile !
PeriodeEssaiSqlDto? AddPeriodeEssai(PeriodeEssaiSqlDto newPeriodeEssai);
IList<PeriodeEssaiSqlDto>? GetAllPeriodeEssais();
PeriodeEssaiSqlDto? GetPeriodeEssaiById(PeriodeEssaiSqlDto periodeEssai);
PeriodeEssaiSqlDto UpdatePeriodeEssai(PeriodeEssaiSqlDto periodeEssai);
} }
} }

@ -2,8 +2,8 @@
namespace espacecollab.backend.infrastructure.sql.SqlRepo.Interface namespace espacecollab.backend.infrastructure.sql.SqlRepo.Interface
{ {
public interface IProjetRepository public interface IProjetRepository : IGenericRepository<ProjetSqlDto>
{ {
IList<ProjetSqlDto>? GetProjetsByClient(string client); IList<ProjetSqlDto> GetProjetsByClient(string client);
} }
} }

@ -2,12 +2,11 @@
namespace espacecollab.backend.infrastructure.sql.SqlRepo.Interface namespace espacecollab.backend.infrastructure.sql.SqlRepo.Interface
{ {
public interface IReferencementRepository public interface IReferencementRepository : IGenericRepository<ReferencementSqlDto>
{ {
IList<CollaborateurSqlDto>? GetReferrersByCollaborateurId(int collaborateurId); IList<CollaborateurSqlDto>? GetReferrersByCollaborateurId(int collaborateurId);
IList<CollaborateurSqlDto>? GetReferrersByCollaborateurApsideMail(string apsideMail); IList<CollaborateurSqlDto>? GetReferrersByCollaborateurApsideMail(string apsideMail);
//TODO Voir les attentes concernant "le référent ayant le plus suivi depuis une date" //TODO Voir les attentes concernant "le référent ayant le plus suivi depuis une date"
} }
} }
Loading…
Cancel
Save