Ajout des agences avec généricité

pull/2/head
Clement FERRERE 3 years ago
parent ad64563055
commit b7064ccc44
  1. 12
      Collaborateur_Epa_Back/espacecollab.backend.api/Controllers/AgenceController.cs
  2. 68
      Collaborateur_Epa_Back/espacecollab.backend.api/Controllers/BaseController.cs
  3. 61
      Collaborateur_Epa_Back/espacecollab.backend.api/Controllers/CollaborateursController.cs
  4. 3
      Collaborateur_Epa_Back/espacecollab.backend.api/Register.cs
  5. 8
      Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/AgenceApiDto.cs
  6. 40
      Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/CollaborateurApiDto.cs
  7. 15
      Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/Mappers/AgenceMapper.cs
  8. 2
      Collaborateur_Epa_Back/espacecollab.backend.appservices.interfaces/IGenericsServices.cs
  9. 14
      Collaborateur_Epa_Back/espacecollab.backend.appservices/AgenceService.cs
  10. 18
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.fake/FakeAgenceRepository.cs
  11. 11
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/AgenceSqlRepository.cs
  12. 2
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/CollaborateurSqlRepository.cs
  13. 2
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/GenericSqlRepository.cs

@ -0,0 +1,12 @@
using espacecollab.backend.appservices;
using espacecollab.backend.appservices.dtos;
namespace espacecollab.backend.api.Controllers
{
public class AgenceController : BaseController<AgenceApiDto>
{
public AgenceController(AgenceService agenceService) : base(agenceService)
{
}
}
}

@ -0,0 +1,68 @@
using espacecollab.backend.appservices.dtos.Interfaces;
using espacecollab.backend.appservices.interfaces;
using Microsoft.AspNetCore.Mvc;
namespace espacecollab.backend.api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public abstract class BaseController<TO> : Controller where TO : class, IGenericIdApiDto
{
protected IGenericsServices<TO> Services { get; }
protected BaseController(IGenericsServices<TO> services)
{
Services = services;
}
[HttpGet]
public ActionResult<IEnumerable<TO>> GetAll()
{
return Ok(Services.GetAll());
}
[HttpGet("{id:int:min(1)}")]
public ActionResult<TO> GetById(uint id)
{
TO? apiDtos = Services.GetById(id);
if (apiDtos == null)
return NotFound();
return Ok(apiDtos);
}
[HttpPost]
public ActionResult<TO> Add(TO apiDto)
{
TO? addedApiDto = Services.Add(apiDto);
if (addedApiDto == null)
return Problem();
return Ok(addedApiDto);
}
[HttpDelete("{id:int:min(1)}")]
public ActionResult Delete(uint id)
{
bool isDeleted = Services.Delete(id);
if (!isDeleted)
return Problem();
return Ok();
}
[HttpPut("{collaborateurId:int:min(1)}")]
public ActionResult<TO> Update(uint id, TO apiDto)
{
if (apiDto.Id != id)
return Unauthorized();
TO? updatedApiDto = Services.Update(apiDto);
if (updatedApiDto == null)
return Problem();
return Ok(updatedApiDto);
}
}
}

@ -4,71 +4,22 @@ using Microsoft.AspNetCore.Mvc;
namespace espacecollab.backend.api.Controllers namespace espacecollab.backend.api.Controllers
{ {
[Route("api/[controller]")] //[Route("api/[controller]")]
[ApiController] //[ApiController]
public class CollaborateursController : ControllerBase public class CollaborateursController : BaseController<CollaborateurApiDto>
{ {
private CollaborateursServices CollaborateursServices { get; } private CollaborateursServices CollaborateursServices { get; }
public CollaborateursController(CollaborateursServices collaborateursServices) public CollaborateursController(CollaborateursServices collaborateursServices) : base(collaborateursServices)
{ {
CollaborateursServices = collaborateursServices; CollaborateursServices = collaborateursServices;
} }
[HttpGet]
public ActionResult<IEnumerable<CollaborateurApiDto>> GetCollaborateurs()
{
return Ok(CollaborateursServices.GetAll());
}
[HttpGet("{collaborateurId:int:min(1)}")]
public ActionResult<CollaborateurApiDto> GetCollaborateurById(uint collaborateurId)
{
CollaborateurApiDto? collaborateur = CollaborateursServices.GetById(collaborateurId);
if (collaborateur == null)
return NotFound();
return Ok(collaborateur);
}
[HttpPost]
public ActionResult<CollaborateurApiDto> AddCollaborateur(CollaborateurApiDto collaborateurApi)
{
CollaborateurApiDto? addedCollaborateur = CollaborateursServices.Add(collaborateurApi);
if (addedCollaborateur == null)
return Problem();
return Ok(addedCollaborateur);
}
[HttpDelete("{collaborateurId:int:min(1)}")]
public ActionResult DeleteCollaborateur(uint collaborateurId)
{
bool isCollaborateurDeleted = CollaborateursServices.Delete(collaborateurId);
if (!isCollaborateurDeleted)
return Problem();
return Ok();
}
[HttpPut("{collaborateurId:int:min(1)}")]
public ActionResult<CollaborateurApiDto> UpdateCollaborateur(uint collaborateurId, CollaborateurApiDto collaborateurApi)
{
if (collaborateurApi.Id != collaborateurId)
return Unauthorized();
CollaborateurApiDto? updatedCollaborateur = CollaborateursServices.Update(collaborateurApi);
if (updatedCollaborateur == null)
return Problem();
return Ok(updatedCollaborateur);
}
[HttpGet("businessunit/{businessUnitId:int:min(1)}")] [HttpGet("businessunit/{businessUnitId:int:min(1)}")]
public ActionResult<IEnumerable<CollaborateurApiDto>> GetCollaborateursByBusinessUnit(uint businessUnitId) public ActionResult<IEnumerable<CollaborateurApiDto>> GetCollaborateursByBusinessUnit(uint businessUnitId)
{ {
IEnumerable<CollaborateurApiDto> collaborateurs = CollaborateursServices.GetCollaborateursByBusinessUnit(businessUnitId); IEnumerable<CollaborateurApiDto> collaborateurs = CollaborateursServices.GetCollaborateursByBusinessUnit(businessUnitId);
if (collaborateurs == null) if (!collaborateurs.Any())
return NotFound(); return NotFound();
return Ok(collaborateurs); return Ok(collaborateurs);
@ -78,7 +29,7 @@ namespace espacecollab.backend.api.Controllers
public ActionResult<IEnumerable<CollaborateurApiDto>> GetCollaborateursByReferrer(uint referrerId) public ActionResult<IEnumerable<CollaborateurApiDto>> GetCollaborateursByReferrer(uint referrerId)
{ {
IEnumerable<CollaborateurApiDto> collaborateurs = CollaborateursServices.GetCollaborateursByReferrer(referrerId); IEnumerable<CollaborateurApiDto> collaborateurs = CollaborateursServices.GetCollaborateursByReferrer(referrerId);
if (collaborateurs == null) if (!collaborateurs.Any())
return NotFound(); return NotFound();
return Ok(collaborateurs); return Ok(collaborateurs);

@ -12,15 +12,18 @@ internal static class Register
public static void InjectDependencies(IServiceCollection services, SqlOption contextOptions) public static void InjectDependencies(IServiceCollection services, SqlOption contextOptions)
{ {
services.AddScoped<CollaborateursServices>(); services.AddScoped<CollaborateursServices>();
services.AddScoped<AgenceService>();
if (contextOptions.LoadFake) if (contextOptions.LoadFake)
{ {
services.AddSingleton<ICollaborateurRepository, FakeCollaborateurRepository>(); services.AddSingleton<ICollaborateurRepository, FakeCollaborateurRepository>();
services.AddSingleton<IAgenceRepository, FakeAgenceRepository>();
} }
else else
{ {
services.AddScoped<MainDbContext>(); services.AddScoped<MainDbContext>();
services.AddScoped<ICollaborateurRepository, CollaborateurSqlRepository>(); services.AddScoped<ICollaborateurRepository, CollaborateurSqlRepository>();
services.AddScoped<IAgenceRepository, AgenceSqlRepository>();
} }
} }
} }

@ -0,0 +1,8 @@
using espacecollab.backend.appservices.dtos.Interfaces;
namespace espacecollab.backend.appservices.dtos;
public record AgenceApiDto(uint Id, string Name) : IGenericIdApiDto
{
public uint Id { get; set; } = Id;
}

@ -2,43 +2,9 @@
namespace espacecollab.backend.appservices.dtos namespace espacecollab.backend.appservices.dtos
{ {
public class CollaborateurApiDto : IGenericIdApiDto public record CollaborateurApiDto(uint Id, string Name, string FirstName, DateTime BirthDate, EnumGenreApi Gender, EnumStatutApi Status, int ChildrenNumber, string Address,
string Telephone, string PersonalMail, string ApsideMail, DateTime ResignationDate, int ReferrerId, int BusinessUnitId) : IGenericIdApiDto
{ {
public uint Id { get; set; } public uint Id { get; set; } = Id;
public string Name { get; set; }
public string FirstName { get; set; }
public DateTime BirthDate { get; set; }
public EnumGenreApi Gender { get; set; }
public EnumStatutApi Status { get; set; }
public int ChildrenNumber { get; set; }
public string Address { get; set; }
public string Telephone { get; set; }
public string PersonalMail { get; set; }
public string ApsideMail { get; set; }
public DateTime ResignationDate { get; set; }
public int ReferrerId { get; set; }
public int BusinessUnitId { get; set; }
public CollaborateurApiDto()
{
}
public CollaborateurApiDto(uint id, string name, string firstName, DateTime birthDate, EnumGenreApi gender, EnumStatutApi status, int childrenNumber, string address, string telephone, string personalMail, string apsideMail, DateTime resignationDate, int referrerId, int businessUnitId)
{
Id = id;
Name = name;
FirstName = firstName;
BirthDate = birthDate;
Gender = gender;
Status = status;
ChildrenNumber = childrenNumber;
Address = address;
Telephone = telephone;
PersonalMail = personalMail;
ApsideMail = apsideMail;
ResignationDate = resignationDate;
ReferrerId = referrerId;
BusinessUnitId = businessUnitId;
}
} }
} }

@ -0,0 +1,15 @@
using espacecollab.backend.infrastructure.sql.dtos;
namespace espacecollab.backend.appservices.dtos.Mappers;
public static class AgenceMapper
{
public static AgenceApiDto ToApi(this AgenceSqlDto agenceSqlDto)
{
return new AgenceApiDto((uint)agenceSqlDto.Id, agenceSqlDto.Name);
}
public static AgenceSqlDto ToSql(this AgenceApiDto agenceApiDto)
{
return new AgenceSqlDto((int)agenceApiDto.Id, agenceApiDto.Name);
}
}

@ -11,5 +11,7 @@ namespace espacecollab.backend.appservices.interfaces
T? GetById(uint id); T? GetById(uint id);
T? Update(T apiDto); T? Update(T apiDto);
bool Delete(uint apiDtoId);
} }
} }

@ -0,0 +1,14 @@
using espacecollab.backend.appservices.dtos;
using espacecollab.backend.appservices.dtos.Mappers;
using espacecollab.backend.infrastructure.interfaces;
using espacecollab.backend.infrastructure.sql.dtos;
namespace espacecollab.backend.appservices;
public class AgenceService : GenericsServices<AgenceSqlDto, AgenceApiDto>
{
public AgenceService(IAgenceRepository agenceRepository)
:base(agenceRepository, AgenceMapper.ToApi, AgenceMapper.ToSql)
{
}
}

@ -0,0 +1,18 @@
using espacecollab.backend.infrastructure.interfaces;
using espacecollab.backend.infrastructure.sql.dtos;
namespace espacecollab.backend.infrastructure.fake
{
public class FakeAgenceRepository : GenericFakeRepository<AgenceSqlDto>, IAgenceRepository
{
public FakeAgenceRepository()
{
Context = new List<AgenceSqlDto>
{
new(1, "Tours"),
new(2, "Clermont-Ferrand")
};
}
}
}

@ -0,0 +1,11 @@
using espacecollab.backend.infrastructure.interfaces;
using espacecollab.backend.infrastructure.sql.dtos;
namespace espacecollab.backend.infrastructure.sql.Repository;
public class AgenceSqlRepository : GenericSqlRepository<AgenceSqlDto>, IAgenceRepository
{
public AgenceSqlRepository(MainDbContext context) : base(context)
{
}
}

@ -5,8 +5,6 @@ namespace espacecollab.backend.infrastructure.sql.Repository
{ {
public class CollaborateurSqlRepository : GenericSqlRepository<CollaborateurSqlDto>, ICollaborateurRepository public class CollaborateurSqlRepository : GenericSqlRepository<CollaborateurSqlDto>, ICollaborateurRepository
{ {
private MainDbContext Context { get; }
public CollaborateurSqlRepository(MainDbContext context) : base(context) public CollaborateurSqlRepository(MainDbContext context) : base(context)
{ {
Context = context; Context = context;

@ -5,7 +5,7 @@ namespace espacecollab.backend.infrastructure.sql.Repository
{ {
public class GenericSqlRepository<T> : IGenericRepository<T> where T : class, IGenericIdSqlDto public class GenericSqlRepository<T> : IGenericRepository<T> where T : class, IGenericIdSqlDto
{ {
private MainDbContext Context { get; } protected MainDbContext Context { get; set; }
public GenericSqlRepository(MainDbContext context) public GenericSqlRepository(MainDbContext context)
{ {

Loading…
Cancel
Save